Nginx 提供了反向代理和負載均衡的功能,通過合理處理業務的分攤,從而提供網站的處理能力,通過緩存的功能,可以把不經常更新的動態資源,進行靜態緩存處理。
1. 反向代理
反向代理功能在 Nginx 中是最重要的功能,負載均衡功能就是從反向代理功能中衍生出來的。
-
正向代理 VS 反向代理
正向代理,是一個位于客戶端和目標服務器之間的代理服務器,客戶端將發送的請求和指定的目標服務器提交給代理服務器,然后代理服務器向目標服務器發起請求,代理服務器并將獲得的響應結果返回給客戶端的過程
從圖示可以看出,客戶端用戶不能直接訪問目標服務器,需要通過請求代理服務器來訪問目標服務器,客戶端用戶需要代理,必須通過配置客戶端來完成代理。
反向代理,反向代理對于客戶端而言就是目標服務器,客戶端向反向代理服務器發送請求后,反向代理服務器將該請求轉發給內部網絡上的后端服務器,并將從后端服務器上得到的響應結果返回給客戶端
用戶A 、用戶B 、用戶C 同時對反向代理服務器發送請求,反向代理服務器則根據其內部的具體配置,將用戶的請求分發給后端服務器進行處理,并將后端服務器處理后的響應結果作為自己的響應結果返回給用戶。反向代理服務器的整個處理過程,用戶并不知情。
- 反向代理服務配置
- 虛擬三臺主機: 一臺 nginx 代理服務器(192.168.1.188) 、 一臺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 配置文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.cqzhangjian80.com;
root /opt/tomcat8080/webapps/ROOT;
index index.html index.htm index.jsp index.do;
#charset koi8-r;
#access_log logs/host.access.log main;
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_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
server {
listen 80;
server_name www.cqzhangjian81.com;
root /opt/tomcat8081/webapps/ROOT;
index index.html index.htm index.jsp index.do;
#charset koi8-r;
#access_log logs/host.access.log main;
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_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
-
客戶機演示
訪問http://www.cqzhangjian80.com 服務器
4.反向代理常用指令
最重要的指令為:proxy_ pass 指定被代理的服務器地址
2.負載均衡
負載均衡( load balance)就是將負載分攤到多個操作單元上執行,從而提高服務的可用
性和響應速度,增強用戶體驗。
2.1 負載方式
- 1、輪詢(默認):每個請求按時間順序逐一分配到不同的后端服務器,如果后端某臺服務器宕機,則自動剔除故障機器,使用戶訪問不受影響。
配置文件修改:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.cqzhangjian.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://myserver;
}
}
upstream myserver {
server 127.0.0.1:8080;
server 127.0.0.1:8081;
}
}
- 2、weight:指定輪詢權重,weight值越大,分配到的幾率就越高,主要用于后端每臺服務器性能不均衡的情況。
配置文件修改:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.cqzhangjian.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://myserver;
}
}
upstream myserver {
server 127.0.0.1:8080 weight=5;
server 127.0.0.1:8081 weight=2;
}
}
weight 值越大,訪問的頻率越高。
負載其他參數:
- 3、ip_hash:每個請求按訪問IP的哈希結果分配,這樣每個訪客固定訪問一個后端服務器,可以有效的解決動態網頁存在的session共享問題
配置文件修改:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.cqzhangjian.com;
#charset koi8-r;
#access_log logs/host.access.log main;
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 配置文件
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.cqzhangjian.com;
#charset koi8-r;
#access_log logs/host.access.log main;
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 - 配置文件修改:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.cqzhangjian.com;
location / {
allow all;
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;
}
}
}
}