Nginx反向代理上游不起作用 [英] Nginx Reverse Proxy upstream not working
问题描述
我在弄清楚Nginx上的负载平衡时遇到了麻烦.我正在使用: -Ubuntu 16.04和 -Nginx 1.10.0.
I'm having trouble figuring out load balancing on Nginx. I'm using: - Ubuntu 16.04 and - Nginx 1.10.0.
简而言之,当我将我的IP地址直接传递到"proxy_pass"时,代理有效:
In short, when I pass my ip address directly into "proxy_pass", the proxy works:
server {
location / {
proxy_pass http://01.02.03.04;
}
}
当我访问代理计算机时,可以从代理ip中看到内容... 但是当我使用上游指令时,它不会:
When I visit my proxy computer, I can see the content from the proxy ip... but when I use an upstream directive, it doesn't:
upstream backend {
server 01.02.03.04;
}
server {
location / {
proxy_pass http://backend;
}
}
当我访问代理计算机时,会看到默认的Nginx服务器页面,而不是上游IP地址中的内容.
When I visit my proxy computer, I am greeted with the default Nginx server page and not the content from the upstream ip address.
任何进一步的帮助将不胜感激.我已经做了大量研究,但无法弄清楚为什么上游"不起作用.我没有任何错误.它只是不代理.
Any further assistance would be appreciated. I've done a ton of research but can't figure out why "upstream" is not working. I don't get any errors. It just doesn't proxy.
推荐答案
好的,看起来我找到了答案...
Okay, looks like I found the answer...
关于后端服务器的两件事,至少在使用IP地址时在上述情况下如此:
two things about the backend servers, at least for the above scenario when using IP addressses:
- 必须指定端口
- 端口不能为:80(根据@karliwsn,端口可以为80,这只是上游服务器无法侦听与反向代理相同的端口.我尚未对其进行测试,但要注意).
后端服务器块应配置如下:
backend server block(s) should be configured as following:
server {
# for your reverse_proxy, *do not* listen to port 80
listen 8080;
listen [::]:8080;
server_name 01.02.03.04;
# your other statements below
...
}
您的反向代理服务器块的配置应如下所示:
and your reverse proxy server block should be configured like below:
upstream backend {
server 01.02.03.04:8080;
}
server {
location / {
proxy_pass http://backend;
}
}
似乎后端服务器正在监听:80,反向代理服务器不呈现其内容.我想这是有道理的,因为服务器实际上正在为普通大众使用默认端口80.
It looks as if a backend server is listening to :80, the reverse proxy server doesn't render it's content. I guess that makes sense, since the server is in fact using default port 80 for the general public.
感谢@karliwson促使我重新考虑端口.
Thanks @karliwson for nudging me to reconsider the port.
这篇关于Nginx反向代理上游不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!