Python 使用Nginx Docker容器部署Django
情况:我想部署一个Django应用程序,我使用的工具是Nginx、Gunicorn,它们都在使用docker Desktop的docker容器中 问题:我可以使用docker的IP、机器的IP和环回IP在本地查看django应用程序。然而,当我试图从我的笔记本电脑(另一台连接在同一wifi上的机器)访问它时,我无法访问它 我的机器:Windows 10,我已经在Windows防火墙入站和出站中启用了端口80的公开 所采取的步骤:我已经尝试在我的机器上运行python-m http.server 80,它运行得非常好,因此我确信在我的Hyper-V docker desktop或nginx配置上可以做点什么 我的docker编写文件Python 使用Nginx Docker容器部署Django,python,django,docker,nginx,devops,Python,Django,Docker,Nginx,Devops,情况:我想部署一个Django应用程序,我使用的工具是Nginx、Gunicorn,它们都在使用docker Desktop的docker容器中 问题:我可以使用docker的IP、机器的IP和环回IP在本地查看django应用程序。然而,当我试图从我的笔记本电脑(另一台连接在同一wifi上的机器)访问它时,我无法访问它 我的机器:Windows 10,我已经在Windows防火墙入站和出站中启用了端口80的公开 所采取的步骤:我已经尝试在我的机器上运行python-m http.server
version: '3'
services:
dashboard:
build: .
volumes:
- .:/opt/services/dashboard/src
- static_volume:/opt/services/dashboard/src/static
networks: # <-- here
- nginx_network
nginx:
image: nginx:1.13
ports:
- 0.0.0.0:80:80
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
- static_volume:/opt/services/dashboard/src/static
depends_on:
- dashboard
networks: # <-- here
- nginx_network
networks: # <-- and here
nginx_network:
driver: bridge
volumes:
static_volume: # <-- declare the static volume
我的nginx配置文件
# first we declare our upstream server, which is our Gunicorn application
upstream dashboard_server {
# docker will automatically resolve this to the correct address
# because we use the same name as the service: "djangoapp"
server dashboard:80;
}
# now we declare our main server
server {
listen 80;
server_name localhost;
location / {
# everything is passed to Gunicorn
proxy_pass http://dashboard_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /opt/services/dashboard/src/static/;
}
}
这是我的文件夹结构的图像。
问题:我如何至少在通过与台式机相同的Wifi连接的笔记本电脑上查看它?我尝试使用我机器的IP访问它。重新启动路由器交换机,它工作正常。您说您无法从连接到同一网络的笔记本电脑访问它。你尝试了什么网址?您应该转到运行web应用程序的计算机的本地IP,比如192.168.0.X,其中X介于2和253(254?)之间。我编辑了问题部分,是的,我尝试使用IP访问它。顺便说一句,我正在使用docker desktop,我想也许我需要设置它的网络?当你从本地机器访问URL时,它工作吗?是的。我在想也许是Hyper-V的NAT有问题?这是可能的。但我还不确定。你为什么在Dockerfile中评论EXPOSE 80?仪表板服务需要暴露端口80否?
# first we declare our upstream server, which is our Gunicorn application
upstream dashboard_server {
# docker will automatically resolve this to the correct address
# because we use the same name as the service: "djangoapp"
server dashboard:80;
}
# now we declare our main server
server {
listen 80;
server_name localhost;
location / {
# everything is passed to Gunicorn
proxy_pass http://dashboard_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
location /static/ {
alias /opt/services/dashboard/src/static/;
}
}