nginx 中的服务器块 - 502 错误 [英] Server Blocks in nginx - 502 Error

查看:67
本文介绍了nginx 中的服务器块 - 502 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一台运行 nginx 的服务器捕获和转发 2 个子域:foo.acme.com、bar.acme.com

I have 2 subdomains I want to catch and forward from one server running nginx: foo.acme.com, bar.acme.com

在我的 nginx.conf 文件中,我设置了 2 个服务器块:

In my nginx.conf file I have set up 2 server blocks:

server {
    listen       80;
    server_name foo.acme.com;
    location / {
        proxy_pass http://<my_ip_server_1>:80;
    }
}
server {
    listen       80;
    server_name bar.acme.com;
    location / {
        proxy_pass http://<my_ip_server_2>:80;
    }
}

我的 2 个子域指向同一个 IP(运行 nginx 的那个).在此配置中,我在两台服务器上都收到 502 Bad Gateway 错误.

My 2 subdomains point to the same IP (the one with nginx running on it). I'm getting 502 Bad Gateway errors on both servers in this configuration.

推荐答案

502 代码的意思是 502 Bad Gateway,服务器作为网关或代理,收到来自上游服务器的无效响应.

The 502 code means 502 Bad Gateway, The server was acting as a gateway or proxy and received an invalid response from the upstream server.

这通常意味着无法访问后端服务器,这可能是它们的问题,而不是您的前端配置.

It usually means the backend servers are not reachable, which could be a problem with them, not with your front-end configuration.

在运行 Nginx 的机器上,你应该测试你可以休息后端服务器.使用 w3m 或该机器上的其他 HTTP 客户端,检查这些 URL.他们是否加载了您期望的内容?

On the machine running Nginx, you should test that you can rest the backend servers. Using w3m or another HTTP client on that machine, check these URLs. Do they load what you expect?

http://<my_ip_server_1>:80
http://<my_ip_server_2>:80

如果没有,您可能需要进行一些更新以确保您的 Nginx 服务器可以访问后端服务器.

If not, you may have some updates to make sure that your Nginx server can reach the backend servers.

我应该补充一点,如果后端服务器都托管多个虚拟域,则您可能需要发送 Host: 标头以使后端服务器提供预期的内容.我喜欢使用 libwww-perl 发行版中的 GETHEAD 工具:

I should add, you may need send the Host: header to get the backend servers to serve the expected content, if they each host multiple virtual domains. I like to use GET and HEAD tools from the libwww-perl distribution:

GET -H 'Host: bar.acme.com' http://http://<my_ip_server_1>:80

在托管 Nginx 的机器上运行测试很重要,因为从桌面运行它可能会产生不同的结果.

It's important to run the test from the machine hosting Nginx, as running it from your desktop could produce a different result.

这篇关于nginx 中的服务器块 - 502 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆