如何使用 nginx 将单个 URL 的部分内容指向两个不同的服务器

如何使用 nginx 将单个 URL 的部分内容指向两个不同的服务器

我已经将 location /tanya 指向一个地址http://52.221.238.24/tanya;,我也希望/tanya/t/指向同一个IP。

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

答案1

我找到了解决方案。问题是,我的 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
      });
 });

相关内容