Django + Gunicorn + Nginx: invalid request (400) in Debug = True

I am trying to start my server with Django, nginx and gunicorn. Everything went well on the development server. But on the production server, the machine gun always returns “Bad Request” (400).

I know that I need to set a variable ALLOWED_HOSTS, and I did it. I tried the correct domain, asterisk, or even set DEBUG to True. But still, it is always Bad Request (400).

Here is my nginx-config:

server {
    listen 80;

    location /static {
        alias /home/username/sites/sub.domain.example.com/static;
    }

    location / {
        proxy_set_header Host $http_host;
        proxy_pass http://localhost:8000;
    }
}

My wsgi-prod.pyfile:

import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "app.settings_prod")

from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

File settings_prod.py(shortened):

DEBUG = False
ALLOWED_HOSTS=["*"]

I start shooting as follows (with virtualenv):

gunicorn --bind 127.0.0.1:8000 app.wsgi_prod:application

When I start the server from manage.py runserver --settings=app.settings_prod, the site is accessible. the gunicorn error log shows nothing, and only 400 is displayed in the access log. Static content really works.

+4
2

Nginx Gunicorn :

proxy_set_header        Host            $host;

, () , IP- :

proxy_set_header        X-Real-IP       $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;

, Nginx, . , .

EDIT: , :

server_name     your_domain.com www.your_domain.com

, ​​ ( ):

os.environ['DJANGO_SETTINGS_MODULE'] = "app.settings_prod" 
+3

:

location / {
    proxy_pass http://127.0.0.1:8081;
    proxy_set_header X-Forwarded-Host $server_name;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
}
0

All Articles