45

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"
4
  • 25
    Did you set ALLOWED_HOSTS in your settings.py? docs.djangoproject.com/en/dev/ref/settings/#allowed-hosts Jan 28, 2014 at 7:29
  • Hum I think I did yes. I run the playbook with this command: ansible-playbook site.yml --extra-vars "source_location=$REMOTE_SOURCE_DIR hostname=$HOSTNAME" -u root where $HOSTNAME is 127.0.0.1. Then on the script from the tuto, the variable is set thanks to export DJANGO_ALLOWED_HOST={{hostname}} and ALLOWED_HOSTS = [os.environ.get('DJANGO_ALLOWED_HOST', '127.0.0.1'),]
    – Michael
    Jan 28, 2014 at 7:57
  • 1
    ok you were right it was the DJANGO_ALLOWED_HOST variable. I set ALLOWED_HOSTS = ['*'] in the settings file and it worked. But I dont understand what value I should put when using a local vagrant... ? 127.0.0.1 does not work... Thanks a lot.
    – Michael
    Jan 28, 2014 at 16:13
  • 2
    Underscores in the domain name also triggers this error. Not for Django 1.3 and 1.4, but at least for 1.6.1. See code.djangoproject.com/ticket/20264 why this behaviour is correct. Feb 6, 2014 at 10:45

3 Answers 3

90

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/

5
  • 1
    or ALLOWED_HOSTS = ("*",) and start apache (service apache2 restart)
    – Halil
    Sep 18, 2014 at 3:59
  • 4
    @HalilKaskavalci : Note that the question specifies gunicorn/nginx, not Apache2. Personally, I would recommend avoiding using ALLOWED_HOSTS=("*",). Nov 10, 2014 at 18:28
  • Oh, my bad. I hadn't noticed that one. Thanks for correcting.
    – Halil
    Nov 10, 2014 at 18:37
  • This solved my problem. I was getting BAD REQUEST 400. Thank you @Andrey Zarubin Nov 1, 2015 at 8:50
  • Also check for trailing whitespace which can cause the error
    – theTypan
    Jun 27, 2019 at 7:19
7

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.

0
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.