Michael
Michael

Reputation: 8758

Bad request 400: nginx / gunicorn

I have followed this tutorial: http://blog.wercker.com/2013/11/25/django-16-part3.html and I am just trying to make it work locally with Vagrant for now. I am not trying to use Wercker.

After everything is installed, I try to access the website but I get a Bad Request (400) error every time. I do not know if that is due to a problem in nginx or in gunicorn.

They both have a log entry so at least I know that the request goes all the way through gunicorn and is not stopped at the nginx level.

Where is the problem located? Gunicorn? nginx?

Here are the logs of gunicorn and nginx.

I see that the favicon is missing but that only should not stop the page from being displayed right?

Gunicorn:

 >>> cat /var/local/sites/hellocities/run/gunicorn.error.log
10.0.0.1 - - [28/Jan/2014:07:05:16] "GET / HTTP/1.0" 400 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:09:43] "GET / HTTP/1.0" 400 - "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"

Nginx:

>>> cat /var/log/nginx/hellocities-access.log
10.0.0.1 - - [28/Jan/2014:07:05:16 +0000] "GET / HTTP/1.1" 400 37 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:05:20 +0000] "GET /favicon.ico HTTP/1.1" 404 200 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:09:43 +0000] "GET / HTTP/1.1" 400 37 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"
10.0.0.1 - - [28/Jan/2014:07:09:44 +0000] "GET /favicon.ico HTTP/1.1" 404 200 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.76 Safari/537.36"

>>> cat /var/log/nginx/hellocities-error.log
2014/01/28 07:05:20 [error] 13886#0: *1 open() "/var/local/sites/hellocities/static/favicon.ico" failed (2: No such file or directory), client: 10.0.0.1, server: _, request: "GET /favicon.ico HTTP/1.1", host: "10.0.0.200"
2014/01/28 07:09:44 [error] 13886#0: *3 open() "/var/local/sites/hellocities/static/favicon.ico" failed (2: No such file or directory), client: 10.0.0.1, server: _, request: "GET /favicon.ico HTTP/1.1", host: "10.0.0.200"

Upvotes: 46

Views: 47725

Answers (3)

gitaarik
gitaarik

Reputation: 46300

I ran into this issue. It was because I forgot to add the proxy_set_header settings in the nginx config:

proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

So Django didn't see the original hostname that was requested, so it didn't match with what was in ALLOWED_HOSTS. Then it gave back the 400 response.

After adding this to my nginx config (at the spot where you do the proxy_pass to Gunicorn) and then restarting nginx, it worked.

More info: https://docs.gunicorn.org/en/stable/deploy.html#nginx-configuration

Upvotes: 1

Andrii Zarubin
Andrii Zarubin

Reputation: 2255

I had the same problem and adding ALLOWED_HOSTS = ("yourdomain.com",) to settings fixed it.

UPDATE: there few other possibilities:

  1. Nginx (or whatever web server you use) doesn't pass the $host variable to the app
  2. Host contains underscores

See details: https://blog.anvileight.com/posts/how-to-fix-bad-request-400-in-django/

Upvotes: 92

Emile Bergeron
Emile Bergeron

Reputation: 17430

As I was having the same issue (400 error code when trying to share with vagrant share), I stumble upon this question. The answer and comments are right, as the obvious solution is to set ALLOWED_HOSTS list, but I was already setting it correctly (I thought).

I can't speak for nginx as I'm running this on apache2, but here's what solved the issue:

  1. Take a look at the ALLOWED_HOSTS doc to find what's best for your case.

  2. With vagrant, you might find it useful to accept all the vagrantshare.com subdomain, so just add '.vagrantshare.com' (notice the dot) to the ALLOWED_HOSTS list.

  3. Not sure if it is really necessary, but I changed the modified date of the wsgi.py file

    touch wsgi.py
    
  4. As I'm using apache2, I needed to restart the service.

    sudo service apache2 restart
    

And then it worked.

Upvotes: 7

Related Questions