Nginx Bad Gateway with Django Social Auth and uwsgi - django

My site is running correctly locally (using the built in runserver), but when running with nginx and uwsgi, I'm getting a Bad Gateway (502) during the django-social-auth redirect.
The relevant nginx error_log:
IPREMOVED - - [11/Oct/2012:12:10:18 +1100] "GET /complete/google/? ..snip .. HTTP/1.1" 502 574 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.26 Safari/537.11"
The uwsgi log:
invalid request block size: 4204 (max 4096)...skip
Thu Oct 11 12:16:46 2012 - error parsing request
Refreshing the Bad Gateway response redirects and logs in correctly. This happens every single time. The nginx and uwsgi logs here have different timing as they were separate requests. The logs are consistent.
This is the first time deploying django to nginx for me, so I'm at a loss as to where to start.

Have you tried increasing the size of the uwsgi buffer:
-b 32768
http://comments.gmane.org/gmane.comp.python.wsgi.uwsgi.general/1171

Related

Nginx 10 minute timeout occurring

I have an AWS-hosted web application that initiates a long-running server process (more than 10 mintues). An Nginx reverse proxy server sits between the application load balancer (ALB) and the service. Both the Nginx server and the service reside within separate Kubernetes pods running on an EC2 instance.
I'm experiencing an issue with a connection being closed. The Nginx logs show a HTTP 499 error:
(][05/Dec/2022:12:02:27 +0000] "POST -------------- HTTP/1.1" 499 0
"https://------------.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X
10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0
Safari/537.36")
The issue is repeatable and occurs exactly 10 minutes after the request was initiated. Despite my having set the ALB, Nginx, and SQL Alchemy timeouts to be much longer than 10 minutes, I suspect a timeout is occurring with a default value of 10 minutes, but I can't figure out where.
Nginx is the product I'm least familiar with and so I suspect that I have failed to make the necessary timeout configs in its conf file. I have set this:
proxy_read_timeout 20m;
Can anyone suggest where in the system the default timeout is occurring?

localhost:8000 working but mywebsite:8000 with etc/hosts modified doesn't work

Everything is in the title: I'm launching my webserver (Django / Python) on port 8000.
/home/olivier/my_venv3.8/bin/python3 /home/olivier/pycharm-2018.1.3/plugins/python/helpers/pydev/pydevd.py --multiproc --qt-support=auto --client 127.0.0.1 --port 40761 --file /home/olivier/projects/evalr/manage.py runserver 8000
pydev debugger: process 5923 is connecting
Connected to pydev debugger (build 193.5662.61)
http://evalr.hqf.fr:8000/fr/assistant/
http://evalr.hqf.fr:8000/fr/sessions/
pydev debugger: process 5935 is connecting
http://evalr.hqf.fr:8000/fr/assistant/
http://evalr.hqf.fr:8000/fr/sessions/
Watching for file changes with StatReloader
Performing system checks...
I've added in my /etc/hosts this line : 127.0.1.1 evalr.hqf.fr and when I ping it, it correctly shows "127.0.0.1"
When I try go on http://evalr.hqf.fr:8000 I get connection refused and when I try to go on http:/localhost:8000 it works.
The problem is that when I try to go on http://evalr.hqf.fr:8000 I dont have any trace on the server side, i.e. it stays here:
System check identified no issues (0 silenced).
January 03, 2020 - 20:41:31
Django version 3.0.2, using settings 'evalr.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
And when I go on http://127.0.0.1:8000/ I get a log of every single access like:
[03/Jan/2020 20:47:42] "GET /fr/sessions/ HTTP/1.1" 302 0
[03/Jan/2020 20:47:42] "GET /fr/login/?next=/fr/sessions/ HTTP/1.1" 200 7031
[03/Jan/2020 20:47:43] "GET /static/vendors/css/all.min.css HTTP/1.1" 200 54456
................ blabla............;
[03/Jan/2020 20:47:43] "GET /favicon.ico HTTP/1.1" 301 0
I've even tried to reboot (even though I'm on Linux, which talks by itself - no reboot needed), so I'm stuck because everything should work fine. What am I missing?
That is because in /etc/hosts it should be 127.0.0.1 instead
Also check the ALLOWED_HOSTS and you could try running it with: python manage.py runserver 0.0.0.0:8000
The fact that you get a connection refused when using the hostname, and do not see any logging from your application, makes me think that you are dealing with DNS caching. My favorite way to test this is using curl, since it can show you both the IP address and response of an HTTP request. Try the following command:
curl -Iv -X GET http://evalr.hqf.fr:8000
Did it resolve to 127.0.0.1? If so, did you get a response? If so, did you see logs in your application?
If the answer to these question is yes, then try to clear the DNS and browser cache.

ngrok doesn't lift my local django web server

I have problems with negrok, it does not raise my local django web server, a few days ago everything worked correctly but a normal day, without having touched anything, it stopped working.
Run the following:
python manage.py runserver
Everything works normally and correctly.
Now I execute the following, in order to raise the local django server:
./ngrok http 8000
ngrok runs correctly without any problem but when accessing the ngrok link, the page is loading and everything is blank and does not work ....
This appears in ngrok:
ngrok by #inconshreveable (Ctrl+C to quit)
Session Status online
Account Julio (Plan: Free)
Version 2.3.34
Region United States (us)
Web Interface http://127.0.0.1:4040
Forwarding http://33822c5a.ngrok.io -> http://localhost:8000
Forwarding https://33822c5a.ngrok.io -> http://localhost:8000
Connections ttl opn rt1 rt5 p50 p90
2 0 0.03 0.01 0.20 0.21
HTTP Requests
-------------
GET / 400 Bad Request
GET / 400 Bad Request
And this appears in django:
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
September 25, 2019 - 17:23:19
Django version 2.2.2, using settings 'ibme_proyect.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Invalid HTTP_HOST header: '33822c5a.ngrok.io'. You may need to add '33822c5a.ngrok.io' to ALLOWED_HOSTS.
Bad Request: /
[25/Sep/2019 17:25:30] "GET / HTTP/1.1" 400 68119
Invalid HTTP_HOST header: '33822c5a.ngrok.io'. You may need to add '33822c5a.ngrok.io' to ALLOWED_HOSTS.
Bad Request: /
[25/Sep/2019 17:25:38] "GET / HTTP/1.1" 400 68254
I add the ngrok host (33822c5a.ngrok.io) in ALLOWED_HOSTS and stop giving django error, and access the ngrok link and I know that it loads again, it doesn't load the HTML, nothing, everything blank ...
Any solution? :(
try
ALLOWED_HOSTS = ["*"]
this will allow tunneling every ngrok run :)

Internal Server Error on access via HTTPS to a Django application

I'm trying to make an access to my Django application by HTTPS.
In the config.py of the project I has set the key
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https')
On my configuration to serve all I've an Apache 2.4 so I've create the right entry into "sites-availables" directory, obviously followed by a2ensite, I've generate my own SSL Key (.crt and .key files), changed ownership and permission, restared apache2.
When I try to access to the site I got the follow error message
"""
Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.
Please contact the server administrator at webmaster#localhost to
inform them of the time this error occurred, and the actions you
performed just before this error.
More information about this error may be available in the server error log.
Apache/2.4.7 (Ubuntu) Server at inno Port 9443
"""
I've take a look into /var/log/apache2/error.log but there's nothing, on /var/log/apache2/access.log I've found
xxx.xxx.xxx.xxx - - [25/May/2016:12:29:36 +0200] "GET /favicon.ico
HTTP/1.1" 500 996 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64;
rv:46.0) Gecko/20100101 Firefox/46.0"
There are someone that can give me some hint?
Thanks a lot
Carlos
It looks like your virtual host within apache isnt properly configured so it's not handling php files the way it should. I guess you should use a handler directive in order to tell apache to handle all .php files and most probably your issue will be fixed. This is the maximum that can be done since you didn't provide much info about the error.

Production Django Application Throwing/Not Throwing 500 Error based on Debug = Value

I have an production django application that runs fine with Debug = True; but doesn't with Debug=False.
If I load run the domain, it shows my urls.py file, which is really bad.
I want to get my application where it uses Debug=False and TEMPLATE_DEBUG=False instead of Debug=True and TEMPLATE_DEBUG=True , since by using the True value it exposes the application
If I view my error.log under nginx with DEBUG=True:
2013/10/25 11:35:34 [error] 2263#0: *5 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xxx.xx.xxx, server: *.myapp.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8001/", host: "www.myapp.com"
view my access.log under nginx with DEBUG=True:
xx.xxx.xx.xxx - - [25/Oct/2013:11:35:33 +0000] "GET / HTTP/1.1" 502 173 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Firefox/24.0"
So my question is, why when I set DEBUG=True and TEMPLATE_DEBUG=True does it load successfully showing the application and when I set DEBUG=False and TEMPLATE_DEBUG=False it shows the custom http 500 error page? (I have created to handle http 500 errors)
Thanks to Toad013 and Dmitry for their suggestions.
It appears the issue might have been with how nginx and gunicorn were being started and not a configuration issue, thus, I ended up using the following to start my app:
/usr/local/bin/gunicorn -c /home/ubuntu/virtualenv/gunicorn_config.py myapp.wsgi
sudo nginx -c /etc/nginx/nginx.conf