繁体   English   中英

在 Django 中的 NGINX 和 Gunicorn 中配置 SSL

[英]Configure SSL in NGINX and Gunicorn in Django

我是部署应用程序的新手。 我需要使用 NGINX 和 Gunicorn 托管 Django 应用程序。

我能够使用 NGINX 和 Gunicorn 成功运行 Django 应用程序。 现在我必须使用 HTTPS 配置 NGINX。

我浏览了一些关于使用 NGINX 和 Gunicorn 配置 SSL 的帖子。 但仍然无法为我的应用程序配置 HTTPS。

下面是NGINX配置文件

server {
listen 80;
listen 443 ssl;
server_name domain_name;
ssl_certificate /etc/ssl/https_certificate.cer;
ssl_certificate_key /etc/ssl/https_key.key;

location / {
    proxy_set_header X-Forwarded-Proto https;
    proxy_pass http://0.0.0.0:8000;
}

location /static/ {
    autoindex on;
    alias PROJECT_STATIC_FILES_DIR;
}

location = /favicon.ico {
    alias PATH_TO_FAVICON/favicon.ico;
}
}

我已经重新启动了 NGINX 服务,但我的 Web 应用程序仍然将其显示为 HTTP。 但不是 HTTPS。 谁能告诉我哪里出错了。

请原谅,如果这是一个重复的问题。

server {
    listen 80;
    listen 443 default_server ssl;

    # other directives
}

或者

您可以创建两个服务器块。 将所有 HTTP 请求重定向到 HTTPS。

server {
    listen 80;
    listen [::]:80;
    server_name domain_name;

    return 301 https://domain_name$request_uri;
}

server {
    listen 443 ssl default_server;
    server_name domain_name;
    ssl_certificate /etc/ssl/https_certificate.cer;
    ssl_certificate_key /etc/ssl/https_key.key;

    location / {
        proxy_pass http://0.0.0.0:8000;
        proxy_set_header Host $http_host;
        proxy_connect_timeout 300;
        proxy_set_header Connection "";
        proxy_send_timeout 300;
        proxy_read_timeout 300;
        send_timeout 300;
        proxy_set_header X-Forwarded-Proto $scheme;


      }

    location /static/ {
        autoindex on;
        alias PROJECT_STATIC_FILES_DIR;
      }

   location = /favicon.ico {
      alias PATH_TO_FAVICON/favicon.ico;
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM