Reverse proxy for multiple servers

I've been searching the web for some time now but i can't really find whether it's possible or how to write the config.

**Is it possible to run an NGINX reverse proxy to multiple hosts (either physical or VM's or docker containers)?**
All i can find are examples that hosts on the same server (i.e. localhost).

What i'd like to achieve is to run NGINX on (for example) Server1. And i have an app on Server2 and another app an Server3.
so the scenario would be:

https://preview.redd.it/b38yerd6rnk71.png?width=344&format=png&auto=webp&s=de000d2d885ae56b8f013e54a9f06e386f3f0413

This is for internal use only, so no domain name. I access the apps from [http://server2/app2](http://server2/app2) for example.

i know that i can specify server blocks in my config file but im not sure what i should do to make something like this to work or what i'd need to make it work.

i've added the ip-addresses and hostnames to the /etc/hosts file

I'm running NGINX from a docker container.

I don't have access to my actually config file at the moment i was tinkering with the following.

server {
listen 80;
server_name _;
location /app1 {
proxy_pass http://SERVER_OR_IPADRESS:port;
}
}

server {
listen 80;
server_name _;

location / {
proxy_pass http://SERVER_OR_IPADRESS:port;
}
}

From my understanding of the NGINX directives is that it primarily looks at the LISTEN directive. So in this case it will listen to anything on port 80 (correct me if i'm wrong).
The SERVER\_NAME is by default an underscore. but i've changed this to match Server1 and Server2 or their respective IP adrresses but with no luck.

I thought i don't need to run nginx an all servers (i could be wrong here but i can't find any definitive proof for that).

Basically, from the client i go to http://server2/app2 to and it would redirect to the corresponding site on server2. Same goes for [http://server3/app3](http://server3/app3). If i test this individually it works. i can access the apps from their IP:PORT but i can't access them from the above URLs.

how do i go about writing this config?

5 thoughts on “Reverse proxy for multiple servers”

  1. Your main idea is correct.

    You have to use either server names or separate IP’s in the listen directives for nginx to be able to distinguish the sites from one another. The most common use is using server\_name. Mainly since it enables you to run virtually unlimited vhosts on a single IP.

    To troubleshoot your problem we actually don’t have enough information. You might want to consider the following:

    * What do your access logs say?
    * Do you get an error when using the above URLs, you can get 404’s, 500’s, 502s, 503s, it all has a different meaning in what exactly is going wrong.
    * Do your backend application keep logs on the incoming HTTP requests? If so, do they tell you anything?

    Reply
  2. If you want to run the same apps in both servers (for better redundancy or performance), you could use either a virtual or physical load balancer. I’ve heard Kemp is a pretty good virtual one but it isn’t free

    Reply
  3. I would suggest looking into docker swarm – 3 containers, one per server, internal network between them and then just proxy pass to particular container. That’s at least how I would do it, but maybe there are better options.

    Reply
  4. I had a similar problem to fix it

    first check if the port is not using by another program in the server1 and then try this config

    server {
    listen 80;
    server_name _;

    location / {
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://SERVER_OR_IPADRESS:port;
    proxy_redirect default;
    }

    location /app1 {
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://SERVER_OR_IPADRESS:port;
    proxy_redirect default;
    }

    }

    i hope this can be useful 🙂

    Reply

Leave a Comment