nginx中的兩個(gè)模塊的proxy_pass的區(qū)別解析
1.ngx_stream_proxy_module模塊的proxy_pass指令
只能在server段使用使用, 只需要提供域名或ip地址和端口。
可以理解為端口轉(zhuǎn)發(fā),可以是tcp端口,也可以是udp端口。
server {
listen 127.0.0.1:12345;
proxy_pass 127.0.0.1:8080;
}
server {
listen 12345;
proxy_connect_timeout 1s;
proxy_timeout 1m;
proxy_pass example.com:12345;
}
server {
listen 53 udp;
proxy_responses 1;
proxy_timeout 20s;
proxy_pass dns.example.com:53;
}
server {
listen [::1]:12345;
proxy_pass unix:/tmp/stream.socket;
2.ngx_http_proxy_module模塊的proxy_pass指令
- 需要在location段,location中的if段,limit_except段中使用,
- 處理需要提供域名或ip地址和端口外,還需要提供協(xié)議,如"http"或"https",
- 還有一個(gè)可選的uri可以配置。
3.proxy_pass后,后端服務(wù)器的url(request_uri)情況分析
server {
listen 80;
server_name www.test.com;
# 情形A
# 訪問(wèn) http://www.test.com/testa/aaaa
# 后端的request_uri為: /testa/aaaa
location ^~ /testa/ {
proxy_pass http://127.0.0.1:8801;
}
# 情形B
# 訪問(wèn) http://www.test.com/testb/bbbb
# 后端的request_uri為: /bbbb
location ^~ /testb/ {
proxy_pass http://127.0.0.1:8801/;
}
# 情形C
# 下面這段location是正確的
location ~ /testc {
proxy_pass http://127.0.0.1:8801;
}
# 情形D
# 下面這段location是錯(cuò)誤的
#
# nginx -t 時(shí),會(huì)報(bào)如下錯(cuò)誤:
#
# nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular
# expression, or inside named location, or inside "if" statement, or inside
# "limit_except" block in /opt/app/nginx/conf/vhost/test.conf:17
#
# 當(dāng)location為正則表達(dá)式時(shí),proxy_pass 不能包含URI部分。本例中包含了"/"
location ~ /testd {
proxy_pass http://127.0.0.1:8801/; # 記住,location為正則表達(dá)式時(shí),不能這樣寫?。?!
}
# 情形E
# 訪問(wèn) http://www.test.com/ccc/bbbb
# 后端的request_uri為: /aaa/ccc/bbbb
location /ccc/ {
proxy_pass http://127.0.0.1:8801/aaa$request_uri;
}
# 情形F
# 訪問(wèn) http://www.test.com/namea/ddd
# 后端的request_uri為: /yongfu?namea=ddd
location /namea/ {
rewrite /namea/([^/]+) /yongfu?namea=$1 break;
proxy_pass http://127.0.0.1:8801;
}
# 情形G
# 訪問(wèn) http://www.test.com/nameb/eee
# 后端的request_uri為: /yongfu?nameb=eee
location /nameb/ {
rewrite /nameb/([^/]+) /yongfu?nameb=$1 break;
proxy_pass http://127.0.0.1:8801/;
}
access_log /data/logs/www/www.test.com.log;
}
server {
listen 8801;
server_name www.test.com;
root /data/www/test;
index index.php index.html;
rewrite ^(.*)$ /test.php?u=$1 last;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
access_log /data/logs/www/www.test.com.8801.log;
}
到此這篇關(guān)于nginx中的兩個(gè)模塊的proxy_pass的區(qū)別的文章就介紹到這了,更多相關(guān)nginx proxy_pass模塊內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信