Nginx中location中关于proxy_pass和rewrite的应用

1,问题描述

 upstream datacollectbackend{
      #ip_hash;
      server 10.234.1.211:6100 max_fails=5 fail_timeout=30s;
      server 10.234.1.26:7100 max_fails=5 fail_timeout=30s;
    } 



      location ~* ^/OCC_DATACO_WEB/.*$ {
           include deny.conf;

           proxy_pass http://datacollectbackend;
           include proxy.conf;

           error_log  logs/datacollection_error.log error;
           access_log  logs/datacollection_access.log  main;
       }

2,用location的proxy_pass做跳转

直接将dt的跳转到和 OCC_DATACO_WEB一样的proxy_pass,如下所示

 location ~* ^/dt/.*$ {
           include deny.conf;

           proxy_pass http://datacollectbackend;
           include proxy.conf;

          error_log  logs/dt_error.log error;
          access_log  logs/dt__access.log  main;
        }   
Sorry,找不到该页面

您使用的URL可能拼写有误,该页可能已经移动,或者它可能只是临时脱机。
重新键入正确网址,或者返回首页

3,用rewrite来取代location

    location ~* ^/dt/.*$ {
          rewrite /dt/(.*) /OCC_DATACO_WEB/$1 break;

        }
然后重启nginx,使用www.linuxidc/dt/possystem访问,已经生效了,成功了。

4,总结

如果相对域名或参数字符串起作用,可以使用全局变量匹配,也可以使用proxy_pass反向代理。
表明看rewrite和location功能有点像,都能实现跳转,主要区别在于rewrite是在同一域名内更改获取资源的路径,而location是对一类路径做控制访问或反向代理,可以proxy_pass到其他机器。很多情况下rewrite也会写在location里,它们的执行顺序是:

执行server块的rewrite指令
执行location匹配
执行选定的location中的rewrite指令
如果其中某步URI被重写,则重新循环执行1-3,直到找到真实存在的文件;循环超过10次,则返回500 Internal Server Error错误。

本次实践中,是需要将dt的工程访问都指向OCC_DATACO_WEB工程里面去,所以需要仅仅location加proxy_pass是不够的,需要rewrite的。

更多Nginx相关教程见以下内容

Nginx 的详细介绍请点这里
Nginx 的下载地址请点这里

猜你喜欢

转载自www.linuxidc.com/Linux/2016-01/126991.htm