Hi, first time attempting to deploy on the cloud and in “Production”. My application was working correctly until I attempted to implement HTTPS. I was able to access the admin panel without issue make and make one post, now I am unable access the admin panel.
I was browsing other similar issues and most of the fixes consisted of adding CSRF_TRUSTED_ORIGINS.
Not in my case.
I think I am stuck with my novice understanding of the security of HTTPS: The POST request from nginx doesn’t have the correct headers to satisfy Djangos requirements to access the resources being requested?
I have added my IP/Domain to the CSRF list, rebuilt the images and cleared the browsing history.
I am out of ideas on what I need to try next.
I have a standard html front end.
Cookie is present when inspecting: csrftoken l5nlxaw### www.django-blog-jb.com Lax
Forbidden (403)
CSRF verification failed. Request aborted.
Origin checking failed -
here are some web container logs and nginx container logs:
80.64.30.78 - - [21/Jan/2025:19:32:33 +0000] "GET /remote/login HTTP/1.1" 403 555 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.203" "-"
80.64.30.78 - - [21/Jan/2025:19:32:34 +0000] "GET /login HTTP/1.1" 400 59546 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.203" "-"
154.213.184.16 - - [21/Jan/2025:19:33:51 +0000] "GET / HTTP/1.1" 400 59405 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36" "-"
76.34.237.147 - - [21/Jan/2025:19:35:51 +0000] "GET / HTTP/1.1" 200 4468 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
76.34.237.147 - - [21/Jan/2025:19:35:56 +0000] "GET / HTTP/1.1" 200 4468 "https://www.django-blog-jb.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
76.34.237.147 - - [21/Jan/2025:19:35:56 +0000] "GET /about/ HTTP/1.1" 200 5082 "https://www.django-blog-jb.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
76.34.237.147 - - [21/Jan/2025:19:35:57 +0000] "GET /sitefeatures/ HTTP/1.1" 200 8304 "https://www.django-blog-jb.com/about/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
76.34.237.147 - - [21/Jan/2025:19:35:57 +0000] "GET /my_blog HTTP/1.1" 200 4009 "https://www.django-blog-jb.com/sitefeatures/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
76.34.237.147 - - [21/Jan/2025:19:35:59 +0000] "GET / HTTP/1.1" 200 4468 "https://www.django-blog-jb.com/my_blog" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
76.34.237.147 - - [21/Jan/2025:19:36:16 +0000] "GET /admin HTTP/1.1" 301 5 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
76.34.237.147 - - [21/Jan/2025:19:36:17 +0000] "GET /admin/ HTTP/1.1" 302 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
76.34.237.147 - - [21/Jan/2025:19:36:17 +0000] "GET /admin/login/?next=/admin/ HTTP/1.1" 200 4160 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
76.34.237.147 - - [21/Jan/2025:19:36:20 +0000] "POST /admin/login/?next=/admin/ HTTP/1.1" 403 2563 "https://www.django-blog-jb.com/admin/login/?next=/admin/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36" "-"
Invalid HTTP_HOST header: '3.15.27.43'. You may need to add '3.15.27.43' to ALLOWED_HOSTS.
my_blog-web-1 | Traceback (most recent call last):
my_blog-web-1 | File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
my_blog-web-1 | response = get_response(request)
my_blog-web-1 | ^^^^^^^^^^^^^^^^^^^^^
my_blog-web-1 | File "/usr/local/lib/python3.11/site-packages/django/utils/deprecation.py", line 128, in __call__
my_blog-web-1 | response = self.process_request(request)
my_blog-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my_blog-web-1 | File "/usr/local/lib/python3.11/site-packages/django/middleware/common.py", line 48, in process_request
my_blog-web-1 | host = request.get_host()
my_blog-web-1 | ^^^^^^^^^^^^^^^^^^
my_blog-web-1 | File "/usr/local/lib/python3.11/site-packages/django/http/request.py", line 151, in get_host
my_blog-web-1 | raise DisallowedHost(msg)
my_blog-web-1 | django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: '3.15.27.43'. You may need to add '3.15.27.43' to ALLOWED_HOSTS.
my_blog-web-1 | Bad Request: /login
my_blog-web-1 | Invalid HTTP_HOST header: '3.15.27.43'. You may need to add '3.15.27.43' to ALLOWED_HOSTS.
my_blog-web-1 | Traceback (most recent call last):
my_blog-web-1 | File "/usr/local/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
my_blog-web-1 | response = get_response(request)
my_blog-web-1 | ^^^^^^^^^^^^^^^^^^^^^
my_blog-web-1 | File "/usr/local/lib/python3.11/site-packages/django/utils/deprecation.py", line 128, in __call__
my_blog-web-1 | response = self.process_request(request)
my_blog-web-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
my_blog-web-1 | File "/usr/local/lib/python3.11/site-packages/django/middleware/common.py", line 48, in process_request
my_blog-web-1 | host = request.get_host()
my_blog-web-1 | ^^^^^^^^^^^^^^^^^^
my_blog-web-1 | File "/usr/local/lib/python3.11/site-packages/django/http/request.py", line 151, in get_host
my_blog-web-1 | raise DisallowedHost(msg)
my_blog-web-1 | django.core.exceptions.DisallowedHost: Invalid HTTP_HOST header: '3.15.27.43'. You may need to add '3.15.27.43' to ALLOWED_HOSTS.
my_blog-web-1 | Bad Request: /
my_blog-web-1 | Forbidden (Origin checking failed - https://www.django-blog-jb.com does not match any trusted origins.): /admin/login/
ALLOWED_HOSTS = [
'www.django-blog-jb.com',
'django-blog-jb.com',
'3.15.27.43,'
'localhost',
]
CSRF_TRUSTED_ORIGINS = [
"https://www.django-blog-jb.com",
"https://django-blog-jb.com",
"https://3.15.27.43",
]
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True
nginx
# Define the limit request zone at the HTTP level
limit_req_zone $binary_remote_addr zone=one:10m rate=5r/s;
server {
listen 80;
server_name www.django-blog-jb.com django-blog-jb.com;
# Redirect HTTP traffic to HTTPS
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name www.django-blog-jb.com django-blog-jb.com;
# SSL certificates
ssl_certificate /etc/letsencrypt/live/www.django-blog-jb.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.django-blog-jb.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
# Serve static files
location /static/ {
alias /app/staticfiles/;
access_log /var/log/nginx/static_access.log;
error_log /var/log/nginx/static_error.log debug;
}
# Proxy pass to the Gunicorn app
location / {
proxy_pass http://web:8000;
proxy_set_header Host $host;
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;
# Apply rate limiting to all requests
limit_req zone=one burst=10 nodelay;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# Block known malicious traffic
location ~* (\.env|/remote/login) {
return 403;
}
}