三、nginx 之 負載均衡和快取
Nginx 提供了反向代理和負載均衡的功能,通過合理處理業務的分攤,從而提供網站的處理能力,通過快取的功能,可以把不經常更新的動態資源,進行靜態快取處理。
1. 反向代理
反向代理功能在 Nginx 中是最重要的功能,負載均衡功能就是從反向代理功能中衍生出來的。
-
正向代理 VS 反向代理
正向代理,是一個位於客戶端和目標伺服器之間的代理伺服器,客戶端將傳送的請求和指定的目標伺服器提交給代理伺服器,然後代理伺服器向目標伺服器發起請求,代理伺服器並將獲得的響應結果返回給客戶端的過程

正向代理圖示
從圖示可以看出,客戶端使用者不能直接訪問目標伺服器,需要通過請求代理伺服器來訪問目標伺服器,客戶端使用者需要代理,必須通過配置客戶端來完成代理。
反向代理,反向代理對於客戶端而言就是目標伺服器,客戶端向反向代理伺服器傳送請求後,反向代理伺服器將該請求轉發給內部網路上的後端伺服器,並將從後端伺服器上得到的響應結果返回給客戶端

反向代理圖示
使用者A 、使用者B 、使用者C 同時對反向代理伺服器傳送請求,反向代理伺服器則根據其內部的具體配置,將使用者的請求分發給後端伺服器進行處理,並將後端伺服器處理後的響應結果作為自己的響應結果返回給使用者。反向代理伺服器的整個處理過程,使用者並不知情。
- 反向代理服務配置
- 虛擬三臺主機: 一臺 nginx 代理伺服器(192.168.1.188) 、 一臺 ofollow,noindex">http://www.cqzhangjian80.com/ 伺服器 、 另外一臺 http://www.cqzhangjian81.com/
在客戶端主機 修改 hosts 檔案
192.168.1.188 www.cqzhangjian80.com 192.168.1.188 www.cqzhangjian81.com
2.配置 nginx 配置檔案
#usernobody; worker_processes1; #error_loglogs/error.log; #error_loglogs/error.lognotice; #error_loglogs/error.loginfo; #pidlogs/nginx.pid; events { worker_connections1024; } http { includemime.types; default_typeapplication/octet-stream; #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" ' #'$status $body_bytes_sent "$http_referer" ' #'"$http_user_agent" "$http_x_forwarded_for"'; #access_loglogs/access.logmain; sendfileon; #tcp_nopushon; #keepalive_timeout0; keepalive_timeout65; #gzipon; server { listen80; server_namewww.cqzhangjian80.com; root /opt/tomcat8080/webapps/ROOT; index index.html index.htm index.jsp index.do; #charset koi8-r; #access_loglogs/host.access.logmain; location ~ /(WEB-INF|META-INF) { deny all; } location ~ \.(jsp|do)$ { proxy_pass http://127.0.0.1:8080; proxy_set_header X-client-IP $remote_addr; } location ~ ^/(docs|examples)(/.*)*$ { root /opt/apache-tomcat-8.5.34/webapps; } error_page500 502 503 504/50x.html; location = /50x.html { roothtml; } } server { listen80; server_namewww.cqzhangjian81.com; root /opt/tomcat8081/webapps/ROOT; index index.html index.htm index.jsp index.do; #charset koi8-r; #access_loglogs/host.access.logmain; location ~ /(WEB-INF|META-INF) { deny all; } location ~ \.(jsp|do)$ { proxy_pass http://127.0.0.1:8081; proxy_set_header X-client-IP $remote_addr; } location ~ ^/(docs|examples)(/.*)*$ { root /opt/apache-tomcat-8.5.34/webapps; } error_page500 502 503 504/50x.html; location = /50x.html { roothtml; } } }
-
客戶機演示
訪問http://www.cqzhangjian80.com 伺服器

訪問http://www.cqzhangjian81.com 伺服器
4.反向代理常用指令

常用指令
最重要的指令為:proxy_ pass 指定被代理的伺服器地址
2.負載均衡
負載均衡( load balance)就是將負載分攤到多個操作單元上執行,從而提高服務的可用
性和響應速度,增強使用者體驗。

負載均衡原理圖
2.1 負載方式
- 1、輪詢(預設):每個請求按時間順序逐一分配到不同的後端伺服器,如果後端某臺伺服器宕機,則自動剔除故障機器,使使用者訪問不受影響。
配置檔案修改:
#usernobody; worker_processes1; #error_loglogs/error.log; #error_loglogs/error.lognotice; #error_loglogs/error.loginfo; #pidlogs/nginx.pid; events { worker_connections1024; } http { includemime.types; default_typeapplication/octet-stream; #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" ' #'$status $body_bytes_sent "$http_referer" ' #'"$http_user_agent" "$http_x_forwarded_for"'; #access_loglogs/access.logmain; sendfileon; #tcp_nopushon; #keepalive_timeout0; keepalive_timeout65; #gzipon; server { listen80; server_namewww.cqzhangjian.com; #charset koi8-r; #access_loglogs/host.access.logmain; location/ { proxy_pass http://myserver; } } upstream myserver { server 127.0.0.1:8080; server 127.0.0.1:8081; } }
-
2、weight:指定輪詢權重,weight值越大,分配到的機率就越高,主要用於後端每臺伺服器效能不均衡的情況。
配置檔案修改:
#usernobody; worker_processes1; #error_loglogs/error.log; #error_loglogs/error.lognotice; #error_loglogs/error.loginfo; #pidlogs/nginx.pid; events { worker_connections1024; } http { includemime.types; default_typeapplication/octet-stream; #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" ' #'$status $body_bytes_sent "$http_referer" ' #'"$http_user_agent" "$http_x_forwarded_for"'; #access_loglogs/access.logmain; sendfileon; #tcp_nopushon; #keepalive_timeout0; keepalive_timeout65; #gzipon; server { listen80; server_namewww.cqzhangjian.com; #charset koi8-r; #access_loglogs/host.access.logmain; location/ { proxy_pass http://myserver; } } upstream myserver { server 127.0.0.1:8080weight=5; server 127.0.0.1:8081weight=2; } }
weight 值越大,訪問的頻率越高。
負載其他引數:

其他引數
- 3、ip_hash:每個請求按訪問IP的雜湊結果分配,這樣每個訪客固定訪問一個後端伺服器,可以有效的解決動態網頁存在的session共享問題
配置檔案修改:
#usernobody; worker_processes1; #error_loglogs/error.log; #error_loglogs/error.lognotice; #error_loglogs/error.loginfo; #pidlogs/nginx.pid; events { worker_connections1024; } http { includemime.types; default_typeapplication/octet-stream; #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" ' #'$status $body_bytes_sent "$http_referer" ' #'"$http_user_agent" "$http_x_forwarded_for"'; #access_loglogs/access.logmain; sendfileon; #tcp_nopushon; #keepalive_timeout0; keepalive_timeout65; #gzipon; server { listen80; server_namewww.cqzhangjian.com; #charset koi8-r; #access_loglogs/host.access.logmain; location/ { proxy_pass http://myserver; } } upstream myserver { ip_hash; server 127.0.0.1:8080; server 127.0.0.1:8081; } }
注意:使用ip_hash 不能 跟 backup 和 weight 引數 使用。
- 4、fair(第三方):更智慧的一個負載均衡演算法,此演算法可以根據頁面大小和載入時間長短智慧地進行負載均衡,也就是根據後端伺服器的響應時間來分配請求,響應時間短的優先分配。如果想要使用此排程演算法,需要Nginx的upstream_fair模組。
- 4.1 下載 fair 模組:
https://github.com/gnosek/nginx-upstream-fair
注意:
nginx-upstream-fair-master fair模組原始碼
官方github下載地址: https://github.com/gnosek/nginx-upstream-fair
說明:如果從github下載最新版本,在安裝到nginx 1.14.0版本時,會報出編譯錯誤。需要對原始碼做一些修改,修改參照(如果你看到這篇文章時,github主已經修改了該bug,或者你用的是nginx 1.14.0以下版本,請忽視...): https://github.com/gnosek/nginx-upstream-fair/pull/27/commits/ff979a48a0ccb9217437021b5eb9378448c2bd9e
對於比較懶的童鞋,這裡提供了已經修改好的原始碼包: https://files.cnblogs.com/files/ztlsir/nginx-upstream-fair-master.zip
- 4.2 重寫編譯nginx:
./configure \ --prefix=/opt/nginx \ --with-http_ssl_module \ --add-module=/opt/nginx-fair make && make install
- 4.3 配置檔案
#usernobody; worker_processes1; #error_loglogs/error.log; #error_loglogs/error.lognotice; #error_loglogs/error.loginfo; #pidlogs/nginx.pid; events { worker_connections1024; } http { includemime.types; default_typeapplication/octet-stream; #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" ' #'$status $body_bytes_sent "$http_referer" ' #'"$http_user_agent" "$http_x_forwarded_for"'; #access_loglogs/access.logmain; sendfileon; #tcp_nopushon; #keepalive_timeout0; keepalive_timeout65; #gzipon; server { listen80; server_namewww.cqzhangjian.com; #charset koi8-r; #access_loglogs/host.access.logmain; location/ { proxy_pass http://myserver; } } upstream myserver { fair; server 127.0.0.1:8080; server 127.0.0.1:8081; } }
- 5、url_hash(第三方):按訪問URL的雜湊結果來分配請求,使每個URL定向到同一臺後端伺服器,可以進一步提高後端快取伺服器的效率。如果想要使用此排程演算法,需要Nginx的hash軟體包。
自學!!!!
3.快取
Nginx 提供了兩種 Web 快取方式, 一種是永久性快取,另一種是臨時性快取。通過反向代理伺服器對訪問較多的內容進行快取,降低後端伺服器的訪問壓力
3.1 永久快取
- 準備一臺 快取伺服器: www.cqzhangjian.com
一臺源資料伺服器:127.0.0.1:8080 - 配置檔案修改:
#usernobody; worker_processes1; #error_loglogs/error.log; #error_loglogs/error.lognotice; #error_loglogs/error.loginfo; #pidlogs/nginx.pid; events { worker_connections1024; } http { includemime.types; default_typeapplication/octet-stream; #log_formatmain'$remote_addr - $remote_user [$time_local] "$request" ' #'$status $body_bytes_sent "$http_referer" ' #'"$http_user_agent" "$http_x_forwarded_for"'; #access_loglogs/access.logmain; sendfileon; #tcp_nopushon; #keepalive_timeout0; keepalive_timeout65; #gzipon; server { listen80; server_namewww.cqzhangjian.com; location/ { allowall; root cache; proxy_store on; proxy_store_access user:rw group:rw all:r; proxy_temp_path cache_tmp; if (!-e $request_filename) { proxy_pass http://127.0.0.1:8080; } } } }