Redirect request coming from a IP:$Port/$url to some flask API using Nginx.

October 19, 2015 3.2k views
Nginx API

Hi how can I use nginx to redirect requests coming to a structure like IP:$Post/$url for e.g. localhost:4000/savedata to some other API which I have made?
I am able to redirect the requests coming to "localhost:4000" to my API, but I am not able to figure out how to incorporate the url part as well.

1 Answer

Instead of a redirect, it sounds like you want to set up nginx as a proxy to your API. Using the proxy_pass variable you can set up nginx to proxy communications to your API. This guide may be helpful, while it is about setting up nginx to proxy connections to apache, the details on how to configure nginx to proxy connections are the same.

by Etel Sverdlov
This tutorial covers combining apache and nginx to serve sites with more speed. This article takes you from a fresh DigitalOcean droplet to one where apache and nginx are configured to work together.
  • I am using the proxy_pass variable only to sort of redirect or as you said serving my API as a proxied one. But I am only able to make a proxy for say "localhost:$PORT" to my API which is something like "localhost:$PORT1/savedata". But since I am being sent the request on "localhost:$PORT/savedata", I want to use this url as a proxy url and not just "localhost:$PORT".
    So, that whenever I am sent request on "localhost:$PORT/savedata", I use nginx as proxy server and send the request to my API which is running on "localhost:$PORT1/savedata"

    • This should be possible by changing the location block the proxy_pass variable is inside:

      
              location /savedata {
      
              proxy_set_header X-Real-IP  $remote_addr;
              proxy_set_header X-Forwarded-For $remote_addr;
              proxy_set_header Host $host;
              proxy_pass http://127.0.0.1:4000;
      
               }
      
      • I tried doing this, but this gives the following message on my browser.

        "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."

        But, when I do it like :

        location / {

            proxy_set_header X-Real-IP  $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_set_header Host $host;
            proxy_pass http://127.0.0.1:4000;
        
             }
        

        i.e. replacing the "/savedata" url with "/". it works fine and correctly takes me to the results of the API http://127.0.0.1:4000;

        • Thanks @ryanpq :

          I found a way how to do this. Actually, it was a very trivial thing.
          This is what I tried:

            server {
                      listen localhost:4000;
                      listen *:4000;
                      location /savedata {
                      proxy_pass http://localhost:5000;
                      }
          

          The only difference was that this time my API also had a url "/savedata" in it. And what the above block does is that it sends the request to "http://localhost:5000/savdata" (like it would try to search for a file or folder after it's root path if it was not working as proxy server). Since earlier I did not have any such url like "/savedata" in my api, so nginx was not able to find it.

Have another answer? Share your knowledge.