How to use Nginx for Reverse Proxy with Load Balancing?

Looking for someone’s knowledge on using Nginx for reverse proxy. Below is the issue I’m finding…

server 1 http://ip:port/folder/
server 2 http://ip:port/folder/

They do match up however the second server has files on it like portal.php which it looks like its trying to load locally from server 1. Is it possible to grab everything from the second server? If that makes sense.

Error Log:

20xx/xx/xx 23:12:49 [error] 9416#0: *7 open() “/usr/share/nginx/html/portal.php” failed (2: No such file or directory), client: **IP ADDRESS**, server: server.local, request: “GET /portal.php?type=stb&action=handshake&token=&JsHttpRequest=1-xml HTTP/1.1”, host: “IP:PORT”, referrer: “http://IP:PORT/c/”

in progress
TroubleShooting 2 Answers 1084 views 0

Answers ( 2 )


    Sounds like you want to know how to set up load balancing and not a reverse proxy.


      Not essentially load balancing. I want to hide the 2nd server behind the first so that when users go to http://IP:port/folder/ it will still work the way it would from server 2. However, it seems to be trying to load files locally instead of following the server 2 address to pull everything.


        Your reverse proxy will be the server you will want to be publicly accessible (so let’s say server 2) will be where you want users to request from and server 1 (behind your NAT) will be the server hosting the content but is hidden.


          Server 1 is the publicly accessible one. Server 2 is the ones with the files on them.

          A device connects to the address such as http://IP:port/folder/ and that will begin to pull stuff through but as you can see from the error log it won’t pull stuff through because anything after /folder/ it looks for it locally rather than on the other server.

          Is there something I can add to the config file to change this?


            Show me your current config on both servers? Are both servers running Nginx as their web-server or is the one hosting the content running Apache?


              One hosting content is running Apache and Nginx, that’s the one hosting the files.

              Config for the server – not hosting the files but will be the main point of contact:

              server { listen 25461; server_name server.local;

              location /c/ { proxy_pass http://IP:25461; proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

              IP will be with the IP address for the server.


                Add in proxy_redirect off;


                  Done added that in. Still no difference!


                    On your Apache server do you have the libapache2-mod-rpaf module setup so that your Apache server can rewrite the values of REMOTE_ADDR, HTTP_HOST and HTTP_PORT etc based on the values provided by the reverse proxy?


                      Unsure – I’ll check that. Do you think that’s the issue?


                        Possibly but can’t say for sure unless I’m seeing how both servers are set up in front of me.


                          I didn’t think it mattered much about the server with the files, as the second server itself works normally, it’s just getting the first server to essentially connect to it.


    location / { proxy_pass; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_redirect off; }

    put that in the server {} and point the proxy pass to whatever IP and port your app is on.

    Another solution – As a Docker container ->

Leave an answer
