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

时间:2018-10-09 12:54:38

标签: javascript node.js nginx

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

但是,我需要指向/ tanya / dynamically_generation指向另一个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";
     }

NodeJ的路线:

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

1 个答案:

答案 0 :(得分:0)

我想出了解决方案。 问题是,我的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 / anyting else”并指向指向“ / 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
      });
 });