nginx結合tomcat實現反向代理和負載均衡的部署
部署環境:CentOS6.5 nginx1.8 tomcat7
下載nginx地址:http://nginx.org/en/download.html
nginx安裝
1.安裝時依賴環境:yum -y install make gcc-c++ zlib zlib-devel openssl openssl-devel libtool
2.把nginx-1.8.1.tar.gz放在 /usr/local/ 下,並解壓。
3.進行configure配置,執行此命令:cd /usr/local/nginx-1.8.1 && ./configure --prefix=/usr/local/nginx
4.編譯並安裝,執行此命令:make && make install。安裝完畢後,將在/usr/local/下看到nginx資料夾,nginx資料夾中有四個資料夾:
conf存放配置檔案 html存放靜態頁面 logs存放日誌檔案 sbin執行檔案
配置conf/下的 nginx.conf 檔案,配置如下:
#開啟程序數 <=CPU數 worker_processes 1; #自定義錯誤日誌儲存位置,全域性設定,預設logs/error.log #error_log logs/error.log; #每個程序最大連線數(最大連線=連線數x程序數)每個worker允許同時產生多少個連結,預設1024 events { worker_connections 1024; } # http { #副檔名與檔案型別對映表 include mime.types; #預設檔案型別 default_type application/octet-stream; #自定義日誌檔案輸出格式 全域性設定 log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; #自定義全域性請求日誌儲存位置,全域性設定,預設logs/access.log, 定義格式:檔案儲存位置 + 日誌輸出格式 #access_log logs/access.log main; #打開發送檔案 sendfile on; #連線超時時間 #keepalive_timeout 0; keepalive_timeout 65; #開啟gzip壓縮 #gzip on; #配置虛擬主機,基於域名、ip和埠,可以配置多個server server { #監聽埠,可以是ip:port 或者 port listen 80; #監聽域名,可以是ip或者域名,server_name有三種匹配方式:精準匹配(www.domain.com)、萬用字元匹配(*.domain.com 、www.*)、正則表示式匹配(~^(?.+)\.domain\.com$) server_name localhost; #自定義請求日誌,區域性,當前server有效 #access_log logs/host.access.log main; #錯誤頁面及其返回地址 error_page 500 502 503 504 /50x.html; #請求匹配,同樣有精準(= /index.html)、模糊(~ index)、正則,允許配置多個location location / { #返回根路徑地址(相對路徑:相對於/usr/local/nginx/) root html; #預設主頁 index index.html index.htm; } # location /html { root html; index index.html index.htm; } # } # }
5.啟動 nginx
/usr/local/nginx/sbin/nginx //啟動
/usr/local/nginx/sbin/nginx -s stop //關閉
/usr/local/nginx/sbin/nginx -s reload //重啟
ps -ef | grep nginx //檢視啟動狀態
在瀏覽器訪問,出現下圖歡迎頁面表示 nginx 安裝成功:
nginx反向代理、動靜分離、負載均衡
正向代理:一個位於客戶端和原始伺服器(origin server)之間的伺服器,為了從原始伺服器取得內容,客戶端向代理髮送一個請求並指定目標(原始伺服器),然後代理向原始伺服器轉交請求並將獲得的內容返回給客戶端。客戶端才能使用正向代理。(
反向代理(Reverse Proxy):方式是指以代理伺服器來接受internet上的連線請求,然後將請求,發給內部網路上的伺服器,並將從伺服器上得到的結果返回給internet上請求連線的客戶端,此時代理伺服器對外就表現為一個反向代理伺服器。(來自百度百科的解釋)
nginx反向代理其實主要通過配置 proxy_pass引數即可代理到某個伺服器,新增如下配置:
vim /usr/local/nginx/conf/nginx.conf
location / { //在對應的 location 中配置,配置中的監聽埠為80
proxy_pass http://127.0.0.1:8080 #我這裡tomcat的埠為8080
root html;
index index.html index.htm;
}
啟動 tomcat、nginx,/usr/local/nginx/sbin/nginx tomcat
啟動: /usr/local/tomcat7/bin/startup.sh
關閉: /usr/local/tomcat7/bin/shutdown.sh
ngnix
啟動: /usr/local/nginx/sbin/nginx
關閉: /usr/local/nginx/sbin/nginx -s stop
測試:http://192.168.2.144/
但是當我們訪問 http://192.168.2.144/demo.jsp,如下圖:demo.jsp,將demo.jsp拷貝到tomcat中ROOT下,中程式碼如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Welcome to my demo.jsp</title>
</head>
<body>
歡迎 my demo.jsp<br/>
訪問的 ip 地址: <%=request.getHeader("real_ip") %> <br/>
nginx server ip is: <%=request.getRemoteAddr()%>
</body>
</html>
我們得到的是nginx代理伺服器的ip,但是我們並不想要代理伺服器的ip,我們想要的是客戶端真實的訪問ip,因此我們可以通過nginx中獲取真實ip,然後傳給真實伺服器。同樣,我們需要到 nginx.conf 中配置,如下 :
location / {
#設定真實ip
proxy_set_header real_ip $remote_addr; //real_ip 設定變數名,可以通過web端獲取
proxy_pass http://127.0.0.1:8080;
root html;
index index.html index.htm;
}
接下來,重啟 nginx 、 tomcat,訪問瀏覽器
動態資源、靜態資源分離
動態資源、靜態資源分離是讓動態網站裡的動態網頁根據一定規則把不變的資源和經常變的資源區分開來,動靜資源做好了拆分以後,我們就可以根據靜態資源的特點將其做快取操作,這就是網站靜態化處理的核心思路
動態資源、靜態資源分離簡單的概括是:動態檔案與靜態檔案的分離。
為什麼要做動 、靜分離?
在我們的軟體開發中,有些請求是需要後臺處理的(如:.jsp,.do等等),有些請求是不需要經過後臺處理的(如:css、html、jpg、js等等檔案),這些不需要經過後臺處理的檔案稱為靜態檔案,否則動態檔案。因此我們後臺處理忽略靜態檔案。這會有人又說那我後臺忽略靜態檔案不就可以了。當然這是可以的,但是這樣後臺的請求次數就明顯增多了。在我們對資源的響應速度有要求的時候,我們應該使用這種動靜分離的策略去解決。
動、靜分離將網站靜態資源(HTML,JavaScript,CSS,img等檔案)與後臺應用分開部署,提高使用者訪問靜態程式碼的速度,降低對後臺應用訪問。這裡我們將靜態資源放到nginx中,動態資源轉發到tomcat伺服器中。
因此,動態資源轉發到tomcat伺服器我們就使用到了前面講到的反向代理了。
vim /usr/local/nginx/conf/nginx.conf
<span style="white-space:pre"> </span>location / {
#設定真實ip
proxy_set_header real_ip $remote_addr;
proxy_pass http://127.0.0.1:8080;
root html;
index index.html index.htm;
}
修改為:
//動態資源
location ~ \.(jsp|jspx|do|action)(\/.*)?$ {
//動態請求轉發到tomcat伺服器,匹配方式可自定義
#設定真實
ip proxy_set_header real_ip $remote_addr; //real_ip 設定變數名,可以通過web端獲取
proxy_pass http://127.0.0.1:8080;
}
//靜態資源
location ~ .*\(js|css|htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ {
//靜態資源到nginx伺服器下static(具體目錄自定義)獲取
root static;
}
啟動tomcat、nginx,訪問http://192.168.58.149/123.jpg和http://192.168.58.149/demo.jsp負載均衡
負載均主要衡解決網路擁塞問題,提高伺服器響應速度,服務就近提供,達到更好的訪問質量,減少後臺伺服器大併發壓力。主要根據演算法策略將請求分攤到多臺後臺伺服器處理。我們以weight(權重)策略為例:
nginx
實現負載均衡主要通過upstream,以兩個tomcat伺服器為例:192.168.58.149:8080 和 192.168.58.150:8080
nginx.conf 配置如下:
在 http 下配置:
<span style="color:#2f2f2f;"> upstream balance {
#weight 值越大,負載權重越大,請求次數越多
#max_fails 允許請求失敗的次數,超過失敗次數後,轉發到下一個伺服器,當有max_fails個請求失敗,就表示後端的伺服器不可用,預設為1,將其設定為0可以關閉檢查
#fail_timeout 指定時間內無響應則失敗, 在以後的fail_timeout時間內nginx不會再把請求發往已檢查出標記為不可用的伺服器
#down 表示當前server不參與負載
#backup 其他非backup server都忙的時候,backup server作為備用伺服器,將請求轉發到backup伺服器
server 192.168.2.190:8080 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.2.39:8080 weight=1 max_fails=2 fail_timeout=30s;
server 127.0.0.1:8080 down;
#server 192.168.58.152:8080 backup;
}
location / {
#設定真實ip
proxy_set_header real_ip $remote_addr;
#real_ip 設定變數名,可以通過web端獲取
proxy_pass http://balance; //配置上面新增的負載伺服器
}
</span><span style="color:#ff0000;">注意upstream放在http下,和server為同級,不然會報 nginx: [emerg] "xxx" directive is not allowed here in xxxx 之類報錯。</span>
上面提到的weight權重策略,還有ip_hash、url_hash雜湊策略啟動149的tomcat、150的tomcat、nginx,訪問瀏覽器試試