nginx将http重定向到docker容器中的https连接被拒绝



我有一个从HTTP流量到HTTPS流量的工作重定向。该环境由docker容器中的烧瓶应用程序组成,该容器正通过NGINX docker容器路由。下面是nginx.conf文件。在运行docker-compose up之后,我能够使容器处于活动状态。运行完curl localhost之后,我得到了一个301 Moved Permanently。但是,当运行curl https://localhost时,我得到的是curl: (7) Failed to connect to localhost port 443: Connection refused。我在macOS Big Sur上检查了我的本地计算机网络设置,防火墙已经关闭(任何流量都应该被允许进入(。我不知道我还需要做些什么才能让它发挥作用。我还在nginx容器的docker compose文件中公开了端口443。任何建议都会有帮助。

NGINX.conf

http {

upstream flask {
server app:8000;
}
server {
listen 80;
server_name localhost;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name localhost;
ssl_certificate /etc/nginx/nginx/files/localhost.crt;
ssl_certificate_key /etc/nginx/nginx/files/localhost.key;
location / {
proxy_pass http://flask;
proxy_set_header Host "localhost";
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
}

Docker Compose

version: "3"
services:
app:
container_name: app
command: gunicorn --bind 0.0.0.0:8000 --workers 2 "app.server:app"
volumes:
- ./:/var/www
build: app
ports:
- 8000:8000
networks:
MyNetwork:
aliases:
- flask
nginx:
container_name: nginx
build: nginx
volumes:
- ./:/var/www
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
ports:
- 80:80
expose:
- "443"
networks:
- MyNetwork
networks:
MyNetwork:

首先,您应该将端口443添加到docker-compose中,如下所示:

ports:
- 80:80
- 443:443

然后你应该音量/etc/nginx/nginx/files:

volumes:
- ./:/var/www
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/nginx/files:/etc/nginx/nginx/files

最新更新