nginx中的兩個模塊的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",
- 還有一個可選的uri可以配置。
3.proxy_pass后,后端服務(wù)器的url(request_uri)情況分析
server {
listen 80;
server_name www.test.com;
# 情形A
# 訪問 http://www.test.com/testa/aaaa
# 后端的request_uri為: /testa/aaaa
location ^~ /testa/ {
proxy_pass http://127.0.0.1:8801;
}
# 情形B
# 訪問 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是錯誤的
#
# nginx -t 時,會報如下錯誤:
#
# 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á)式時,proxy_pass 不能包含URI部分。本例中包含了"/"
location ~ /testd {
proxy_pass http://127.0.0.1:8801/; # 記住,location為正則表達(dá)式時,不能這樣寫?。?!
}
# 情形E
# 訪問 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
# 訪問 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
# 訪問 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中的兩個模塊的proxy_pass的區(qū)別的文章就介紹到這了,更多相關(guān)nginx proxy_pass模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
prometheus監(jiān)控nginx的實(shí)現(xiàn)
這篇文章主要介紹了prometheus監(jiān)控nginx的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
PHP(FastCGI)在Nginx的alias下出現(xiàn)404錯誤的解決方法
這篇文章主要介紹了PHP(FastCGI)在Nginx的alias下出現(xiàn)404錯誤的解決方法,涉及nginx平臺的相關(guān)配置技巧,需要的朋友可以參考下2016-05-05
Nginx配置檢測服務(wù)狀態(tài)的實(shí)現(xiàn)方法
這篇文章主要介紹了Nginx配置檢測服務(wù)狀態(tài)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05

