nginx reverse proxy multiple backends

2019-01-08 07:44发布

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.domain.com should point to x.x.x.100:80, client2.domain.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.

Thanks.

标签: nginx
2条回答
冷血范
2楼-- · 2019-01-08 08:18

@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.domain.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.domain.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.100:80;
  }
}
查看更多
祖国的老花朵
3楼-- · 2019-01-08 08:26

you can match the different url's with server blocks then inside each server block you'd have the reverse proxy settings

server { 
  server_name client.domain.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.domain.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.100:80;
}

obviously add further nginx settings (like error_page, access_log, ...) as desired in each server-block

查看更多
登录 后发表回答