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.

1
  • 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? Commented Sep 6, 2016 at 12:50

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

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/?
sure, in that case you simply place the proxy directives in an appropriate location block, instead of in the main server block
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
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)?
proxy_pass seems to not be allowed in server context
|
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;
    }
}

2 Comments

in which file should I write these? is it /etc/nginx/nginx.conf? or sites-enabled?
@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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.