41

Here is my situation: I will have one frontend server running Nginx, and multiple backends servers running Apache + passenger with different rails applications. I am NOT trying to do any load balancing. What I need to do is setup Nginx to proxy connections to specific servers based on the URL. IE, client.example.com should point to x.x.x.100:80, client2.example.com should point to x.x.x.101:80, etc.

I am not that familiar with Nginx, but I could not find a specific configuration online that fit my situation.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Capt.Redbeard
  • 790
  • 1
  • 8
  • 14
  • hey were you able to find a solution for this? in my case i want same client to access to multiple of these backend servers, can you help me find a config for that? – Vinodborole Sep 06 '16 at 12:50

2 Answers2

40

You can match the different URLs with server {} blocks, then inside each server block, you'd have the reverse proxy settings.

Below, an illustration;

server {
  server_name client.example.com;

  # app1 reverse proxy follow
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass http://x.x.x.100:80;

}

server {
  server_name client2.example.com;

  # app2 reverse proxy settings follow
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_pass http://x.x.x.101:80;
}

Also, you may add further Nginx settings (such as error_page and access_log) as desired in each server {} block.

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
cobaco
  • 10,224
  • 6
  • 36
  • 33
  • 2
    Is it possible to configure each origin server to be proxied based on path and not just incoming host/authority? I.e. `/system1/` is proxied to `http://x.x.x.100/` while `/system2/` is proxied to `http://x.x.x.120/`? – Asbjørn Ulsberg Jun 12 '15 at 12:08
  • 1
    sure, in that case you simply place the proxy directives in an appropriate location block, instead of in the main server block – cobaco Jun 15 '15 at 10:01
  • 6
    I put this in a config file inside sites-enabled and restarted the nginx and now I'm getting this error in logs: directive is not allowed here in /etc/nginx/sites-enabled/my.domain.com – mohamnag Jun 22 '15 at 21:34
  • which directive is not allowed? (running `nginx -t` should tell you which directive on which line). What scope is the config block that directive is in (http/server/location)? – cobaco Jun 24 '15 at 07:53
  • `proxy_pass` seems to not be allowed in server context – Paweł Szczur Dec 28 '16 at 14:46
  • Why not add an upstream configuration? This may make the configuration more clear. See [this example from official guide](https://www.nginx.com/resources/admin-guide/nginx-tcp-ssl-upstreams/). – qtopierw Oct 02 '17 at 03:02
  • in which file should I write these? is it /etc/nginx/nginx.conf? or sites-enabled? – Eziz Durdyyev Jan 09 '19 at 11:06
28

@mohamnag's comment is right. proxy_pass is only allowed inside a location

See:

http://wiki.nginx.org/HttpProxyModule#proxy_pass

https://www.nginx.com/resources/admin-guide/reverse-proxy/

So the correct config would be

server {
    server_name client.example.com;

    location / {
        # app1 reverse proxy follow
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://x.x.x.100:80;
    }
}

server {
    server_name client2.example.com;

    location / {
        # app2 reverse proxy settings follow
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass http://x.x.x.101:80;
    }
}
Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
jaltek
  • 2,402
  • 1
  • 23
  • 16
  • 1
    in which file should I write these? is it /etc/nginx/nginx.conf? or sites-enabled? – Eziz Durdyyev Jan 09 '19 at 11:06
  • 2
    @EzizDurdyyev: "sites-enabled" is IMHO a Ubuntu related layout of configuration files and does only contain symlinks to the available sites in "sites-available". But yes, this should be placed inside the single site configurations under "sites-available". – jaltek Jul 03 '19 at 13:38