一 Nginx代理1.1 Nginx代理概述nginx是一款自由的、開源的、高性能的HTTP服務(wù)器和反向代理服務(wù)器。同時也是一個IMAP、POP3、SMTP代理服務(wù)器。nginx可以作為一個HTTP服務(wù)器進行網(wǎng)站的發(fā)布處理,同時nginx可以作為反向代理進行負載均衡的實現(xiàn)。
1.2 Nginx代理模式Nginx通常有如下三種代理模式:
1.3 正向代理正向代理(forward)是一個位于客戶端和原始服務(wù)器(origin server)之間的服務(wù)器,即代理服務(wù)器。為了從原始服務(wù)器取得內(nèi)容,客戶端向代理服務(wù)器發(fā)送一個請求并指定目標(biāo)原始服務(wù)器,然后代理服務(wù)器向原始服務(wù)器轉(zhuǎn)交請求并將獲得的內(nèi)容返回給客戶端。
場景一:客戶端訪問本來無法訪問的原始服務(wù)器的資源
假設(shè)最初客戶端要訪問原始服務(wù)器需要經(jīng)過R1和R2路由器這樣一個路由節(jié)點,如果路由器R1或者路由器R2發(fā)生故障(或存在禁止訪問的安全策略等),那么就無法訪問原始服務(wù)器了。
但是如果客戶端讓代理服務(wù)器去代替自己訪問原始服務(wù)器,由于代理服務(wù)器沒有在路由器R1或R2節(jié)點中,而是通過其它的路由節(jié)點訪問的原始服務(wù)器,那么客戶端就可以得到原始服務(wù)器的數(shù)據(jù)了。
場景二:加速訪問原始服務(wù)器的資源
假設(shè)客戶端要訪問原始服務(wù)器,經(jīng)過R1路由器和R2路由器,而R1到R2路由器的鏈路是一個低帶寬鏈路。而客戶端到代理服務(wù)器,從代理服務(wù)器到原始服務(wù)器都是高帶寬鏈路。那么使用正向代理則可以加速訪問。
場景三:Cache作用
Cache(緩存)技術(shù)和代理服務(wù)技術(shù)是緊密聯(lián)系的(不光是正向代理,反向代理也使用了Cache(緩存)技術(shù))。
如上圖所示,假設(shè)客戶端要訪問原始服務(wù)器某數(shù)據(jù)A之前,代理服務(wù)器已經(jīng)通過訪問原始服務(wù)器數(shù)據(jù)A,那么代理服務(wù)器會把數(shù)據(jù)A保存一段時間,此時再次通過代理服務(wù)器訪問數(shù)據(jù)A,那么代理服務(wù)器不再訪問原始服務(wù)器,而把緩存的數(shù)據(jù)A直接發(fā)給客戶端。這一技術(shù)在Cache中術(shù)語就叫Cache命中。
場景四:代理服務(wù)器實現(xiàn)授權(quán)控制
如上圖所示,防火墻作為網(wǎng)關(guān),用來過濾外網(wǎng)對其的訪問。假設(shè)局域網(wǎng)內(nèi)部客戶端A和客戶端B都設(shè)置了代理服務(wù)器??赏ㄟ^代理服務(wù)器配置客戶端A允許訪問互聯(lián)網(wǎng),而客戶端B不允許訪問互聯(lián)網(wǎng),從而實現(xiàn)不通客戶端的訪問代理授權(quán)控制。
場景五:隱藏客戶端蹤跡
如上圖所示,原始服務(wù)器并不知道訪問自己的實際的客戶端,因為代理服務(wù)器代替客戶端去直接與原始服務(wù)器進行交互,從而對于原始服務(wù)器而言,客戶端的蹤跡被隱藏了。
1.4 反向代理反向代理正好與正向代理相反,對于客戶端而言代理服務(wù)器就像是原始服務(wù)器,并且客戶端不需要進行任何特別的設(shè)置??蛻舳讼蚍聪虼淼拿臻g(name-space)中的內(nèi)容發(fā)送普通請求,接著反向代理將判斷向何處(原始服務(wù)器)轉(zhuǎn)交請求,并將獲得的內(nèi)容返回給客戶端。
場景一:隱藏原始服務(wù)器蹤跡
客戶端始終認為它訪問的是原始服務(wù)器而不是代理服務(wù)器,但實用際上反向代理服務(wù)器接受客戶端的應(yīng)答,從原始資源服務(wù)器中取得客戶端的需求資源,然后發(fā)送給客戶端。由于防火墻的作用,只允許代理服務(wù)器訪問原始資源服務(wù)器。對于此環(huán)境下,防火墻和反向代理的共同作用保護了原始資源服務(wù)器,對于客戶端而言是透明的。
場景二:負載均衡器
當(dāng)反向代理服務(wù)器存在多個,從而部署為集群,當(dāng)多個客戶端訪問原始服務(wù)器(原始服務(wù)器也可以是集群)的時候,不同的代理服務(wù)器應(yīng)答不同的客戶端,然后發(fā)送不同的客戶端所需的資源,從而實現(xiàn)負載均衡效果。
提示:Nginx基于反向代理實現(xiàn)負載均衡配置參考《012.Nginx負載均衡》。
場景三:Cache作用
同時反向代理服務(wù)器類似正向代理服務(wù)器一樣擁有Cache的作用,可以緩存原始資源服務(wù)器的資源,而不是每次都要向原始資源服務(wù)器組請求數(shù)據(jù),特別對于一些靜態(tài)的數(shù)據(jù),比如圖片和普通文件,如果這些反向代理服務(wù)器能夠做到和客戶端來自同一個網(wǎng)絡(luò),那么客戶端訪問反向代理服務(wù)器,就會得到很高質(zhì)量的速度。這正是CDN技術(shù)的核心。場景四:正方代理混合場景
實際項目操作時,正向代理和反向代理很有可能會存在在一個應(yīng)用場景中,正向代理代理客戶端的請求去訪問目標(biāo)服務(wù)器,目標(biāo)服務(wù)器是一個反向代理服務(wù)器,反向代理了多臺真實的業(yè)務(wù)處理服務(wù)器。
1.5 透明代理參考:https://www.cnblogs.com/gbq-dog/p/10653054.html。
1.6 常見代理軟件通常大多數(shù)開源代理軟件,都能實現(xiàn)正反代理兩種方式。開源軟件中如squid,既可以做正向代理,也可以實現(xiàn)反向代理。MS ISA也可以用來在Windows平臺下做正向代理。反向代理中最主要的實踐就是WEB服務(wù),如Nginx。
二 代理配置項2.1 配置語法語法:proxy_buffering on | off;
默認值:proxy_buffering on;
可配置段:http, server, location
作用:配置proxy緩沖區(qū)。
擴展:
語法:proxy_redirect default; proxy_redirect off; proxy_redirect redirect replacement;
默認值:proxy_redirect default;
可配置段:http, server, location
作用:配置proxy重定向。
擴展:
語法:proxy_buffering on | off;
默認值:proxy_buffering on;
可配置段:http, server, location
作用:配置proxy緩沖區(qū)。
擴展:
語法:proxy_set_header field value;
默認值:proxy_set_header Host $proxy_host; proxy_set_header Connection close;
可配置段:http, server, location
作用:配置proxy頭信息。
擴展:
語法:proxy_connect_timeout time;
默認值:proxy_connect_timeout 60s;
可配置段:http, server, location
作用:配置proxy超時。
擴展:
三 配置正向代理3.1 正向代理配置1 [root@proxy ~]# vi /etc/nginx/conf.d/reverse.conf 2 server{ 3 resolver 8.8.8.8; #配置DNS解析IP地址 4 resolver_timeout 30s; #超時時間(5秒) 5 listen 8080; 6 access_log /var/log/nginx/reverse.access.log main; 7 error_log /var/log/nginx/reverse.error.log warn; 8 location / { 9 proxy_pass http://$http_host$request_uri; #配置正向代理參數(shù) 10 proxy_set_header Host $http_host; #解決如果URL中帶"."后Nginx 503錯誤 11 proxy_buffers 256 4k; #配置緩存大小 12 proxy_max_temp_file_size 0; #關(guān)閉磁盤緩存讀寫減少I/O 13 proxy_connect_timeout 30; #代理連接超時時間 14 proxy_cache_valid 200 302 10m; 15 proxy_cache_valid 301 1h; 16 proxy_cache_valid any 1m; #配置代理服務(wù)器緩存時間 17 } 18 } 1 [root@proxy ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@proxy ~]# nginx -s reload #重載配置文件 配置釋義:
不能有hostname。
必須有resolver, 即dns,超時時間(30秒)可選。
配置正向代理參數(shù),均是由 Nginx 變量組成。
提示:配置好后,重啟nginx,以瀏覽器為例,若需要使用這個代理服務(wù)器,則只需將瀏覽器代理設(shè)置為http://+服務(wù)器ip地址+:+80即可使用了。
參考文檔:https://www.cnblogs.com/gbq-dog/p/10653054.html
四 反向代理配置4.1 環(huán)境預(yù)設(shè)4.2 配置反向代理[root@proxy ~]# vi /etc/nginx/conf.d/forward.conf server { listen 80; server_name forward.; access_log /var/log/nginx/forward.access.log main; error_log /var/log/nginx/forward.error.log warn; location / { proxy_pass https://www./; proxy_redirect off; # proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 10m; #允許客戶端請求的最大單文件字節(jié)數(shù) client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù) proxy_connect_timeout 300; #nginx跟后端服務(wù)器連接超時時間(代理連接超時) proxy_send_timeout 300; #后端服務(wù)器數(shù)據(jù)回傳時間(代理發(fā)送超時) proxy_read_timeout 300; #連接成功后,后端服務(wù)器響應(yīng)時間(代理接收超時) proxy_buffer_size 4k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小 proxy_buffers 4 32k; #proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的話,這樣設(shè)置 proxy_busy_buffers_size 64k; #高負荷下緩沖大小(proxy_buffers*2) proxy_temp_file_write_size 64k; #設(shè)定緩存文件夾大小,大于這個值,將從upstream服務(wù)器傳 } } 1 [root@proxy ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@proxy ~]# nginx -s reload #重載配置文件 配置釋義:
proxy_set_header X-Real-IP $remote_addr:把源IP【$remote_addr,建立HTTP連接header里面的信息】賦值給X-Real-IP,從而通過$X-Real-IP來獲取源IP;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for:在nginx作為代理服務(wù)器時,設(shè)置的IP列表,會把經(jīng)過的機器ip,代理機器ip都記錄下來,用【,】隔開。
4.3 測試反向代理瀏覽器訪問:http://forward./
參考文檔:https://www.runoob.com/w3cnote/nginx-proxy-balancing.html
4.4 其他代理配置語句proxy_pass:設(shè)置代理服務(wù)器的地址,可以是主機名稱、IP地址加端口號等形式。
proxy_pass URL
提示:
1:當(dāng)代理的是一組服務(wù)器時可以使用 upstream 指令來設(shè)置。
2:當(dāng)URL中含有uri時,(例如 "http://127.0.0.1:8080/"、"http://127.0.0.1:8080/demo.html")不管客戶端訪問的是地址中的uri是什么,代理服務(wù)器都會代理到URL的地址;當(dāng)URL中不包含uri時(例如:"http://127.0.0.1:8080"),那么當(dāng)客戶端訪問服務(wù)器時,代理服務(wù)器會根據(jù)客戶端請求的uri來訪問具體的URL地址。
proxy_pass_request_body on|off:用于配置是否將客戶端請求的請求體發(fā)送給代理服務(wù)器。
proxy_pass_request_headers on|off:用于配置是否將客戶端請求的頭信息發(fā)送給代理服務(wù)器。
proxy_set_header field value:可以更改nginx接收到的客戶端請求的請求頭信息,然后將新的請求頭信息發(fā)送給被代理的服務(wù)器。
proxy_set_body value:ngin接收到客戶端的請求后使用該指令可以修改request中的body體,然后將請求轉(zhuǎn)發(fā)給代理服務(wù)器。
proxy_connect_timeout time:nginx服務(wù)器與被代理服務(wù)器之間嘗試建立連接的的超時時間,默認為60s。
proxy_read_timeot time:nginx服務(wù)器接收被代理服務(wù)器數(shù)據(jù)時最大的等待時間,默認為60s。
proxy_send_timeout time:nginx服務(wù)器發(fā)送數(shù)據(jù)至被代理服務(wù)器的最大等待時間,例如60s內(nèi)沒有發(fā)出一個字節(jié)則默認斷開連接,默認60s。
proxy_http_version 1.0|1.1:nginx服務(wù)器提供代理服務(wù)的http協(xié)議版本。
proxy_method method:nginx服務(wù)器設(shè)置請求被代理服務(wù)器時使用的請求方法,一般為POST或者GET。
proxy_ignore_client_abort:當(dāng)客戶端中斷網(wǎng)絡(luò)請求時,nginx服務(wù)是否中斷對代理服務(wù)器的請求,默認off。
五 四層代理配置5.1 四層代理nginx-1.9.0 開始支持 TCP 代理,即4層代理,編譯安裝默認不會支持,需要加上 –with-stream 參數(shù)編譯。
5.2 環(huán)境預(yù)設(shè)1 [root@nginx02 ~]# mkdir /usr/share/nginx/rs/ 2 [root@nginx02 ~]# echo '<h1>Rs172.24.10.22-81</h1>' > /usr/share/nginx/rs/index.html 3 [root@nginx02 ~]# cat > /etc/nginx/conf.d/rs.conf <<EOF 4 server { 5 listen 81; 6 server_name 172.24.10.22; 7 location / { 8 root /usr/share/nginx/rs; 9 index index.html; 10 access_log /var/log/nginx/rs.access.log main; 11 error_log /var/log/nginx/rs.error.log warn; 12 } 13 } 14 EOF 1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@nginx02 ~]# nginx -s reload #重載配置文件 1 [root@nginx03 ~]# mkdir /usr/share/nginx/rs/ 2 [root@nginx03 ~]# echo '<h1>Rs172.24.10.23-81</h1>' > /usr/share/nginx/rs/index.html 3 [root@nginx03 ~]# cat > /etc/nginx/conf.d/rs.conf <<EOF 4 server { 5 listen 81; 6 server_name 172.24.10.23; 7 location / { 8 root /usr/share/nginx/rs; 9 index index.html; 10 access_log /var/log/nginx/rs.access.log main; 11 error_log /var/log/nginx/rs.error.log warn; 12 } 13 } 14 EOF 1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@nginx02 ~]# nginx -s reload #重載配置文件 5.3 配置四層代理1 [root@nginx01 ~]# cat >> /etc/nginx/nginx.conf << EOF 2 stream { 3 upstream myweb { 4 server 172.24.10.22:81 weight=5 max_fails=1 fail_timeout=10s; 5 server 172.24.10.23:81; 6 } 7 upstream myssh { 8 hash $remote_addr consistent; 9 server 172.24.10.24:22; 10 } 11 12 server { 13 listen 8888; 14 proxy_connect_timeout 2s; 15 proxy_timeout 900s; 16 proxy_pass myweb; 17 } 18 19 server { 20 listen 2222; 21 proxy_connect_timeout 2s; 22 proxy_timeout 900s; 23 proxy_pass myssh; 24 } 25 } 26 EOF 提示:stream和http同等級,因此如上配置需要追加至主配置文件最后。
1 [root@proxy ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@proxy ~]# nginx -s reload #重載配置文件 5.4 確認驗證瀏覽器訪問:http://113.31.111.246:8888/。
[root@proxy ~]# ssh -p 2222 172.24.9.247 #測試ssh
提示:更多代理模塊細節(jié),參考《附001.Nginx代理相關(guān)模塊解析》。
六 按類型反向代理配置6.1 環(huán)境預(yù)設(shè)1 [root@nginx02 ~]# mkdir /usr/share/nginx/basebrowser/ 2 [root@nginx02 ~]# echo '<h1>Chrmoe-172.24.10.22</h1>' > /usr/share/nginx/basebrowser/index01.html 3 [root@nginx02 ~]# echo '<h1>Firefox-172.24.10.22</h1>' > /usr/share/nginx/basebrowser/index02.html 1 [root@nginx02 ~]# cat > /etc/nginx/conf.d/basebrowser.conf <<EOF 2 server { 3 listen 88; 4 server_name 172.24.10.22; 5 location / { 6 root /usr/share/nginx/basebrowser; 7 index index01.html; 8 access_log /var/log/nginx/chrmoebrowser.access.log main; 9 error_log /var/log/nginx/chrmoebrowser.error.log warn; 10 } 11 } 12 server { 13 listen 89; 14 server_name 172.24.10.22; 15 location / { 16 root /usr/share/nginx/basebrowser; 17 index index02.html; 18 access_log /var/log/nginx/firefoxbrowser.access.log main; 19 error_log /var/log/nginx/firefoxbrowser.error.log warn; 20 } 21 } 22 EOF 1 [root@nginx03 ~]# mkdir /usr/share/nginx/cellphone/ 2 [root@nginx03 ~]# echo '<h1>iPhone-172.24.10.23</h1>' > /usr/share/nginx/cellphone/index01.html 3 [root@nginx03 ~]# echo '<h1>Android-172.24.10.23</h1>' > /usr/share/nginx/cellphone/index02.html 1 [root@nginx03 ~]# cat > /etc/nginx/conf.d/cellphone.conf <<EOF 2 server { 3 listen 88; 4 server_name 172.24.10.23; 5 location / { 6 root /usr/share/nginx/cellphone; 7 index index01.html; 8 access_log /var/log/nginx/iphone.access.log main; 9 error_log /var/log/nginx/iphone.error.log warn; 10 } 11 } 12 server { 13 listen 89; 14 server_name 172.24.10.23; 15 location / { 16 root /usr/share/nginx/cellphone; 17 index index02.html; 18 access_log /var/log/nginx/android.access.log main; 19 error_log /var/log/nginx/android.error.log warn; 20 } 21 } 22 EOF 1 [root@nginx04 ~]# mkdir /usr/share/nginx/iebrowser/ 2 [root@nginx04 ~]# echo '<h1>IE Browser-172.24.10.24</h1>' > /usr/share/nginx/iebrowser/index.html 1 [root@nginx04 ~]# cat > /etc/nginx/conf.d/iebrowser.conf <<EOF 2 server { 3 listen 88; 4 server_name 172.24.10.24; 5 location / { 6 root /usr/share/nginx/iebrowser; 7 index index.html; 8 access_log /var/log/nginx/iebrowser.access.log main; 9 error_log /var/log/nginx/iebrowser.error.log warn; 10 } 11 } 12 EOF 1 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@nginx02 ~]# nginx -s reload #重載配置文件 3 [root@nginx03 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 4 [root@nginx03 ~]# nginx -s reload #重載配置文件 5 [root@nginx04 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 6 [root@nginx04 ~]# nginx -s reload #重載配置文件 1 [root@client ~]# curl 172.24.10.22:89 2 <h1>Firefox-172.24.10.22</h1> 3 [root@client ~]# 4 [root@client ~]# curl 172.24.10.22:88 5 <h1>Chrmoe-172.24.10.22</h1> 6 [root@client ~]# curl 172.24.10.22:89 7 <h1>Firefox-172.24.10.22</h1> 8 [root@client ~]# curl 172.24.10.23:88 9 <h1>iPhone-172.24.10.23</h1> 10 [root@client ~]# curl 172.24.10.23:89 11 <h1>Android-172.24.10.23</h1> 12 [root@client ~]# curl 172.24.10.24:88 13 <h1>IE Browser-172.24.10.24</h1> 6.2 反向代理配置1 [root@nginx01 ~]# mkdir /usr/share/nginx/type/ 2 [root@nginx01 ~]# echo '<h1>Type-172.24.10.21</h1>' > /usr/share/nginx/type/index.html 1 [root@nginx01 ~]# vi /etc/nginx/conf.d/type.conf 2 upstream chrome { 3 server 172.24.10.22:88; 4 } 5 upstream firefox { 6 server 172.24.10.22:89; 7 } 8 upstream iphone { 9 server 172.24.10.23:88; 10 } 11 upstream android { 12 server 172.24.10.23:89; 13 } 14 upstream iebrowser { 15 server 172.24.10.24:88; 16 } 17 18 server { 19 listen 80; 20 server_name type.; 21 access_log /var/log/nginx/type.access.log main; 22 error_log /var/log/nginx/type.error.log warn; 23 proxy_set_header Host $host; 24 proxy_set_header X-Real-IP $remote_addr; 25 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 26 client_max_body_size 10m; #允許客戶端請求的最大單文件字節(jié)數(shù) 27 client_body_buffer_size 128k; #緩沖區(qū)代理緩沖用戶端請求的最大字節(jié)數(shù) 28 proxy_connect_timeout 300; #nginx跟后端服務(wù)器連接超時時間(代理連接超時) 29 proxy_send_timeout 300; #后端服務(wù)器數(shù)據(jù)回傳時間(代理發(fā)送超時) 30 proxy_read_timeout 300; #連接成功后,后端服務(wù)器響應(yīng)時間(代理接收超時) 31 proxy_buffer_size 4k; #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小 32 proxy_buffers 4 32k; #proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的話,這樣設(shè)置 33 proxy_busy_buffers_size 64k; #高負荷下緩沖大?。╬roxy_buffers*2) 34 proxy_temp_file_write_size 64k; #設(shè)定緩存文件夾大小,大于這個值,將從upstream服務(wù)器傳 35 location / { 36 root /usr/share/nginx/type/; 37 index index.html; 38 39 if ($http_user_agent ~* "chrome"){ 40 proxy_pass http://chrome; 41 } 42 if ($http_user_agent ~* "firefox"){ 43 proxy_pass http://firefox; 44 } 45 if ($http_user_agent ~* "android"){ 46 proxy_pass http://android; 47 } 48 if ($http_user_agent ~* "iphone"){ 49 proxy_pass http://iphone; 50 } 51 if ($http_user_agent ~* "MSIE"){ 52 proxy_pass http://iebrowser; 53 } 54 } 55 } 1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@nginx01 ~]# nginx -s reload #重載配置文件 6.3 確認驗證不同瀏覽器分別訪問:http://type./
七 反向代理及緩存7.1 環(huán)境預(yù)設(shè)1 [root@nginx02 ~]# mkdir /usr/share/nginx/cache/ 2 [root@nginx02 ~]# echo '<h1>Cache-172.24.10.22</h1>' > /usr/share/nginx/cache/index.html 3 1 [root@nginx02 ~]# cat > /etc/nginx/conf.d/cache.conf <<EOF 2 server { 3 listen 90; 4 server_name 172.24.10.22; 5 location / { 6 root /usr/share/nginx/cache; 7 index index.html; 8 access_log /var/log/nginx/cache.access.log main; 9 error_log /var/log/nginx/cache.error.log warn; 10 } 11 } 12 EOF 1 [root@nginx02 ~]# ll /usr/share/nginx/cache 2 total 16K 3 -rw-r--r-- 1 root root 28 Jun 23 22:33 index.html 4 -rw-r--r-- 1 root root 11K Jun 23 22:34 nginx.jpg #上傳一張測試圖片 5 [root@nginx02 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 6 [root@nginx02 ~]# nginx -s reload #重載配置文件 1 [root@nginx03 ~]# mkdir /usr/share/nginx/cache/ 2 [root@nginx03 ~]# echo '<h1>Cache-172.24.10.23</h1>' > /usr/share/nginx/cache/index.html 1 [root@nginx03 ~]# cat > /etc/nginx/conf.d/cache.conf <<EOF 2 server { 3 listen 90; 4 server_name 172.24.10.23; 5 location / { 6 root /usr/share/nginx/cache; 7 index index.html; 8 access_log /var/log/nginx/cache.access.log main; 9 error_log /var/log/nginx/cache.error.log warn; 10 } 11 } 12 EOF 1 [root@nginx03 ~]# ll /usr/share/nginx/cache 2 total 16K 3 -rw-r--r-- 1 root root 28 Jun 23 22:33 index.html 4 -rw-r--r-- 1 root root 11K Jun 23 22:34 nginx.jpg #上傳一張測試圖片 5 [root@nginx03 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 6 [root@nginx03 ~]# nginx -s reload #重載配置文件 7.2 配置代理及緩存1 [root@nginx01 ~]# mkdir -p /data/cache #創(chuàng)建緩存目錄 2 [root@nginx01 ~]# mkdir -p /data/cache_temp #創(chuàng)建緩存臨時目錄 1 [root@nginx01 ~]# vi /etc/nginx/nginx.conf #追加如下代碼日志記錄 2 …… 3 log_format main '[$remote_addr]-[$remote_user]-[$time_local]-["$request"]' 4 '[$status]-[$body_bytes_sent]-["$http_referer"]' 5 '["$http_user_agent"]-["$http_x_forwarded_for"]'; 6 '[$upstream_addr]-[$upstream_status]-[$request_time]-[$upstream_response_time]' 7 …… 1 [root@nginx01 ~]# vi /etc/nginx/conf.d/cache.conf 2 upstream cache { 3 server 172.24.10.22:90; 4 server 172.24.10.23:90; 5 } 6 7 proxy_temp_path /data/cache_temp; 8 proxy_cache_path /data/cache levels=1:2 keys_zone=mycache:20m max_size=10g inactive=60m use_temp_path=off; 9 10 server { 11 listen 80; 12 server_name cache.; 13 access_log /var/log/nginx/cache.access.log main; 14 error_log /var/log/nginx/cache.error.log warn; 15 16 location / { 17 expires 3d; 18 add_header Nginx-Cache "$upstream_cache_status"; #增加一個頭信息 19 20 proxy_cache mycache; #調(diào)用定義的cache zone 21 proxy_pass http://cache; #配置反向代理 22 proxy_set_header Host $host; 23 proxy_set_header X-Real-IP $remote_addr; 24 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 25 26 proxy_cache_valid 200 302 304 2h; #200和302及304頭信息過期時間為2小時 27 proxy_cache_valid any 10m; #其他過期時間10分鐘 28 proxy_cache_key $host$request_uri$uri$is_args$args; #定義緩存的key 29 proxy_cache_bypass $cookie_nocache $arg_comment; #配置不緩存 30 proxy_no_cache $arg_nocache; #配置不緩存 31 proxy_cache_lock on; 32 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; #一個服務(wù)報錯請求下一個 33 } 34 location ~ /purge(/.*) { 35 allow 127.0.0.1; 36 allow 172.24.10.0/24; 37 deny all; 38 proxy_cache_purge mycache $1$is_args$args; 39 } 40 } 1 [root@nginx01 ~]# nginx -t -c /etc/nginx/nginx.conf #檢查配置文件 2 [root@nginx01 ~]# nginx -s reload #重載配置文件 配置釋義:
proxy_cache_path:配置緩存目錄,文件目錄層級2級,空間名字20m大小,目錄最大大小10g(超過啟動nginx自己的淘汰規(guī)則),在60分鐘的時間內(nèi)沒有被訪問就會被清理,并且不使用臨時目錄。
proxy_no_cache:部分不設(shè)置緩存,cookie_nocache上面配置的參數(shù),cookie_nocache不為0或者空,那么是不會進行緩存的。
proxy_cache_lock on:如果多個客戶端請求的文件不在緩存(MISS),只有第一個這些請求是通過原始服務(wù)器的。
提示:proxy_no_cache和proxy_cache_bypass類似,其差異是proxy_no_cache用于控制什么情況下響應(yīng)不被緩存。比如配置“proxy_no_cache$args_nocache”,如果帶的nocache參數(shù)值至少有一個不為空或者為0,則響應(yīng)將不被緩存。而proxy_cache_bypass,控制什么情況不使用緩存的內(nèi)容,而是直接到后端獲取最新的內(nèi)容。如果命中,則$upstream_cache_status為BYPASS。
延伸:
upstream_cache_status狀態(tài)如下:
注意:使用proxy_cache_purge清除緩存,必須提前安裝ngx_cache_purge模塊,安裝模塊必須基于編譯安裝的Nginx。
7.3 確認驗證客戶端訪問測試:
1 [root@client ~]# curl -I http://cache./nginx.jpg 結(jié)論:第一次訪問會出現(xiàn)MISS,沒有命中緩存,第二次訪問即可直接命中緩存。
參考文檔:https://blog.csdn.net/h13140995776/article/details/101174283。
|
|