7

我使用本教程部署了一个 Django 网站

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

该网站可从互联网 www.simplesol.com 访问

当我尝试发出 POST 请求时,它失败并且我收到 502 错误。

在我的 nginx-error.log 文件中,我收到此错误消息

*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/"

这些是我的 /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=-

我的配置文件有问题吗?我不知道我哪里出错了,我在谷歌上没有任何成功。提前感谢您对我的任何见解!

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

通讯录应用

模型.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')

表格.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__"

视图.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='jbobst@simplesol.com',
            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=
    ['sales@simplesol.com','scheduling@simplesol.com'],
        )

        m.send_and_save()

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



class ContactThanksView(TemplateView):
    template_name = 'contact_thanks.html'
4

2 回答 2

10

Gunicorn 的默认超时时间是30 秒

沉默超过这么多秒的工人被杀死并重新启动。

如果工作人员被杀死/重新启动,则与 nginx 的连接将终止,并且 nginx 会产生您遇到的错误

*344 上游过早关闭连接,同时从上游读取响应标头

您在 gunicorn 的日志中看到的错误证实了这一点:

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

所以要么修改你的代码以在〜30秒内完成所有事情(更好),要么调整超时(更糟)

于 2018-03-15T05:22:17.927 回答
0

在 Nginx 和 gunicorn 配置中设置超时

示例:我通过添加添加--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

在 Nginx 中:

 location / {
        include proxy_params;
       proxy_read_timeout 600s;
       proxy_connect_timeout 600s;
      proxy_pass http://unix:/home/ubuntu/yourworkspace/Project/app.sock;
        }
于 2021-10-06T12:37:05.943 回答