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

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/

Related

*10 upstream timed out (110: Connection timed out) while reading response header from upstream with uwsgi

I currently have a server setup with nginx and uwsgi with django
This error doesn't happen until I try to change my rds instance
my fully error message is
*10 upstream timed out (110: Connection timed out) while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: xxx.xxx.xxx.xxx, request: "GET /load/ HTTP/1.1", upstream: "uwsgi://unix:/tmp/load.sock", host: "example.com", referrer: "https://example.com/"
I was using aws rds (postgres) which works perfectly fine. The only change I made is changing from regular postgres service to aurora postgres I didn't upgrade the db, from regular to aurora. I created a new aurora postgres. I got everything setup...changed host and everything in my django db setting. runserver locally works fine. It does connect to db with read and write. Works perfectly. But when I deploy to server, open up my domain. Anything ui related looks fine but db related, NO. Took awhile then of course the 504 gateway timeout. I went to checkout the nginx error log. That's the error message I found. Googled, tried a few settings other stackoverflow posts suggested such as addingsingle-interpreter = true into uwsgi.ini file. No luck.
Can someone please give me an idea where I should look into for this problem?
Thanks in advance.
try going to your rds instance, check its' security group setting. Happened to me once, too me a while to find out that the security group setting is the problem. I didn't recall setting up the security group but it restricted with local IP

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?

Bad Request (400) after making supervisor restart

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.

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.