Nginx反向代理到主机中的应用 [英] Nginx reverse proxy to an app in host
问题描述
我有一个应用程序在Docker外部的端口5000上运行.我试图通过Docker compose在Nginx中运行反向代理,但无法与主机的端口5000进行通信.在我的docker-compose.yml文件中, :
I have an app that is running outside Docker on port 5000. I am trying to run a reverse proxy in nginx via Docker compose but am unable to communicate with the host's port 5000. In my docker-compose.yml file I have:
ports:
- 80:80
- 443:443
- 5000:5000
当我尝试运行此命令时,我得到:
When I try to run this I get:
ERROR: for nginx Cannot start service nginx: driver failed programming external connectivity on endpoint nginx (374026a0d34c8b6b789dcd82d6aee6c4684b3201258cfbd3fb18623c4101): Error starting userland proxy: listen tcp 0.0.0.0:5000: bind: address already in use
如果我将- 5000:5000
注释掉,我将得到:
If I comment out - 5000:5000
I get:
[error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream
如何从Docker nginx容器连接到主机中已经运行的应用程序?
How do I connect to an already running app in the Host from a Docker nginx container?
我的nginx.conf文件
My nginx.conf file
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
upstream mysite {
server 0.0.0.0:5000;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://mysite;
}
}
}
当我尝试卷曲localhost时,响应为502 Bad Gateway
.该应用程序本身和curl 127.0.0.1:5000可以从主机正常响应.
The response when I try to curl localhost is 502 Bad Gateway
. The app itself and curl 127.0.0.1:5000 responds fine from the host.
我还尝试了此处找到的解决方案但我得到nginx: [emerg] host not found in upstream "docker"
. Docker是我的主机的主机名.
EDIT 2:
I have also tried the solution found here but I get nginx: [emerg] host not found in upstream "docker"
. Docker is my host's hostname.
我的docker-compose.yml
EDIT 3: My docker-compose.yml
version: '3'
services:
simple:
build: ./simple
container_name: simple
ports:
- 80:80
- 443:443
我的Dockerfile:
My Dockerfile:
FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"]
我正在通过linux中的主机名"命令来获得计算机主机.
I am getting the computer host via the "hostname" command in linux.
推荐答案
问题出在0.0.0.0:5000.由于Nginx在docker内部运行,因此它尝试在docker机器内部查找此地址,但由于在docker内部没有任何运行在0.0.0.0:5000上的地址而失败.
所以为了解决这个问题
The problem lies with 0.0.0.0:5000. Since Nginx is running inside the docker, it tries to find this address inside docker machine but fails since there is nothing running on 0.0.0.0:5000 inside docker.
So in order to resolve this
- 您需要为其提供一个可以到达的地址.解决它需要你 首先在主机上以0.0.0.0:5000运行您的应用程序,即您应该能够从浏览器以0.0.0.0:5000打开您的应用程序.
- 查找您的IP地址.获得IP地址后,您应该能够 通过 ip_address:5000 打开应用程序.由于您的Docker和主机共享同一网络,因此也可以从docker访问此地址
- 现在,用此 ip_address:5000 替换Nginx conf文件中的 0.0.0.0:5000 .您将能够为您的应用程序提供服务
- You need to give it an address that it can reach. Solving it requires that you first run your application at 0.0.0.0:5000 on your host machine i.e you should be able to open your application at 0.0.0.0:5000 from your browser.
- Find your IP address. once you get your IP address you should be able to open you application through ip_address:5000. since your docker and host share the same network this address can be reached from docker also
- Now, replace the 0.0.0.0:5000 in your Nginx conf file with this ip_address:5000. you would be able to serve your application
这篇关于Nginx反向代理到主机中的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!