"upstream prematurely closed connection while reading response header from upstream" Django, Ubuntu, Nginx, Gunicorn
Asked Answered
B

2

12

I deployed a Django website using this tutorial

https://jee-appy.blogspot.com/2017/01/deply-django-with-nginx.html

The website can be accessed from the internet, www.simplesol.com

When I try to make a POST request, it fails and I get a 502 error.

In my nginx-error.log file I get this error message

*344 upstream prematurely closed connection while reading response header 
from upstream, client: InternalIP, server: ExternalIP, request: "POST 
/audit/ HTTP/1.1", upstream: 
"http://unix:/home/webadmin/virtual_env/run/gunicorn.sock:/audit/", host: 
"www.simplesol.com", referrer: "http://www.simplesol.com/audit/"

These are the contents of my /etc/nginx/sites-available/simplesol.conf

command = /home/webadmin/gunicorn_start.bash                   ;  Command to start app
user = webadmin                                                ; User to run as
stdout_logfile = /home/webadmin/logs/gunicorn_supervisor.log   ; Where to write log messages
redirect_stderr = true                                       ; Save stderr in the same log
environment=LANG=en_US.UTF-8,LC_ALL=en_US.UTF-8              ; Set UTF-8 as default encoding

/etc/nginx/sites-available/simplesol.conf

upstream 192.168.8.5 {

server unix:/home/webadmin/adrienne/run/gunicorn.sock fail_timeout=0;
}

server {

    listen   80;
server_name 192.168.8.5 ;

client_max_body_size 4G;
access_log /home/webadmin/logs/nginx-access.log;
error_log /home/webadmin/logs/nginx-error.log;

location /static/ {
    autoindex on;
    alias /home/webadmin/static/;
}

location /media/ {
    alias   /home/webadmin/media/;
location / {

    # an HTTP header important enough to have its own Wikipedia entry:
    #   http://en.wikipedia.org/wiki/X-Forwarded-For
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;


    # enable this if and only if you use HTTPS, this helps Rack
    # set the proper protocol for doing redirects:
    #proxy_set_header X-Forwarded-Proto https;

    # pass the Host: header from the client right along so redirects
    # can be set properly within the Rack application
    proxy_set_header Host $http_host;

    # we don't want nginx trying to do something clever with
    # redirects, we set the Host: header above already.
    proxy_redirect off;

    # set "proxy_buffering off" *only* for Rainbows! when doing
    # Comet/long-poll stuff.  It's also safe to set if you're
    # using only serving fast clients with Unicorn + nginx.
    # Otherwise you _want_ nginx to buffer responses to slow
    # clients, really.
    proxy_buffering off;

    # Try to serve static files from nginx, no point in making an
    # *application* server like Unicorn/Rainbows! serve static files.
    if (!-f $request_filename) {
        proxy_pass http://192.168.8.5;
        break;
    }
}

# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
    root /home/webadmin/SimpleSolWebsite/static/;
}
   }

gunicorn_start.bash

#!/bin/bash

NAME="django_app"                                   
DJANGODIR=/home/ubuntu/sample_project               
SOCKFILE=/home/ubuntu/django_env/run/gunicorn.sock  
USER=ubuntu                                         
GROUP=ubuntu                                        
NUM_WORKERS=3                                       
DJANGO_SETTINGS_MODULE=sample_project.settings              
DJANGO_WSGI_MODULE=sample_project.wsgi              
echo "Starting $NAME as `whoami`"



cd $DJANGODIR
source /home/ubuntu/django_env/bin/activate
export DJANGO_SETTINGS_MODULE=$DJANGO_SETTINGS_MODULE
 export PYTHONPATH=$DJANGODIR:$PYTHONPATH



RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR



exec gunicorn ${DJANGO_WSGI_MODULE}:application \
  --name $NAME \
  --workers $NUM_WORKERS \
  --user=$USER --group=$GROUP \
  --bind=unix:$SOCKFILE \
  --log-level=debug \
  --log-file=-

Is there something wrong with my configuration files? I don't know where I went wrong and I haven't had any success on google. Thanks in advance for any insight you have for me!

gunicorn_supervisor_log

[2018-03-14 22:23:22 +0000] [25559] [DEBUG] GET /contact/
[2018-03-14 22:23:51 +0000] [10934] [DEBUG] POST /contact/
[2018-03-14 15:24:21 -0700] [24869] [CRITICAL] WORKER TIMEOUT (pid:10934)
[2018-03-14 22:24:21 +0000] [10934] [INFO] Worker exiting (pid: 10934)
[2018-03-14 15:24:21 -0700] [23688] [INFO] Booting worker with pid: 23688

Contacts App

models.py

from django.db import models


class Contacts(models.Model):
    contact_name = models.CharField(max_length=256, default='contact name')
    phone_number = models.CharField(max_length=15)
    phone_ext = models.CharField(max_length=10, default='ext', blank="True")
    email = models.EmailField()
    comments = models.TextField()
    company_name = models.CharField(max_length=250, default='company name')

forms.py

from django import forms
from contacts.models import Contacts
from django.forms import ModelForm
from django.db import models

class ContactForm(forms.ModelForm):
    class Meta:
        model = Contacts
        fields = "__all__"

views.py

from django.shortcuts import render
from django.views.generic.edit import FormView
from contacts.forms import ContactForm
from django.views.generic import TemplateView
from exchangelib import *
from exchangelib.items import *

class ContactRequestView(FormView):
    template_name = 'contact_form.html'
    form_class = ContactForm
    success_url = '/contact/thanks/'


    def form_valid(self, form):
        credentials = Credentials(
           username='simplesol.com\\jbobst',
           password='password'
       )
        account = Account(
            primary_smtp_address='[email protected]',
            credentials=credentials,
            autodiscover=True,
            access_type=DELEGATE
            )
        audit_request = ''
        for k in form.cleaned_data:
            audit_request += k + ' = ' + form.cleaned_data[k] + '\n'

        m = Message(
            account=account,
            subject='You have a new site audit request',
            body= audit_request,
            to_recipients=
    ['[email protected]','[email protected]'],
        )

        m.send_and_save()

        form.save()
        return super().form_valid(form)



class ContactThanksView(TemplateView):
    template_name = 'contact_thanks.html'
Beale answered 7/3, 2018 at 22:40 Comment(0)
H
13

Gunicorn's default timeout is 30 seconds

Workers silent for more than this many seconds are killed and restarted.

If a worker is killed/restarted the connection to the nginx gets terminated and nginx would produce the error that you experience

*344 upstream prematurely closed connection while reading response header from upstream

This is confirmed by the error you see in gunicorn's logs:

[2018-03-14 22:23:51 +0000] [10934] [DEBUG] POST /contact/
[2018-03-14 15:24:21 -0700] [24869] [CRITICAL] WORKER TIMEOUT (pid:10934)

So either modify you code to complete everything in ~30 seconds (better) or adjust the timeout (worse)

Hendrika answered 15/3, 2018 at 5:22 Comment(8)
Thanks for the information. How do I modify my code to complete everything in 30 seconds?Beale
Well it depends on your app and most likely involves your queries optimization / some refactoring - not something that can be easily advised without digging into the code and your data )Hendrika
github.com/RabbleMonster/SimpleSolWebsite The code is up on github. Here' sthe link, I know it's a lot to look through so I'll add the siteaudit and contact forms to my post. Thanks so much for your help. This is my first time deploying a django project and I've been really lost.Beale
ffeast, do you think it could be the code that sends the emails that's slowing it down?Beale
I took that part of the code out and now it works. Thanks so much for your help I would have never gotten to that conclusion without your input!Beale
Yes it could. Seems it communicates with ms exchange => network connection, if there is a networking problem your worker would hangHendrika
This is, of course, assuming your code is what's taking too long. I am currently sitting with a Django app which works fine on a dev server, but times out on a production server despite them being exact clones of eachother (db included)Puckett
I increased the timeout. I am uploading Excel files and the upload time over my 10mbit service is just over 30 seconds. A better solution I suppose would be to implement some kind of progress meter which would keep the connection alive but that's a lot more complicated than increasing the timeout.Export
E
14

Set timeout in both Nginx and gunicorn configuration

Example: I have configured it for flask by add adding --timeout 600

ExecStart=/home/ubuntu/yourworkspace/venvProject/bin/gunicorn --workers 3 --bind unix:/home/ubuntu/yourworkspace/Project/app.sock  --timeout 600  -m 007 wsgi:app

In Nginx:

 location / {
        include proxy_params;
       proxy_read_timeout 600s;
       proxy_connect_timeout 600s;
      proxy_pass http://unix:/home/ubuntu/yourworkspace/Project/app.sock;
        }
Elide answered 6/10, 2021 at 12:37 Comment(2)
This one should be accepted as a working solution... the accepted solution is only addressing something that is already known without showing any practical way to solve it!Fasciate
I added these changes even though I am not able to see changes I am getting the same issue for only all govt websites, Ex: ahara.kar.nic.in, Can you please suggest anythingOpportunism
H
13

Gunicorn's default timeout is 30 seconds

Workers silent for more than this many seconds are killed and restarted.

If a worker is killed/restarted the connection to the nginx gets terminated and nginx would produce the error that you experience

*344 upstream prematurely closed connection while reading response header from upstream

This is confirmed by the error you see in gunicorn's logs:

[2018-03-14 22:23:51 +0000] [10934] [DEBUG] POST /contact/
[2018-03-14 15:24:21 -0700] [24869] [CRITICAL] WORKER TIMEOUT (pid:10934)

So either modify you code to complete everything in ~30 seconds (better) or adjust the timeout (worse)

Hendrika answered 15/3, 2018 at 5:22 Comment(8)
Thanks for the information. How do I modify my code to complete everything in 30 seconds?Beale
Well it depends on your app and most likely involves your queries optimization / some refactoring - not something that can be easily advised without digging into the code and your data )Hendrika
github.com/RabbleMonster/SimpleSolWebsite The code is up on github. Here' sthe link, I know it's a lot to look through so I'll add the siteaudit and contact forms to my post. Thanks so much for your help. This is my first time deploying a django project and I've been really lost.Beale
ffeast, do you think it could be the code that sends the emails that's slowing it down?Beale
I took that part of the code out and now it works. Thanks so much for your help I would have never gotten to that conclusion without your input!Beale
Yes it could. Seems it communicates with ms exchange => network connection, if there is a networking problem your worker would hangHendrika
This is, of course, assuming your code is what's taking too long. I am currently sitting with a Django app which works fine on a dev server, but times out on a production server despite them being exact clones of eachother (db included)Puckett
I increased the timeout. I am uploading Excel files and the upload time over my 10mbit service is just over 30 seconds. A better solution I suppose would be to implement some kind of progress meter which would keep the connection alive but that's a lot more complicated than increasing the timeout.Export

© 2022 - 2024 — McMap. All rights reserved.