最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

nginx - Django Returning http instead of HTTPS in Prod - Stack Overflow

programmeradmin4浏览0评论

I deployed a Django app in an Ec2 with gunicorn and nginx but links generated by django are http instead of HTTPS. I have included the necessary settings. For example, when I have paginations, I got

count": 12,
"next": "/?page=2",
"previous": null,

Instead of

count": 12,
"next": "/?page=2",
"previous": null,

All the links are like this, although they got redirected when you click on them or copy them in the browser but it is showing a funny reaction in the frontend

My DNS is being managed by cloudfare and everything seem to be fine there.

#In settings 

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
USE_X_FORWARDED_HOST = True
#My nginx file
server {
    listen 80;
    server_name fronend.example;

    location / {
        autoindex on;
        root /home/ubuntu/frontend/dist/frontend;
        try_files $uri $uri/ /index.html;
    }
}

# Backend Nginx Config
server {
    listen 80;
    server_name backend.example;

    location = /favicon.ico {
        access_log off;
        log_not_found off;
    }

    location /staticfiles/ {
        alias /home/ubuntu/backend/static;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
#        proxy_set_header Host $host;    #I commented these out when I was troubleshootng
#        proxy_set_header X-Real-IP $remote_addr;
#        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/backend.example/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/backend.example/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = backend.example) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    server_name backend.example;
    return 404; # managed by Certbot
}

Some directives are added by Certbot when I registered for the SSL certificate. Although, nothing changed before and after the ssl certification.

It should be noted that this gives a different reactions when deployed on Render (works normally and shows HTTPS) even without the settings USE_X_FORWARDED_HOST and SECURE_PROXY_SSL_HEADER in the django settings

发布评论

评论列表(0)

  1. 暂无评论