簡體   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