我们有一个QA版本,一个UAT版本和一个DEV版本的webapp。 用户需要通过http://uat.company.com:41002/webapp和http://dev.company.com:41002/webapp进入。 在端口41001上还有一个不同的webapp,他们也需要访问端口8080。
这些url需要在公司外部可用,我们只有一个公共IP地址可以访问。 因为这样的DNSlogging需要全部3个地址来指向一个IP。 在单个IP地址上,服务器驻留在运行nginx。 在后台我需要每个url指向一个不同的服务器
http://uat.company.com --> 123.123.123.1 http://qa.company.com --> 123.123.123.2 http://dev.company.com --> 123.123.123.3
恐怕我不知道正确的术语,但是URI和端口的其余部分也必须结转到IP地址。 即如果有人访问
http://uat.company.com:41002/webapp/somepage`
它会看起来好像是他们访问的页面,但他们真的会看
http://123.123.123.1:41002/webapp/somepage
或者如果他们访问
http://qa.company.com:8080/static/home.html
他们真的会在看
http://123.123.123.2:8080/static/home.html
但他们的浏览器仍然会说http://qa.company.com:8080/static/home.html
我努力了
server { server_name uat.company.com; listen 41001; listen 41002; listen 8080; location / { proxy_pass http://123.123.123.1:$server_port$uri; proxy_set_header Host $host; }
}
但是,这给了我一个坏的网关502页与日志: 2015/01/28 16:04:49 [crit] 30571#0: *1 connect() to 123.123.123.1:41002 failed (13: Permission denied) while connecting to upstream, client: 172.23.128.245, server: uat.company.com, request: "GET /webapp/ HTTP/1.1", upstream: "http://123.123.123.1:41002/webapp/", host: "uat.company.com:41002"
我希望这更清楚。
更新从Xaviers的build议,SELinux可能一直在阻碍,我已经禁用它,我确实进一步。 现在使用上面的nginxconfiguration似乎连接到第二台服务器:然而,端口仍然没有通过。 我在打电话
uat.company.com:41002/webapp/
这将直接调用服务,redirect到
uat.company.com:41002/webapp/spring/config/main
然而,通过代理发生的事情是,它正在返回或结束于
uat.company.com/webapp/spring/config/main
并因此无法加载页面…
我已经确定了我的设置有什么问题。
1)SELinux阻止我连接上游。 我现在已经禁用了这个function,并且会考虑稍后再正确设置它
2)proxy_pass按预期工作,但是我需要的参数是http://123.123.123.1:$server_port/$uri$is_args$args;
3) proxy_set_header Host $host正确地将主机名设置回我想要的,但是它吃掉了端口号。 我需要的正确格式是proxy_set_header Host $host:$server_port
有可能是更好的解决scheme,我还没有得到一个完整的工作解决scheme,因为我已经削减它的工作,但我的configuration工作部分是:
server { listen 41002; server_name uat.comapny.com; location /webapp { proxy_pass http://123.123.123.1:41002/$uri$is_args$args; proxy_set_header Host $host:$server_port; } }
一旦我充实了,我会发布一个更通用的版本。 非常感谢所有的帮助。
你想维护端口,那么你应该把它放在proxy_pass 。
server { server_name bob.something.com; listen 41001; listen 41002; listen 8080; location / { proxy_pass http://123.123.123.1:$server_port$uri; proxy_set_header Host $host; } }
你必须认识到这将通过nginx代理请求。