proxy_pass
指令用于指定后端服務器的地址,可以采用以下不同的格式:
- 直接指定地址和端口:
location / {proxy_pass http://backend_server:8080;
}
這將請求代理到 http://backend_server:8080
。
-
使用變量:
location / {set $backend_server http://backend.example.com;proxy_pass $backend_server; }
這里通過使用
set
指令將后端服務器的地址存儲在變量$backend_server
中,并將其傳遞給proxy_pass
。 -
根據請求路徑進行代理:
location /app1/ {proxy_pass http://backend_server1; }location /app2/ {proxy_pass http://backend_server2; }
這將根據請求的路徑不同將請求代理到不同的后端服務器。
-
使用域名進行代理:
location / {proxy_pass http://backend.example.com; }
將請求代理到指定域名的后端服務器。
-
使用 UNIX 套接字:
nginxCopy code location / {proxy_pass unix:/path/to/backend.sock; }
如果后端服務器是通過 UNIX 套接字提供服務,可以使用
unix:
指定套接字的路徑。
無論使用哪種形式,確保 proxy_pass
后面的地址是有效的,并且 Nginx 服務器能夠訪問該地址。在配置時,還要注意是否需要設置其他選項,如 proxy_set_header
用于修改請求頭。
1、Nginx中有兩個模塊都有proxy_pass
指令。
ngx_http_proxy_module
的proxy_pass
(常用這種,以下栗子大部分是這種)和ngx_stream_proxy_module
的proxy_pass
2、二者區別
在兩個模塊中,兩個proxy_pass
都是用來做后端代理的指令。
ngx_stream_proxy_module
模塊的proxy_pass
指令只能在server段使用使用, 只需要提供域名或ip地址和端口。可以理解為端口轉發,可以是tcp端口,也可以是udp端口。ngx_http_proxy_module
模塊的proxy_pass
指令需要在location段,location中的if段,limit_except段中使用,處理需要提供域名或ip地址和端口外,還需要提供協議,如"http"或"https",還有一個可選的uri可以配置。
3、proxy_pass的具體用法
ngx_stream_proxy_module
模塊的proxy_pass
指令
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;
}
ngx_http_proxy_module
模塊的proxy_pass
指令
server {listen 80;server_name www.test.com;# 正常代理,不修改后端url的location /some/path/ {proxy_pass http://127.0.0.1;}# 修改后端url地址的代理(本例后端地址中,最后帶了一個斜線)location /testb {proxy_pass http://www.other.com:8801/;}# 使用 if in locationlocation /google {if ( $geoip_country_code ~ (RU|CN) ) {proxy_pass http://www.google.hk;}}location /yongfu/ {# 沒有匹配 limit_except 的,代理到 unix:/tmp/backend.socket:/uri/proxy_pass http://unix:/tmp/backend.socket:/uri/;;# 匹配到請求方法為: PUT or DELETE, 代理到9080limit_except PUT DELETE {proxy_pass http://127.0.0.1:9080;}}}
4、舉個例子
假設下面四種情況分別用 http://192.168.1.1/proxy/test.html 進行訪問。
第一種:
location /proxy/ {proxy_pass http://127.0.0.1/;
}
代理到URL:http://127.0.0.1/test.html
第二種(相對于第一種,最后少一個 / )
location /proxy/ {proxy_pass http://127.0.0.1;
}
代理到URL:http://127.0.0.1/proxy/test.html
第三種:
location /proxy/ {proxy_pass http://127.0.0.1/aaa/;
}
代理到URL:http://127.0.0.1/aaa/test.html
第四種(相對于第三種,最后少一個 / )
location /proxy/ {proxy_pass http://127.0.0.1/aaa;
}
代理到URL:http://127.0.0.1/aaatest.html
server{listen 80;server_name www.test.com;# 情形A# 訪問 http://www.test.com/testa/aaaa# 后端的request_uri為: /testa/aaaalocation ^~ /testa/ {proxy_pass http://127.0.0.1:8801;}# 情形B# 訪問 http://www.test.com/testb/bbbb# 后端的request_uri為: /bbbblocation ^~ /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## 當location為正則表達式時,proxy_pass 不能包含URI部分。本例中包含了"/"location ~ /testd {proxy_pass http://127.0.0.1:8801/; # 記住,location為正則表達式時,不能這樣寫!!!}# 情形E# 訪問 http://www.test.com/ccc/bbbb# 后端的request_uri為: /aaa/ccc/bbbblocation /ccc/ {proxy_pass http://127.0.0.1:8801/aaa$request_uri;}# 情形F# 訪問 http://www.test.com/namea/ddd# 后端的request_uri為: /yongfu?namea=dddlocation /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=eeelocation /nameb/ {rewrite /nameb/([^/]+) /yongfu?nameb=$1 break;proxy_pass http://127.0.0.1:8801/}
}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;
}
文件: /data/www/test/test.php
<?php
echo '$_SERVER[REQUEST_URI]:' . $_SERVER['REQUEST_URI'];
通過查看 $_SERVER[‘REQUEST_URI’] 的值,我們可以看到每次請求的后端的request_uri的值,進行驗證。
5、參考
- http://nginx.org/en/docs/http/ngx_http_proxy_module.html
- http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html