Docker Nginx反向代理提供了"502 Bad Gateway" [英] Docker nginx reverse proxy gives "502 Bad Gateway"
问题描述
我正在尝试使具有nginx的docker容器作为其他docker容器的反向代理,并且我在除基本位置'/'之外的其他位置不断获取错误的网关".
I'm trying to have a docker container with nginx work as reverse proxy to other docker containers and I keep getting "Bad Gateway" on locations other other than the base location '/'.
我有以下服务器块:
server {
listen 80;
location / {
proxy_pass "http://game2048:8080";
}
location /game {
proxy_pass "http://game:9999";
}
}
它适用于http://localhost
,但不适用于http://localhost/game
,它在浏览器中提供了错误的网关",并在nginx容器上提供了该信息:
It works for http://localhost
but not for http://localhost/game
which gives "Bad Gateway" in the browser and this on the nginx container:
[error] 7#7: *6 connect() failed (111: Connection refused)
while connecting to upstream, client: 172.17.0.1, server: ,
request: "GET /game HTTP/1.1", upstream: "http://172.17.0.4:9999/game",
host: "localhost"
我使用官方的nginx docker映像并在上面放置自己的配置.您可以对其进行测试并在此处查看所有详细信息: https://github.com/jollege/ngprox1
I use the official nginx docker image and put my own configuration on it. You can test it and see all details here: https://github.com/jollege/ngprox1
任何想法出什么问题了吗?
Any ideas what goes wrong?
注意:我已经在docker主机上设置了本地主机名条目以匹配这些名称:
NB: I have set local hostname entries on docker host to match those names:
127.0.1.1 game2048
127.0.1.1 game
推荐答案
我已修复它!我在nginx config中的不同服务器块中设置了服务器名称.记住要使用docker端口,而不要使用主机端口.
I fixed it! I set the server name in different server blocks in nginx config. Remember to use docker port, not host port.
server {
listen 80;
server_name game2048;
location / {
proxy_pass "http://game2048:8080";
}
}
server {
listen 80;
server_name game;
location / {
# Remember to refer to docker port, not host port
# which is 9999 in this case:
proxy_pass "http://game:8080";
}
}
github存储库已更新,以反映此修复程序,./README.old01.md
下有旧的自述文件.
The github repo has been updated to reflect the fix, the old readme file is there under ./README.old01.md
.
典型地,当我认真地向他人表达问题时,我会找到答案.你知道那种感觉吗?
Typical that I find the answer when I carefully phrase the question to others. Do you know that feeling?
这篇关于Docker Nginx反向代理提供了"502 Bad Gateway"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!