nginx 之 proxy_pass详解

在nginx中配置proxy_pass代理转发时,如果在proxy_pass后面的url加/,表示绝对根路径;如果没有/,表示相对路径,把匹配的路径部分也给代理走。

假设下面四种情况分别用 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

nginx中有两个模块都有proxy_pass指令。

  • ngx_http_proxy_moduleproxy_pass
语法: proxy_pass URL;场景: location, if in location, limit_except说明: 设置后端代理服务器的协议(protocol)和地址(address),以及location中可以匹配的一个可选的URI。协议可以是"http"或"https"。地址可以是一个域名或ip地址和端口,或者一个 unix-domain socket 路径。  详见官方文档: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_passURI的匹配,本文第四部分重点讨论。
  • ngx_stream_proxy_moduleproxy_pass
语法: proxy_pass address;场景: server说明: 设置后端代理服务器的地址。这个地址(address)可以是一个域名或ip地址和端口,或者一个 unix-domain socket路径。  详见官方文档: http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_pass

二、两个proxy_pass的关系和区别

在两个模块中,两个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可以配置。

三、proxy_pass的具体用法

ngx_stream_proxy_module模块的proxy_pass指令

 
  1. server {

  2. listen 127.0.0.1:12345;

  3. proxy_pass 127.0.0.1:8080;

  4. }

  5.  
  6. server {

  7. listen 12345;

  8. proxy_connect_timeout 1s;

  9. proxy_timeout 1m;

  10. proxy_pass example.com:12345;

  11. }

  12.  
  13. server {

  14. listen 53 udp;

  15. proxy_responses 1;

  16. proxy_timeout 20s;

  17. proxy_pass dns.example.com:53;

  18. }

  19.  
  20. server {

  21. listen [::1]:12345;

  22. proxy_pass unix:/tmp/stream.socket;

  23. }

ngx_http_proxy_module模块的proxy_pass指令

 
  1. server {

  2. listen 80;

  3. server_name www.test.com;

  4.  
  5. # 正常代理,不修改后端url的

  6. location /some/path/ {

  7. proxy_pass http://127.0.0.1;

  8. }

  9.  
  10. # 修改后端url地址的代理(本例后端地址中,最后带了一个斜线)

  11. location /testb {

  12. proxy_pass http://www.other.com:8801/;

  13. }

  14.  
  15. # 使用 if in location

  16. location /google {

  17. if ( $geoip_country_code ~ (RU|CN) ) {

  18. proxy_pass http://www.google.hk;

  19. }

  20. }

  21.  
  22. location /yongfu/ {

  23. # 没有匹配 limit_except 的,代理到 unix:/tmp/backend.socket:/uri/

  24. proxy_pass http://unix:/tmp/backend.socket:/uri/;;

  25.  
  26. # 匹配到请求方法为: PUT or DELETE, 代理到9080

  27. limit_except PUT DELETE {

  28. proxy_pass http://127.0.0.1:9080;

  29. }

  30. }

  31.  
  32. }

四、proxy_pass后,后端服务器的url(request_uri)情况分析

 
  1. server {

  2. listen 80;

  3. server_name www.test.com;

  4.  
  5. # 情形A

  6. # 访问 http://www.test.com/testa/aaaa

  7. # 后端的request_uri为: /testa/aaaa

  8. location ^~ /testa/ {

  9. proxy_pass http://127.0.0.1:8801;

  10. }

  11.  
  12. # 情形B

  13. # 访问 http://www.test.com/testb/bbbb

  14. # 后端的request_uri为: /bbbb

  15. location ^~ /testb/ {

  16. proxy_pass http://127.0.0.1:8801/;

  17. }

  18.  
  19. # 情形C

  20. # 下面这段location是正确的

  21. location ~ /testc {

  22. proxy_pass http://127.0.0.1:8801;

  23. }

  24.  
  25. # 情形D

  26. # 下面这段location是错误的

  27. #

  28. # nginx -t 时,会报如下错误:

  29. #

  30. # nginx: [emerg] "proxy_pass" cannot have URI part in location given by regular

  31. # expression, or inside named location, or inside "if" statement, or inside

  32. # "limit_except" block in /opt/app/nginx/conf/vhost/test.conf:17

  33. #

  34. # 当location为正则表达式时,proxy_pass 不能包含URI部分。本例中包含了"/"

  35. location ~ /testd {

  36. proxy_pass http://127.0.0.1:8801/; # 记住,location为正则表达式时,不能这样写!!!

  37. }

  38.  
  39. # 情形E

  40. # 访问 http://www.test.com/ccc/bbbb

  41. # 后端的request_uri为: /aaa/ccc/bbbb

  42. location /ccc/ {

  43. proxy_pass http://127.0.0.1:8801/aaa$request_uri;

  44. }

  45.  
  46. # 情形F

  47. # 访问 http://www.test.com/namea/ddd

  48. # 后端的request_uri为: /yongfu?namea=ddd

  49. location /namea/ {

  50. rewrite /namea/([^/]+) /yongfu?namea=$1 break;

  51. proxy_pass http://127.0.0.1:8801;

  52. }

  53.  
  54. # 情形G

  55. # 访问 http://www.test.com/nameb/eee

  56. # 后端的request_uri为: /yongfu?nameb=eee

  57. location /nameb/ {

  58. rewrite /nameb/([^/]+) /yongfu?nameb=$1 break;

  59. proxy_pass http://127.0.0.1:8801/;

  60. }

  61.  
  62. access_log /data/logs/www/www.test.com.log;

  63. }

  64.  
  65. server {

  66. listen 8801;

  67. server_name www.test.com;

  68.  
  69. root /data/www/test;

  70. index index.php index.html;

  71.  
  72. rewrite ^(.*)$ /test.php?u=$1 last;

  73.  
  74. location ~ \.php$ {

  75. try_files $uri =404;

  76. fastcgi_pass unix:/tmp/php-cgi.sock;

  77. fastcgi_index index.php;

  78. include fastcgi.conf;

  79. }

  80.  
  81. access_log /data/logs/www/www.test.com.8801.log;

  82. }

  83.  
  84.  

文件: /data/www/test/test.php

 
  1. <?php

  2. echo '$_SERVER[REQUEST_URI]:' . $_SERVER['REQUEST_URI'];

通过查看 $_SERVER['REQUEST_URI'] 的值,我们可以看到每次请求的后端的request_uri的值,进行验证。

小结

情形A和情形B进行对比,可以知道proxy_pass后带一个URI,可以是斜杠(/)也可以是其他uri,对后端request_uri变量的影响。
情形D说明,当location为正则表达式时,proxy_pass不能包含URI部分。
情形E通过变量($request_uri, 也可以是其他变量),对后端的request_uri进行改写。
情形F和情形G通过rewrite配合break标志,对url进行改写,并改写后端的request_uri。需要注意,proxy_pass地址的URI部分在情形G中无效,不管如何设置,都会被忽略。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值