I’m trying to run Django on Nginx + Unicorn (Debian 7) as described in this tutorial: https://www.digitalocean.com/community/tutorials/how-to-deploy-a-local-django-app-to-a-vps
This is how my Nginx looks like:
server{
server_name 146.185.185.212;
access_log off;
location /static/ {
alias opt/myenv/static/;
}
location / {
proxy_pass http://127.0.0.1:8001;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM N$
}
}
This is how I start Gunicorn:
gunicorn --bind 127.0.0.1:8001 projname.wsgi:application
The problem is that i keep getting : Bad Request (400) in the browser.
Does anyone have any clue on how to fix this?
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
Click below to sign up and get $100 of credit to try our products over 60 days!
Check out my answer here: https://www.digitalocean.com/community/questions/400-error-bad-request-when-visiting-domain-name-ip-address-loads-fine
Here’s how you fix it:
You must first make sure you have
proxy_set_header Host $host;
& the server-IP:8001 in theproxy_pass
instead of 127.0.0.1:8001 under the location directive in your nginx project config file.Open your Django project settings.py in /opt/projectname/projectname/projectname.setting.py or /opt/projectname/projectname/projectname/projectname/settings.py (notice added project directory) and set the
ALLOWED_HOSTS
to [“your_server_ip”]. Example:ALLOWED_HOSTS = ["25.22.12.21"]
If you have started Gunicorn, then while in virtualenv, type
ps ax|grep gunicorn
, It will give you a list of gunicorn processes, on the left you will see the process id of these processes, you may kill each of them my typingkill xxxx
and then make sure Gunicorn is not running by typingbg
, It will not return the following “Job 1 in already in background”.Finally,
service nginx restart
and it should work.Here are my files for example:
/etc/nginx/sites-available/projectname (nginx project config file)
/opt/projectname/projectname/projectname/settings.py (project settings.py)
Hope this helps, Basit
Nope, same error. This is how my nginx looks like now:
This is my ALLOWED_HOSTS in settings.py:
Try adding
to your nginx config and restarting nginx. Does that fix it?
Make sure ALLOWED_HOSTS contains your domain name.