我一直在尝试使用ec2实例在django中使用nginx将所有请求自动传输到https协议,但是我无法这样做..这是我的nginx文件。 请向我提出问题。
nginx文件
operator<<
我也已将其添加到settings.py
settings.py
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;
}
}
gunicorn.service
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
SECURE_SSL_REDIRECT =True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
我还将我的hostedzone A类型记录设置为elb负载均衡器提供的别名。
我尝试了很多事情,例如返回https://sitename,并为两种协议创建一个不同的服务器块,但是它开始了两次请求之间的循环。这就是为什么我要发布我最初开始的代码的原因。我已经搜索了很多,但是在nginx和django方面都没有帮助,请帮助..我希望我所有的表单请求也仅通过https。
答案 0 :(得分:0)
您需要为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;
}
}