Nginx bad gateway error on some methods while other works - django

I am running Django on nginix but one of my functions is returning this error:
upstream prematurely closed connection while reading response header from upstream
While other methods are working fine.
I have traied increasing server timeout in nginx.conf file but still it gives me the same error.
This methods runs fine whe i run my django independently with
python manage.py runserver 0.0.0.0:8000
Can someone help me to resolve it?

Related

Axios with django server on localhost

Trying to access django server on localhost from my react native app. All I get is Network Error, which doesn't say much. Tried changing info.plist file as suggested in other answers on stack, tried changing firewall settings and so on.
The thing that worked and solved all my problems was changing the url on axios to my local ip and running django server with command: python manage.py runserver 0.0.0.0 which to my understanding will accept any connection to the server.

How change buffer-size in gunicorn(django) + nginx + docker

My problem is that in some requests that application recive body size large(xml) and return xml large, the request returns 502. This occurs randomly.
I think that it's running buffer overflow.
My application running in docker with guinicorn like wsgi. And i want change value of buffer-size to 64k. how do I do that?
my gunicorn deploy command
python manage.py migrate && gunicorn backend.wsgi:application -b 0.0.0.0:8000 --workers 3 --log-level=info
I can be mistaken, my base in:
Nginx uwsgi (104: Connection reset by peer) while reading response header from upstream
Error log nginx
38286 upstream prematurely closed connection while reading response header from upstream
I think it's an nginx issue, check out client_max_body_size

504 gateway timeout flask socketio

I am working on a flask-socketio server which is getting stuck in a state where only 504s (gateway timeout) are returned. We are using AWS ELB in front of the server. I was wondering if anyone wouldn't mind giving some tips as to how to debug this issue.
Other symptoms:
This problem does not occur consistently, but once it begins happening, only 504s are received from requests. Restarting the process seems to fix the issue.
When I run netstat -nt on the server, I see many entries with rec-q's of over 100 stuck in the CLOSE_WAIT state
When I run strace on the process, I only see select and clock_gettime
When I run tcpdump on the server, I can see the valid requests coming into the server
AWS health checks are coming back succesfully
EDIT:
I should also add two things:
flask-socketio's server is used for production (not gunicorn or uWSGI)
Python's daemonize function is used for daemonizing the app
It seemed that switching to gunicorn as the wsgi server fixed the problem. This legitimately might be an issue with the flask-socketio wsgi server.

How to check errors for gunicorn using sock

I have a Django project running using gunicorn sock(not port).
I am using supervisor to run it. The problem is - supervisor is saying that the process is running. Logs doesnt show anything.
But site says "Bad gateway". Nginx generally shows bad gateway when the gunicorn is not running. But here, gunicorn is running without errors but nginx shows bad gateway.
If it uses port, I would have tested locally using "wget http://localhost:8000" but since we use sock here, how to test if its really running and why its not showing any error.

Nginx "Broken pipe" When Debugging Django?

I have a Django site that uses Gunicorn and Nginx. Occasionally, I'll have a problem that I need to debug. In the past, I would shut down Gunicorn and Nginx, go to my Django project directory and start the Django development server ("python ./manage.py runserver 0:8000"), and then restart Nginx. I could then insert set_trace() commands and do my debugging. When I fixed the problem I'd shut down Nginx and then restart Gunicorn and Nginx. I'm pretty sure this was working.
Recently, though, I've begun having problems. What happens now is that when I've stopped at a breakpoint, after a couple of minutes the web page that I've stopped on will change and display "404 Not Found" and if I take another step in the debugger, I'll see this error:
- Broken pipe from ('127.0.0.1', 43742)
This happens on my development, staging, and production servers which I'm accessing via their domain names, e.g. "web01.example.com" (not really example).
What is the correct way to debug my Django application on my remote servers?
Thanks.
I figured out the problem. First I observed that when I stopped at a breakpoint, the page always timed out after exactly one minute which suggested that the Nginx connection to the web server was timing out if the web server took more than 60 seconds to respond. I then found an Nginx proxy_read_timeout directive which defines this timeout. Then it was merely a matter of changing the length of the timeout in my Nginx config file:
# /etc/nginx/sites-enabled/example.conf
http {
server {
...
location #django {
...
# Set timeout to 1 hour
proxy_read_timeout 3600s;
...
}
...
}
}
Once you've made this change you need to reload Nginx, not restart it, in order to this change to take effect. Then you start Django as I indicated above and you can now debug your Django application without it timing out. Just be sure to remove the timeout setting when you're done debugging, reload Nginx again, and restart Gunicorn.