首页 > 解决方案 > 使用自签名证书为 SSL 重新配置 Nginx 时出现问题

问题描述

我在 Digital Ocean 上有一个带有 Ubuntu 18.04、Nginx、Gunicorn、Django 和一个测试 Web 应用程序的 VPS,所有配置(ufw)都可以使用 http:80。一切正常。教程

现在我修改文件/sites-available/LibrosWeb以允许使用自签名证书的 SSL 流量,因为我没有域。教程。结果“错误 502 错误网关”

这是适用于 http: 80 的初始代码:

server{
    #Configuracion http

    listen 80;
    listen [::]:80;
    server_name 15.15.15.15;

    location = /favicon.ico { access_log off; log_not_found off; }
    location  /robots.txt {
        alias /var/www/LibrosWeb/robots.txt ;
    }        
    location /static/ {
        root /home/gela/LibrosWeb;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

这是允许 SSL 的代码(错误 502):

server{
    #Configuracion SSL

    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name 15.15.15.15;
    include snippets/self-signed.conf;
    include snippets/ssl-params.conf;

    location = /favicon.ico { access_log off; log_not_found off; }
    location  /robots.txt {
        alias /var/www/LibrosWeb/robots.txt ;
    }
    location /static/ {
        root /home/gela/LibrosWeb;
    }

    location / {
        include proxy_params;
        proxy_pass https://unix:/run/gunicorn.sock;
    }
}

server{
    #Configuracion http

    listen 80;
    listen [::]:80;
    server_name 15.15.15.15;
    return 302 https://15.15.15.15$request_uri;
}

UFW配置为:

80,443/tcp (Nginx Full)    ALLOW IN    Anywhere
80,443/tcp (Nginx Full (v6)) ALLOW IN    Anywhere (v6)

/etc/nginx/snippets/self-signed.conf/etc/nginx/snippets/ssl-params.conf文件与教程中的相同。

我已经测试了两天的配置,我能得到的最多的是我工作到了一半,也就是说,我可以显示 django 的默认页面,但不能显示我的应用程序的页面,如果我输入这样的代码:

server{
    #Configuracion http

    listen 80;
    listen [::]:80;
    server_name 15.15.15.15;
    return 302 https://15.15.15.15$request_uri;

    location = /favicon.ico { access_log off; log_not_found off; }
    location  /robots.txt {
        alias /var/www/LibrosWeb/robots.txt ;
    }
    location /static/ {
        root /home/gela/LibrosWeb;
    }
}

server{
    #Configuracion SSL

    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name 15.15.15.15;
    include snippets/self-signed.conf;
    include snippets/ssl-params.conf;

    location / {
       include proxy_params;
       proxy_pass https://unix:/run/gunicorn.sock;
    }
}

有什么问题,或者缺少什么?

标签: djangosslnginx

解决方案


我想我受苦的日子已经结束了。在阅读了数百条日志后,我发现了问题所在。将 Whitenoise 更新到 4.0,您必须更改配置的形状,导致使用我的旧配置,gunicorn 服务会抛出错误。其余的都很好。

http://whitenoise.evans.io/en/stable/django.html#django-middleware

谢谢您的帮助。再会。


推荐阅读