Bad Request (400) after making supervisor restart - django

After restarting my django app: supervisorctl restart [process] I've got Bad Request(400) error when visiting my site. The app is under nginx with gunicorn and supervisor.
I remember to had the same problem some time ago and restarting supervisor from some specific folder on the server had helped. I've tried to restart supervisor from different locations, however it doesn't help.
nginx-error.log
2014/04/08 06:45:23 [error] 9635#0: *9 connect() to
unix:/webapps/filmyposlowie/run/gunicorn.sock failed (111: Connection
refused) while connecting to upstream, client: 78.10.91.212, server:
filmyposlowie.pl, request: "GET / HTTP/1.1", upstream:
"http://unix:/webapps/filmyposlowie/run/gunicorn.sock:/", host:
"filmyposlowie.pl"

I have the same issue, I solved it by adding these two lines in my nginx's server config file
proxy_set_header Host $http_host;
proxy_redirect off;

When you change DEBUG setting to False, you must also set ALLOWED_HOSTS.
You may simply make Django accept requests by allowing localhost:
ALLOWED_HOSTS = ['127.0.0.1']
This will work if gunicorn is running on same machine and it is bound to 127.0.0.1
Ref: DEBUG
Finally, if DEBUG is False, you also need to properly set the
ALLOWED_HOSTS setting. Failing to do so will result in all requests
being returned as “Bad Request (400)”.

I've changed DEBUG to True in settings.py of my django project and it works fine now.

Related

Django on Digitalocean with Python and Gunicorn throws 500 error no matter the Debug settings

I'm REALLY confused. Ive been using the same settings for years and they have worked great... until today.
Setup:
Django on Gunicorn with NGINX on Digitalocean
Python 3
Anytime I go to a URL it looks liek the 500 error is being triggered and then the 500.html template is displayed. But this is happening regardless of the Debug settings. Additionally, there is nothing that is being written to the Gunicorn errors. The only error I'm getting is a 404 on the template directory.
As a sanity check, here is some sample configurations that I'm using.
NGINX Location:
root /home/myname/myrepoproject;
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/myname/myrepoproject/myproject/app/templates;
}
NGINX Error:
2018/03/26 17:09:14 [crit] 3267#3267: *35 connect() to unix:/home/myname/myproject/myproject.sock failed (2: No such file or directory) while connecting to upstream, client: XXX.XXX.XXX.XXX, server: ZZZ.ZZZ.ZZZ.ZZZ, request: "GET /500.html/ HTTP/1.1", upstream: "http://unix:/home/myname/myproject/myproject.sock:/500.html/", host: "arecord.domain.name"
Gunicorn ExecStart:
ExecStart=/home/myname/.virtualenvs/myve/bin/gunicorn --error-logfile ~/logs/gunicorn/gunicorn-errors.log --workers 3 --bind unix:/home/myname/myve/myproject.sock production
The static and upload directories work.
Any troubleshooting ideas welcome.
From the errors you posted it looks like your nginx and gunicorn configs do not have the same location set of the socket so they fail to communicate.
Nginx is looking at unix:/home/myname/myproject/myproject.sock but your gunicorn command line says --bind unix:/home/myname/myve/myproject.sock.
Make them match and it should work.

nginx, uwsgi, DJango, 502 when DEBUG=False, "upstream prematurely closed connection"

I have a working nginx production server running a Django app, using uwsgi (set up with this tutorial).
nginx and uwsgi are communicating through a UNIX socket.
However, as soon as I turn DEBUG = False in my Django settings, I get a 502 error. The nginx error log tells me:
2015/09/08 10:37:51 [error] 940#0: *4 upstream prematurely closed connection while reading response header from upstream, client: myIP, server: mydomain.ca, request: "GET /quests/ HTTP/1.1", upstream: "uwsgi://unix:///tmp/hackerspace.sock:", host: "myDomain"
How can I prevent the socket connection from timing out, and why is DEBUG = False making this difference?
Thanks!
I found the solution that works for me. I had to specify hosts for ALLOWED_HOSTS list in django's settings.py
ALLOWED_HOSTS = ['example.com', 'example.dev']
The "ALLOWED_HOSTS" answer also solved my problem. One thing to elaborate on, since it was not immediately clear to me anyways, the values you put here are the potential domain names (IPs, etc) that your site will be accessed by.
If your site is http://mysite.here/ then you NEED to put "mysite.here" in the ALLOWED_HOSTS list. Apparently, with Debug=True there is no HOST validation, once it is switched to False the system starts rejecting any request where the value of HOST: header does not appear in the list. For further reading:
https://docs.djangoproject.com/en/1.10/ref/settings/

Where is the uWSGI (error) log file when installing uWSGI using pip?

I'm trying to run my Django app using Nginx and uWSGI. When I try to go to
127.0.0.1:8001/media/media.png
Nginx properly shows the image, but when I go to
127.0.0.1:8000/CMS/users
Django is supposed to call a view. Instead, Nginx returns a 504 Gateway Time-out. When I view the Nginx error log, it says
*6 upstream timed out (110: Connection timed out) while reading response header from upstream, client: 127.0.0.1, server: 192.168.174.131, request: "GET /CMS/users HTTP/1.1", upstream: "uwsgi://127.0.0.1:8001", host: "127.0.0.1:8000"
Does a uWSGI error log file exist? If yes, where can I find it? (I'm searching for it because I think it might help debug the issue).
Note: I installed uwsgi using pip.

502 on Nginx after start CSF

I've installed gunicorn, django and nginx as described in this tutorial.
I've developed a function in django that adds a user in my database and it works perfectly!!
Now I've installed CSF (Config Server Firewall) and if I run it csf -r, the same function for adding a user doesn't work.
I see, in the error log of Nginx, this raw:
*2014/06/25 10:11:04 [error] 5396#0: *19 upstream prematurely closed connection while reading response header from upstream, client: 79.35.50.121, server: [MY_SERVER_NAME], request: POST /django/registerClient HTTP/1.1, upstream: [MY_IP_SERVER]:8001/django/registerClient, host: [MY_IP_SERVER]*
I underline that the function work without star csf.
Can anyone can explain to me why I get this problem?

502 error after adding application to a Django project running on nginx and gunicorn

I am trying to add an application to an existing Django project, but once I have done it I get a 502 error.The server is running Ubuntu. I don't think it has to do with the applications code because I got it running on the django development server. It goes away when I take out the app's name from settings.py and restart gunicorn.
Here's a part of the log
2011/07/15 01:24:45 [error] 16136#0: *75593 connect() failed (111: Connection refused) while connecting to upstream, client: 24.17.8.152, server: staging.site.org, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8020/", host: "staging.site.org"
Here's the nginx config file.
Nginx Config File
I'm not sure what other information is needed. Not sure where the gunicorn logs are located. My server admin skills are kind of lacking.
Nginx isn't able to connect to your backend (gunicorn) or gunicorn is refusing the connection. You provided no details about the configuration so that's all the help you'll get. You are correct that the application code has nothing to do with it. It's a configuration error on your part.