我已成功通过 certbot 将 SSL 安装到我的 Nginx Docker 容器中,但安装后,通过路由的所有流量都HTTPS
拒绝连接。
curl https://www.example.com
或者curl https://the_ip_of_server
curl: (7) Failed to connect to example.com port 443 after 9822 ms: Connection refused
~服务器上的端口 443 已打开(AWS Lisghtsail)
curl http://www.example.com
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.21.6</center>
</body>
</html>
但当 curl http://the_ip_of_server
<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.21.6</center>
</body>
</html>
这是nginx.conf
#Limit Concurrency
limit_conn_zone $binary_remote_addr zone=per_ip:10m;
server {
server_name example.com www.example.com;
location / {
proxy_pass http://flask:8080/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_intercept_errors on;
limit_conn per_ip 12;
}
error_page 404 /notfound.html;
location /notfound.html {
root /var/www/html;
internal;
}
error_page 500 502 503 504 /maintenance.html;
location /maintenance.html {
root /var/www/html;
internal;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = www.example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
if ($host = example.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80 default_server;
server_name example.com www.example.com;
return 404; # managed by Certbot
}
docker-compose.yml
version: '3.7'
services:
flask:
build: ./Flask App
container_name: flask
restart: always
environment:
- APP_NAME=Env
expose:
- 8080
nginx:
build: ./Nginx
container_name: nginx
restart: always
ports:
- "80:80"
答案1
为了路由https
流量,我应该将容器的端口 443 发布到主机(服务器)的端口 443。
添加- "443:443"
到 docker-compose 文件的 nginx 部分应该可以解决这个问题。