【问题标题】:Configure SSL in NGINX and Gunicorn in Django在 NGINX 中配置 SSL,在 Django 中配置 Gunicorn
【发布时间】:2020-05-06 11:35:57
【问题描述】:

我是部署应用程序的新手。我需要使用 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。谁能告诉我哪里出错了。

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

【问题讨论】:

  • 您可以通过https 访问您的网站吗?当你点击https://example.com 时会发生什么?
  • @n1rna 当我通过 https 访问时,它的连接不安全

标签: django ssl nginx deployment gunicorn


【解决方案1】:
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;
   }
}

【讨论】:

  • 感谢您的回复,会尽力让您知道!
  • 我已经更新了位置块,你能试试这个吗?同样在更新后尝试在隐身窗口中。
  • 我用上述配置更新了位置块,并在隐身窗口中尝试了现在我得到“域名重定向你太多次”。在浏览器中。 :(
  • Saroj Rai 你能帮帮我吗?
  • 已编辑,您可以试试这个吗?尝试清除缓存
猜你喜欢
  • 2021-02-02
  • 2016-04-20
  • 2018-11-25
  • 2017-02-23
  • 2014-04-30
  • 2020-12-28
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多