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 0
TroubleShooting 13 Answers 131 views 0

Answers ( 13 )

  1. Gillian Koch
    0

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

    • Quentin Scott
      0

      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.

      • Gillian Koch
        0

        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.

        • Quentin Scott
          0

          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?

          • Gillian Koch
            0

            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?

            • Quentin Scott
              0

              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.

              • Gillian Koch
                0

                Add in proxy_redirect off;

                • Quentin Scott
                  0

                  Done added that in. Still no difference!

                  • Gillian Koch
                    0

                    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?

                    • Quentin Scott
                      0

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

                      • Gillian Koch
                        0

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

                        • Quentin Scott
                          0

                          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.

  2. Harvey Parker
    0

    location / {
    proxy_pass http://127.0.0.1;
    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 -> https://hub.docker.com/r/jwilder/nginx-proxy/

Leave an answer

Click the camera icon to upload an image to your answer. You can upload one image with JPG, GIF or PNG extension and Maximum size of file cannot be more than 2 MB. If you want to add multiple images, paste the direct URLs of images in the answer box.