ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

nginx中的两个模块的proxy_pass的区别

2021-11-29 11:34:15  阅读:141  来源: 互联网

标签:www http nginx location pass test proxy


  1. ngx_stream_proxy_module模块的proxy_pass指令

    • 只能在server段使用使用, 只需要提供域名或ip地址和端口。

    • 可以理解为端口转发,可以是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地址和端口外,还需要提供协议,如"http"或"https",
    • 还有一个可选的uri可以配置。
  3. proxy_pass后,后端服务器的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
          # 
          # 当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/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;
      }
      
      

标签:www,http,nginx,location,pass,test,proxy
来源: https://www.cnblogs.com/qumogu/p/15618533.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有