Bad Request 400 in Nginx When Running Django using Gunicorn

Have a RSS feed from your website? Add it here Browse Feeds

Bad Request 400 in Nginx When Running Django using Gunicorn

From Bastaki Blog Posts via Bastaki Blogs | Published February 19, 2025, 4:56 p.m. by Administrator

The "Bad Request 400" error in Nginx when running Django with Gunicorn usually indicates an issue with the HTTP request that Nginx is forwarding to Gunicorn. Here's a breakdown of common causes and how to troubleshoot them:

1. ALLOWED_HOSTS Setting in Django:

  • Problem: Django's ALLOWED_HOSTS setting is a security measure that prevents requests with unknown host headers from being processed. If Nginx forwards a request with a host that's not listed in ALLOWED_HOSTS, Django will return a 400 error.

  • Solution:

    • Open your Django project's settings.py file.
    • Add your domain name or IP address to the ALLOWED_HOSTS list. For example:
    Python
     
    ALLOWED_HOSTS = ['yourdomain.com', '127.0.0.1', 'localhost']
    
    • If you're unsure

...
Read Original Article Back to Posts