Django Returning http instead of HTTPS in Prod
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": "http://my.example.com/article/?page=2",
"previous": null,
Instead of
count": 12,
"next": "https://example.com/journal/?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.com;
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.com;
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.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/backend.example.com/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.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name backend.example.com;
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