0

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";
 }
  • Hi and welcome. Your example configuration seems to be a bit conflicting with your description. Could you try to show in a more explicit way, which URL needs to be redirected to which destination? – Tero Kilkanen Oct 09 '18 at 19:23
  • Hi,I have my app runs at "/" : http://127.0.53.53:3000. I need a forum at "/tanya" and some of its content at"/tanya/t/" which run on a different server : http://52.221.238.24/tanya/. Right now "/", "/tanya" and "/tanya/t'" works fine for me. problem is I also want a route from nodeJs as : "/tanya/:question", but it's not working since location "/tanya" is already configured in nginx to point to a different server. – Subrat Dash Oct 10 '18 at 04:37
  • My question is , is there a way we could configure nginx locations to point "/tanya" and "/tanya/t/ to IP : 52.221.238.24. at the same time run a route "/tanya/:questions" from my nodeJs server. *note : :question is dynamic and assists get request from client. eg url : 127.0.53.53:3000/tanya/how-to-resolve – Subrat Dash Oct 10 '18 at 04:47
  • So basically to make it more clear, lets say IPs A and B, I would need them pointing as: "/" : 127.0.53.53:3000 "/tanya" : 52.221.238.24/tanya "/tanya/some random questions : 127.0.53.53:3000/tanya:questions "/tanya/t/" : 52.221.238.24/tanya/t/ – Subrat Dash Oct 10 '18 at 04:54

1 Answers1

0

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