我在 Gunicorn
应用程序服务器上运行 EC2 Ubuntu 18.04 服务器和 Python Flask 框架,Nginx
反向代理监听端口 80 和 443。我添加了 LetsEncrypt
使用 certbot 的域。
该网站在没有 ssl 的情况下工作正常。在 Nginx
上使用 LetsEncrypt
ssl 配置,服务器无法加载页面。
我之前的 supervisor
和 Nginx
配置如下,不支持 ssl,Nginx
与 gunicorn
一起使用没有问题。
server {
listen 80;
server_name example.com www.example.com;
location /static {
alias /home/ubuntu/myapp-backend/myapp/static;
}
location / {
proxy_pass http://localhost:8000;
include /etc/nginx/proxy_params;
proxy_redirect off;
}
}
[program:myapp]
directory=/home/ubuntu/myapp-backend
command=/home/ubuntu/myapp-backend/venv/bin/gunicorn -w 5 run:app
user=ubuntu
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
stderr_logfile=/var/log/myapp/myapp.err.log
stdout_logfile=/var/log/myapp/myapp.out.log
当我更改 Nginx
配置以包括 LetsEncrypt
支持,同时监听端口 80 和 443,网站没有显示。它显示无限期的 301 重定向请求.
server {
listen 80;
server_name example.com www.example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
server_name www.example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
location /static {
alias /home/ubuntu/myapp-backend/myapp/static;
}
location / {
proxy_pass http://localhost:8000;
include /etc/nginx/proxy_params;
proxy_redirect off;
}
}
当我加载站点 example.com
时,它重定向到 https:\\www.example.com
。然而,网站主页不加载或显示来自服务器/Nginx 的任何响应。当我登录到服务器并运行 curl -v localhost:8000
时,gunicorn 工作正常。
curl -v localhost:8000
* Rebuilt URL to: localhost:8000/
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: localhost:8000
> User-Agent: curl/7.58.0
> Accept: */*
>
< HTTP/1.1 500 INTERNAL SERVER ERROR
< Server: gunicorn/19.9.0
< Date: Sat, 28 Sep 2019 14:14:47 GMT
< Connection: close
< Content-Type: text/html; charset=utf-8
< Content-Length: 27911
<
<!doctype html>
...
之前有Stackoverflow question在 gunicorn
上使用 ssl parameters 通过 ssl可以将其添加到 supervisor
配置中。
我相信错误必须与端口 443 上的 Nginx
配置或 Gunicorn
supervisor
配置信息有关。如果你能看一看,我将不胜感激。
最佳答案
你有太多的server
block
您的 SSL 配置中的第二个 block 捕获到 443
的连接并将它们重定向到 https://
URL。从而导致死循环:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/www.example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.example.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
return 301 https://www.example.com$request_uri;
}
只有当用户访问端口 80
上的服务器时,才应返回此 301
。这就是此配置文件中的第一个 server
block 实际执行的操作。
删除整个 block ,它应该可以工作。您已经将第三个服务器 block 配置为(正确地)捕获到 443
的流量并将其代理到 gunicorn。
There was earlier Stackoverflow question on gunicorn over ssl with ssl parameters which can be added to supervisor config. I believe the error must be with on Nginx configuration.
您在 nginx 中终止 SSL,然后在纯 HTTP 中代理到 gunicorn(这是涉及 nginx 的正确操作方式)。您在那里链接的问题是关于向 gunicorn 添加 native SSL 支持,以便 gunicorn 终止 SSL。仅当您的基于 Internet 的用户直接连接到 gunicorn 服务器时才需要这样做。
关于ssl - Nginx 反向代理无法通过 LetsEncrypt SSL 连接与 Gunicorn 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58147528/