简体   繁体   中英

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

I have pointed a location /tanya to an address http://52.221.238.24/tanya ;, I also want /tanya/t/ to point to the same IP.

However, I need to point /tanya/dynamically_generated to point to another IP http://127.0.53.53:3000 ;

How is it possible using nginx.

I tried the following:

     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 route:

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

I figured the solution. Problem was, my nodeJs route was conflicting with the Nginx locations. Solution : I created another subdir for my nodeJs route. It seems something like the following:

Nginx config :

    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";
     }

Apparently, Nginx lookup for the LONGEST location uri first. As a result, it first looks up for either of "/tanya/amp/" and "/tanya/t/" and points to their respective IPs.

If none of them ("/amp" or "/t") are queried, it looks up "/tanya/anyting else" and points to IP pointed at "/tanya" location.

I thus made another change in my route (nodeJs) :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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