Daphne 301 redirect | ERR_TOO_MANY_REDIRECTS

hi,
I tried to deploy my django channels application on linux with nginx + daphne
I am getting ERR_TOO_MANY_REDIRECTS issue again and again i even tried --proxy-headers in daphne but still getting the error.

settings.py

# CSRF settings
CSRF_TRUSTED_ORIGINS = ["https://"+host for host in allowed_hosts_list]
CSRF_COOKIE_AGE = 60 * 60 * 5  # Default: 31449600 seconds (5 hour)
CSRF_COOKIE_DOMAIN = None  # Set the cookie domain
CSRF_COOKIE_HTTPONLY = False  # Default: False
CSRF_COOKIE_PATH = '/'  # Default: '/'
CSRF_COOKIE_SAMESITE = 'Lax'  # Default: 'Lax'
CSRF_COOKIE_SECURE = True  # Default: False
# HTTPS settings
SECURE_HSTS_SECONDS = 30  # Unit is seconds; *USE A SMALL VALUE FOR TESTING!* change to higher value for production
SECURE_HSTS_PRELOAD = True
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True

i have use this command

daphne -b 0.0.0.0 -p 8000 --proxy-headers myproject.asgi:application

Nginx config:

ssl_session_cache   shared:SSL:10m;
ssl_protocols TLSv1.2 TLSv1.3;

upstream backendserver {
    server backend:8000;
}
server {
    listen 443 ssl;
    http2 on;
    server_name mydomain.com;
    keepalive_timeout   70;

    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;


    location /static/ {
        alias /opt/djangofiles/staticfiles/;
    }

    location /media/ {
        alias /opt/djangofiles/media/;
    }

    location / {
        proxy_pass http://backendserver;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_redirect off;
        if (!-f $request_filename){
            proxy_pass http://backendserver;
            break;
        }
    }

    location /ws/ {
        proxy_pass http://backendserver;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        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;
    }

}

My server is working on my public ip but when using domain i get ERR_TOO_MANY_REDIRECTS.
I also have cloudflare on my domain but i don’t think that would be a problem

The Error was because of the Cloudflare.

I have Cloudflare on my domain and in my DNS i have Proxied enabled because of which the request was not directly going from domain to my server instead it was coming from Cloudflare server because of which nginx was unable to identify server name instead it was using the other conf file which had localhost as servername.

To fix this problem i disabled the Cloudflare Proxied option.

Now the request is going directly from domain to server and nginx is using the domain conf file instead of using localhost conf file and now it is working fine.

I had the same issue while being behind Cloudflare, i change the settings of SSL/TLS to full strict, since i am using TLS1.2 and above and i manage chipper suites as well.

1 Like