上海網(wǎng)絡(luò)企業(yè)優(yōu)化公司seo是什么職業(yè)
- nginx反向代理
- 8.反向代理
- 8.1 實現(xiàn)http反向代理
- 8.1.1 反向代理配置參數(shù)
- 8.1.2 反向代理單臺web服務(wù)器
- 8.1.2.1 端口號后加"/"
- 8.1.2.2 端口號后不加"/"
- 8.1.3指定location 實現(xiàn)反向代理,動靜分離
- 8.1.4 反向代理實例:緩存功能
- 8.1.4.1 舉例
- 8.1.5 實現(xiàn)反向代理客戶端 IP 透傳
- 8.1.5.1 一級代理
- 8.1.5.2 多級代理ip地址透傳
nginx反向代理
8.反向代理
8.1 實現(xiàn)http反向代理
官方文檔: https://nginx.org/en/docs/http/ngx_http_proxy_module.html
8.1.1 反向代理配置參數(shù)
#官方文檔:https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
proxy_pass;
#用來設(shè)置將客戶端請求轉(zhuǎn)發(fā)給的后端服務(wù)器的主機,可以是主機名(將轉(zhuǎn)發(fā)至后端服務(wù)做為主機頭首部)、IP
地址:端口的方式
#也可以代理到預(yù)先設(shè)置的主機群組,需要模塊ngx_http_upstream_module支持#示例:location /web {index index.html;proxy_pass http://10.0.0.18:8080; #8080后面無uri,即無 / 符號,需要將location后面url 附加到proxy_pass指定的url后面,此行為類似于root
#proxy_pass指定的uri不帶斜線將訪問的/web,等于訪問后端服務(wù)器
http://10.0.0.18:8080/web/index.html,即后端服務(wù)器配置的站點根目錄要有web目錄才可以被訪問# http://nginx/web/index.html ==> http://10.0.0.18:8080/web/index.htmlproxy_pass http://10.0.0.18:8080/; #8080后面有uri,即有 / 符號,相當(dāng)于置換,即訪問/web時實際返回proxy_pass后面uri內(nèi)容.此行為類似于alias #proxy_pass指定的uri帶斜線,等于訪問后端服務(wù)器的http://10.0.0.18:8080/index.html 內(nèi)容返回給客戶端} # http://nginx/web/index.html ==> http://10.0.0.18:8080#重啟Nginx測試訪問效果:
#curl -L http://www.kgc.org/web#如果location定義其uri時使用了正則表達式模式(包括~,~*,但不包括^~),則proxy_pass之后必須不能使用uri; 即不能有/ ,用戶請求時傳遞的uri將直接附加至后端服務(wù)器之后
server {...server_name HOSTNAME;location ~|~* /uri/ {proxy_pass http://host:port; #proxy_pass后面的url 不能加/}...}http://HOSTNAME/uri/ --> http://host/uri/proxy_hide_header field;
#用于nginx作為反向代理的時候,在返回給客戶端http響應(yīng)時,隱藏后端服務(wù)器相應(yīng)頭部的信息,可以設(shè)置
在http,server或location塊
#示例: 隱藏后端服務(wù)器ETag首部字段location /web {index index.html;proxy_pass http://10.0.0.18:8080/; proxy_hide_header ETag;}proxy_pass_header field;
#默認nginx在響應(yīng)報文中不傳遞后端服務(wù)器的首部字段Date, Server, X-Pad, X-Accel等參數(shù),如果
要傳遞的話則要使用 proxy_pass_header field聲明將后端服務(wù)器返回的值傳遞給客戶端
#field 首部字段大小不敏感
#示例:透傳后端服務(wù)器的Server和Date首部給客戶端,同時不再響應(yīng)報中顯示前端服務(wù)器的Server字段
proxy_pass_header Server;
proxy_pass_header Date;proxy_pass_request_body on | off;
#是否向后端服務(wù)器發(fā)送HTTP實體部分,可以設(shè)置在http,server或location塊,默認即為開啟proxy_pass_request_headers on | off;
#是否將客戶端的請求頭部轉(zhuǎn)發(fā)給后端服務(wù)器,可以設(shè)置在http,server或location塊,默認即為開啟
8.1.2 反向代理單臺web服務(wù)器
要求:將用戶對域 www.pc.com的請求轉(zhuǎn)發(fā)給后端服務(wù)器處理
7-1
vim /apps/nginx/conf.d/pc.conf
location / {proxy_pass http://192.168.210.102;
}
#添加此項,使訪問7-1可以跳轉(zhuǎn)到7-2nginx -s reload
7-3
curl 192.168.210.101
端口號問題,端口號轉(zhuǎn)的時候可以一起轉(zhuǎn)
7-1
vim /apps/nginx/conf.d/pc.conf
location / {proxy_pass http://192.168.210.102:9527;
}
#添加端口號nginx -s reload
7-2
vim /apps/nginx/conf/nginx.conf
server {listen 80;listen 9527;#添加端口號9527
}nginx -s reload
ss -lntp | grep nginx
7-3
curl 192.168.210.101
8.1.2.1 端口號后加"/"
7-1
vim /apps/nginx/conf.d/pc.conf
location /web {proxy_pass http://192.168.210.102:9527;
}
#不加/,相當(dāng)于把/web追加到9527的后面,訪問的時候會報錯,因為7-2下沒有web文件夾nginx -s reload
7-2
cd /apps/nginx/html
mkdir web
#創(chuàng)建web文件夾
echo web > web/index.html
7-3
curl 192.168.210.101/web -L
8.1.2.2 端口號后不加"/"
7-1
vim /apps/nginx/conf.d/pc.conf
location /web {proxy_pass http://192.168.210.102:9527/;
}
#加/,代表將location后面的url置換到后面
nginx -s reload
7-3
curl 192.168.210.101/web -L
8.1.3指定location 實現(xiàn)反向代理,動靜分離
7-3(192.168.210.103)客戶端
7-1(192.168.210.101)反向代理
7-4(192.168.210.104)動態(tài)頁面
7-2(192.168.210.102)靜態(tài)頁面
7-4
systemctl stop firewalld
setenforce 0
#關(guān)閉防火墻
yum install httpd -y
#安裝apache
cd /var/www/html
echo httpd > index.html
#制作頁面
ls
cat index.html
systemctl start httpd
#開啟服務(wù)
7-1
vim /apps/nginx/conf.d/pc.conf
location /static {proxy_pass http://192.168.210.102:9527;
}
location /api {proxy_pass http://192.168.210.104;
}nginx -s reload
7-4
cd /var/www/html
mkdir api
#創(chuàng)建文件夾api
echo api >> api/index.html
echo api >> api/index.html
echo api >> api/index.html
#制作動態(tài)頁面
cat api/index.html
7-2
cd /apps/nginx/html
mkdir static
#創(chuàng)建static文件夾
echo static >> static/index.html
echo static >> static/index.html
echo static >> static/index.html
#制作靜態(tài)頁面
cat static/index.html
7-3
curl 192.168.210.101/api -L
curl 192.168.210.101/static -L
#實現(xiàn)動靜分離
8.1.4 反向代理實例:緩存功能
proxy_cache zone_name | off; 默認off
#指明調(diào)用的緩存,或關(guān)閉緩存機制;Context:http, server, location
#zone_name 表示緩存的名稱.需要由proxy_cache_path事先定義proxy_cache_key string;
#緩存中用于“鍵”的內(nèi)容,默認值:proxy_cache_key $scheme$proxy_host$request_uri;proxy_cache_valid [code ...] time;
#定義對特定響應(yīng)碼的響應(yīng)內(nèi)容的緩存時長,定義在http{...}中示例:proxy_cache_valid 200 302 10m;proxy_cache_valid 404 1m;proxy_cache_path;
#定義可用于proxy功能的緩存;Context:http
proxy_cache_path path [levels=levels] [use_temp_path=on|off]
keys_zone=zone_name:size [inactive=time] [max_size=size] [manager_files=number]
[manager_sleep=time] [manager_threshold=time] [loader_files=number]
[loader_sleep=time] [loader_threshold=time] [purger=on|off]
[purger_files=number] [purger_sleep=time] [purger_threshold=time];#示例:在http配置定義緩存信息proxy_cache_path /var/cache/nginx/proxy_cache #定義緩存保存路徑,proxy_cache會自動創(chuàng)建levels=1:2:2 #定義緩存目錄結(jié)構(gòu)層次,1:2:2可以生成2^4x2^8x2^8=2^20=1048576個目錄keys_zone=proxycache:20m #指內(nèi)存中緩存的大小,主要用于存放key和metadata(如:使用次數(shù)),一般1M可存放8000個左右的keyinactive=120s #緩存有效時間 max_size=10g; #最大磁盤占用空間,磁盤存入文件內(nèi)容的緩存空間最大值#調(diào)用緩存功能,需要定義在相應(yīng)的配置段,如server{...};或者location等
proxy_cache proxycache;
proxy_cache_key $request_uri; #對指定的數(shù)據(jù)進行MD5的運算做為緩存的key
proxy_cache_valid 200 302 301 10m; #指定的狀態(tài)碼返回的數(shù)據(jù)緩存多長時間
proxy_cache_valid any 1m; #除指定的狀態(tài)碼返回的數(shù)據(jù)以外的緩存多長時間,必須設(shè)置,否則不會緩存proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 |
http_502 | http_503 | http_504 | http_403 | http_404 | off ; #默認是off
#在被代理的后端服務(wù)器出現(xiàn)哪種情況下,可直接使用過期的緩存響應(yīng)客戶端
#示例
proxy_cache_use_stale error http_502 http_503;proxy_cache_methods GET | HEAD | POST ...;
#對哪些客戶端請求方法對應(yīng)的響應(yīng)進行緩存,GET和HEAD方法總是被緩存
8.1.4.1 舉例
7-1
vim /apps/nginx/conf/nginx.confproxy_cache_path /data/nginx/proyxcache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
#加在include前mkdir /data/nginx
#創(chuàng)建nginx文件夾
vim /apps/nginx/conf.d/pc.confserver{listen 80;proxy_cache proxycache;proxy_cache_key $request_uri;proxy_cache_valid 200 302 301 10m;proxy_cache_valid any 5m;} nginx -tnginx -s reload
ls /data/nginx#看是否生成proyxcache文件
在瀏覽器訪問192.168.210.101
關(guān)閉web服務(wù)器7-2,再次在瀏覽器訪問192.168.210.101
還能出來界面,說明有緩存
8.1.5 實現(xiàn)反向代理客戶端 IP 透傳
8.1.5.1 一級代理
7-1(192.168.210.101)代理服務(wù)器
7-2(192.168.210.102)web服務(wù)器
7-3(192.168.210.103)客戶端
7-2
yum install httpd -y
#安裝apache
cd /var/www/html
echo real server > index.html
#制作頁面
cat index.html
systemctl start httpd
ss -lntp |grep 80
#開啟服務(wù)
7-1
vim /apps/nginx/conf.d/pc.conf
#刪掉這個location模塊
location /api {proxy_pass http://192.168.210.104;
}
nginx -t
nginx -s reload
7-3
curl 192.168.210.101
7-2
tail -f /var/log/httpd/access_log
#只能看到192.168.210.101的日志,看不到192.168.210.103的
7-1
tail -f /apps/nginx/logs/access.log
#192.168.210.101可以看到192.168.210.103的日志
7-1
vim /apps/nginx/conf.d/pc.confproxy_set_header test "$remote_addr"; #往日志里面加了test,內(nèi)容是$remote_addr#$remote_addr只能記一次,如果中間是三層代理,它就無法去實現(xiàn)了
nginx -t
nginx -s reload
7-2
vim /etc/httpd/conf/httpd.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{test}i\"" combinedsystemctl restart httpd
7-3
curl 192.168.210.1017-2
tail -f /var/log/httpd/access_log
#192.168.210.103的地址就過來了
8.1.5.2 多級代理ip地址透傳
7-1
vim /apps/nginx/conf/nginx.conf
#把日志全都打開
7-4
yum install -y epel-release
yum install nginx -y
7-1
scp /apps/nginx/conf.d/pc.conf 192.168.210.104:/etc/nginx
7-4
cd /etc/nginx
vim pc.conf
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#添加
nginx -t
systemctl start nginx
7-1
vim /apps/nginx/conf.d/pc.conf
systemctl restart nginx
7-2
vim /etc/httpd/conf/httpd.conf
LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{X-Forwarded-For}i\"" combinedsystemctl restart httpd
7-3
curl 192.168.210.1017-1
tail -f /apps/nginx/logs/access.log
7-2
tail -f /var/log/httpd/access_log