【问题标题】:HTTPS protocol not working with django and nginx on ec2 instanceHTTPS 协议不适用于 ec2 实例上的 django 和 nginx
【发布时间】:2019-07-16 16:42:48
【问题描述】:

我一直在尝试在 ec2 实例中使用 django 中的 nginx 将所有请求自动传输到 https 协议,但我无法这样做.. 这是我的 nginx 文件.. 请向我提出问题。

nginx 文件

server{
listen 443 ssl;
server_name www.priyamarya.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
    root /home/ubuntu/project/aryapriyam/;
}

location / {
    include proxy_params;
    proxy_pass http://unix:/home/ubuntu/project/aryapriyam/project.sock;
    }
}
server{
listen 80;
server_name priyamarya.com;
return 301 https://www.priyamarya.com;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
    root /home/ubuntu/project/aryapriyam/;
}

location / {
    include proxy_params;
    proxy_pass http://unix:/home/ubuntu/project/aryapriyam/project.sock;
    }
}

我也在settings.py中添加了这个

settings.py

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT =True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

gunicorn.service

[Unit]
Description=gunicorn daemon
After=network.target

[Service]
User=ubuntu
Group=www-data
WorkingDirectory=/home/ubuntu/project/aryapriyam
ExecStart=/home/ubuntu/project/venv/bin/gunicorn --access-logfile - -- 
workers 3 --chdir /home/ubuntu/project/aryapriyam/ --bind 
unix:/home/ubuntu/project/aryapriyam/project.sock 
project.wsgi:application

[Install]
WantedBy=multi-user.target

我还将我的 hostszone A 类型记录集设置为 elb 负载均衡器提供的别名。

我已经尝试了很多事情,比如返回 https://sitename ,并为这两种协议创建不同的服务器块,但它会在请求之间启动一个循环。这就是为什么我要发布我最初开始的代码。我已经搜索了很多,但对于 nginx 和 django 都没有任何帮助,请帮助.. 我希望我的所有表单请求也只能通过 https。

【问题讨论】:

  • 当您为每个 80 和 443 配置创建 2 个服务器块时,您会得到什么类型的循环? hhp-hhtps-http-https , https-https-https ? (您可以通过浏览器的开发者工具的网络选项卡查看它(通常为F12)。
  • 我用两个块编辑了这个问题。它现在可以工作,但它显示的 nginx 欢迎页面不是我的 index.html
  • 早些时候我得到了 https-https-https 循环。所以这次我尝试在每个块上使用一个不同的服务器名称,它工作!但它显示 nginx 欢迎页面...
  • 我仍然坚持这个请帮助....@flaixman

标签: django amazon-web-services nginx amazon-ec2 https


【解决方案1】:

您需要为 ssl 添加其他 服务器块 并为 ssl 使用以下配置

此配置还将http 请求重定向到https即 ssl 端口 443

server {
    listen 80;
    server_name testing.com;
    return 301 https://testing.com;
    location = /favicon.ico { access_log off; log_not_found off; }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/ubuntu/sample_project/sample_project.sock;
    }
}

server {
   listen 443 ssl;
   listen [::]:443 ssl;
   server_name testing.com;

   ssl on;
   ssl_certificate /etc/nginx/ssl/nginx.crt;
   ssl_certificate_key /etc/nginx/ssl/nginx.key;
   location /static/ {
           root /home/ubuntu/sample_project;
      }
   location / {
         include proxy_params;
         proxy_pass http://unix:/home/ubuntu/sample_project/sample_project.sock;
       }

}

【讨论】:

  • amazon 不提供 ssl 证书或密钥。所以我不知道在 ssl_certificate 和密钥部分放什么。如果我在每个块中使用其中一个域名但它显示 nginx 欢迎页面,它的工作原理..
  • 您能分享一下您的表单操作的 sn-p 吗?
猜你喜欢
  • 1970-01-01
  • 2020-01-31
  • 2016-05-30
  • 2019-07-08
  • 2019-01-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多