[英]Docker Nginx + Node: address already in use
I'm trying to configure Nginx (inside Docker container) with Node.js (outside, on host machine). 我正在尝试使用Node.js配置Nginx(在Docker容器内)(在主机上)。 The Nginx configuration uses upstream
and proxy-pass
directives: Nginx配置使用upstream
和proxy-pass
指令:
upstream helloworld {
server localhost:8080;
}
server {
listen 443;
ssl on;
ssl_certificate /some/cert.crt;
ssl_certificate_key /some/cert.key;
location / {
proxy_pass http://helloworld;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
}
}
Node app listens on port 8080 of the host machine. 节点应用程序侦听主机的端口8080。 Now, I start Nginx container 现在,我启动Nginx容器
docker run \
-d \
-p 80:80 \
-p 443:443 \
-p 8080:8080 \
-v /some/mounts:/to/some/mounts \
--name nginx \
nginx:alpine
What I expect is that Nginx receives connections to ports 80 and 443 and forwards them to port 8080 inside of its container, which will then be forwarded to port 8080 of the host machine to the Node app ( -p 8080:8080
), however it gives an error: Error starting userland proxy: listen tcp 0.0.0.0:8080: bind: address already in use.
我期望Nginx接收到端口80和443的连接,并将它们转发到其容器内的端口8080,然后将其转发到主机的端口8080到节点应用程序( -p 8080:8080
),但是它给出错误: Error starting userland proxy: listen tcp 0.0.0.0:8080: bind: address already in use.
I've tried changing localhost
to 127.0.0.1
and even to the ip-address of the host machine ( 172.17.0.1
produced by $ /sbin/ip route
from inside of the Nginx container), but none of it seems to work. 我已经尝试将localhost
更改为127.0.0.1
甚至更改为localhost
的ip-address(由Nginx容器内部的$ /sbin/ip route
生成的172.17.0.1
),但它们似乎都没有。 If I don't use -p 8080:8080
while starting the container it doesn't work either. 如果我在启动容器时不使用-p 8080:8080
则它也不起作用。
Even though 172.17.0.1
is the host machine's ip-address, I can't connect to it from within the Nginx container: 即使172.17.0.1
是主机的ip-address,我也无法从Nginx容器中连接到它:
$ wget 172.17.0.1:8080
Connecting to 172.17.0.1:8080... failed: Connection refused.
Now, I know I can Dockerize my Node app and use --link
argument when starting Nginx container, but it is not a solution for the moment, since it requires a lot of re-writing of the Node app. 现在,我知道我可以使用Dockerize我的Node应用程序并在启动Nginx容器时使用--link
参数,但它暂时不是解决方案,因为它需要大量重写Node应用程序。
Any help is very much appreciated. 很感谢任何形式的帮助。
I would first suggest that you move your nodejs app into a container on the same network (bridge network) as nginx. 我首先建议您将nodejs应用程序移动到与nginx相同的网络(桥接网络)上的容器中。 It makes it easier/more secure (isolate node behind the nginx public proxy). 它使它更容易/更安全(隔离nginx公共代理后面的节点)。
Otherwise, run your nginx on the host network so that its localhost
is the same as the host. 否则,在主机网络上运行nginx,使其localhost
与主机相同。
docker run \
-d \
-p 80:80 \
-p 443:443 \
-p 8080:8080 \
-v /some/mounts:/to/some/mounts \
--name nginx \
--network host \
nginx:alpine
If you're still getting a bind: address already in use
, that's because more than one service is listening to that address. 如果您仍然在使用bind: address already in use
,那是因为有多个服务正在侦听该地址。 Use netstat or lsof (linux|osx) to figure out what process is listening to 8080. 使用netstat或lsof(linux | osx)来确定正在侦听8080的进程。
Some extra info on network settings. 有关网络设置的一些额外信息。 https://docs.docker.com/engine/reference/run/#/network-settings https://docs.docker.com/engine/reference/run/#/network-settings
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.