Nginx 反向代理上游不工作
Nginx Reverse Proxy upstream not working
我在计算 Nginx 上的负载平衡时遇到问题。我在用着:
- Ubuntu 16.04 和
- Nginx 1.10.0.
简而言之,当我将我的 IP 地址直接传递给 "proxy_pass" 时,代理有效:
server {
location / {
proxy_pass http://01.02.03.04;
}
}
当我访问我的代理计算机时,我可以看到来自代理 ip 的内容...
但是当我使用上游指令时,它不会:
upstream backend {
server 01.02.03.04;
}
server {
location / {
proxy_pass http://backend;
}
}
当我访问我的代理计算机时,迎接我的是默认的 Nginx 服务器页面,而不是来自上游 IP 地址的内容。
如有任何进一步的帮助,我们将不胜感激。我做了很多研究,但无法弄清楚为什么 "upstream" 不起作用。我没有收到任何错误。它只是不代理。
好的,看来我找到答案了...
关于后端服务器的两件事,至少对于上述使用 IP 地址的情况:
- 必须指定端口
- 端口不能是:80(根据@karliwsn,端口可以是80,只是上游服务器不能监听与反向代理相同的端口。我还没有测试过,但要注意).
后端服务器块应配置如下:
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
...
}
你的反向代理服务器块应该配置如下:
upstream backend {
server 01.02.03.04:8080;
}
server {
location / {
proxy_pass http://backend;
}
}
看起来后端服务器正在侦听 :80,反向代理服务器不呈现它的内容。我想这是有道理的,因为服务器实际上为一般 public.
使用默认端口 80
感谢@karliwson 提醒我重新考虑移植。
以下示例有效:
唯一需要说明的是,如果服务器IP是“server_name”,那么访问该站点需要使用该IP,也就是说在浏览器中需要输入URL 作为 http://yyy.yyy.yyy.yyy or (http://yyy.yyy.yyy.yyy:80), if you use the domain name as the "server_name", then access the proxy server using the domain name (e.g. http://www.yourdomain.com)
upstream backend {
server xxx.xxx.xxx.xxx:8080;
}
server {
listen 80;
server_name yyy.yyy.yyy.yyy;
location / {
proxy_pass http://backend;
}
}
我在计算 Nginx 上的负载平衡时遇到问题。我在用着: - Ubuntu 16.04 和 - Nginx 1.10.0.
简而言之,当我将我的 IP 地址直接传递给 "proxy_pass" 时,代理有效:
server {
location / {
proxy_pass http://01.02.03.04;
}
}
当我访问我的代理计算机时,我可以看到来自代理 ip 的内容... 但是当我使用上游指令时,它不会:
upstream backend {
server 01.02.03.04;
}
server {
location / {
proxy_pass http://backend;
}
}
当我访问我的代理计算机时,迎接我的是默认的 Nginx 服务器页面,而不是来自上游 IP 地址的内容。
如有任何进一步的帮助,我们将不胜感激。我做了很多研究,但无法弄清楚为什么 "upstream" 不起作用。我没有收到任何错误。它只是不代理。
好的,看来我找到答案了...
关于后端服务器的两件事,至少对于上述使用 IP 地址的情况:
- 必须指定端口
- 端口不能是:80(根据@karliwsn,端口可以是80,只是上游服务器不能监听与反向代理相同的端口。我还没有测试过,但要注意).
后端服务器块应配置如下:
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
...
}
你的反向代理服务器块应该配置如下:
upstream backend {
server 01.02.03.04:8080;
}
server {
location / {
proxy_pass http://backend;
}
}
看起来后端服务器正在侦听 :80,反向代理服务器不呈现它的内容。我想这是有道理的,因为服务器实际上为一般 public.
使用默认端口 80感谢@karliwson 提醒我重新考虑移植。
以下示例有效:
唯一需要说明的是,如果服务器IP是“server_name”,那么访问该站点需要使用该IP,也就是说在浏览器中需要输入URL 作为 http://yyy.yyy.yyy.yyy or (http://yyy.yyy.yyy.yyy:80), if you use the domain name as the "server_name", then access the proxy server using the domain name (e.g. http://www.yourdomain.com)
upstream backend {
server xxx.xxx.xxx.xxx:8080;
}
server {
listen 80;
server_name yyy.yyy.yyy.yyy;
location / {
proxy_pass http://backend;
}
}