繁体   English   中英

如何使用Nginx将单个URL的一部分指向2个不同的服务器

[英]how to point parts of a single url to 2 different servers using nginx

我已将位置/ tanya指向地址http://52.221.238.24/tanya;,我也希望/ tanya / t /指向相同的IP。

但是,我需要指向/ tanya / dynamically_genic指向另一个IP http://127.0.53.53:3000 ;

如何使用nginx。

我尝试了以下方法:

     location / {
            proxy_pass http://127.0.53.53:3000;
            include /etc/nginx/proxy_params;
            proxy_http_version 1.1;
            chunked_transfer_encoding off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
     }

    location /tanya/t/ {
            proxy_pass http://52.221.238.24/tanya/t/;
            include /etc/nginx/proxy_params;
            proxy_http_version 1.1;
            chunked_transfer_encoding off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
     }

    location /tanya {
            proxy_pass http://127.0.53.53:3000;
            include /etc/nginx/proxy_params;
            proxy_http_version 1.1;
            chunked_transfer_encoding off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
     }

     location = /tanya/ {
            proxy_pass http://52.221.238.24/tanya;
            include /etc/nginx/proxy_params;
            proxy_http_version 1.1;
            chunked_transfer_encoding off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
     }

NodeJs路由:

     app.get('/tanya/:questionUrl',(req,res)=>{
          let ampListUrl = req.ampListBase+'/tanya/t';
          res.render('./html/questionsTanya.ejs', {
              question: question,
              ampListUrl:ampListUrl
          });
     });

我想出了解决方案。 问题是,我的nodeJs路由与Nginx位置冲突。 解决方案:我为我的nodeJs路由创建了另一个子目录。 似乎如下所示:

Nginx的配置:

    location / {
            proxy_pass http://127.0.53.53:3000;
            include /etc/nginx/proxy_params;
            proxy_http_version 1.1;
            chunked_transfer_encoding off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
     }

     location /tanya/t/ {
            proxy_pass http://52.221.238.24/tanya/t/;
            include /etc/nginx/proxy_params;
            proxy_http_version 1.1;
            chunked_transfer_encoding off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
     }

    location /tanya/amp/ {
            proxy_pass http://127.0.53.53:3000;
            include /etc/nginx/proxy_params;
            proxy_http_version 1.1;
            chunked_transfer_encoding off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
     }
    location /tanya {
            proxy_pass http://52.221.238.24/tanya;
            include /etc/nginx/proxy_params;
            proxy_http_version 1.1;
            chunked_transfer_encoding off;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
     }

显然,首先是Nginx查找最长位置uri。 结果,它首先查找“ / tanya / amp /”和“ / tanya / t /”中的一个,并指向它们各自的IP。

如果都不查询(“ / amp”或“ / t”),则查询“ / tanya /其他”,并指向指向“ / tanya”位置的IP。

因此,我对路线(nodeJs)进行了另一处更改:

     app.get('/tanya/amp/:questionUrl',(req,res)=>{
      let ampListUrl = req.ampListBase+'/tanya/t';
      res.render('./html/questionsTanya.ejs', {
          question: question,
          ampListUrl:ampListUrl
      });
 });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM