【问题标题】:upstream prematurely closed connection (uwsgi + nginx + django)上游过早关闭连接(uwsgi + nginx + django)
【发布时间】:2017-07-22 08:17:22
【问题描述】:

我正在尝试在新服务器上配置我的 django 应用程序。除非我尝试传递 GET 参数,否则它工作正常。我收到以下错误。

uWSGI:

[pid: 21530|app: 0|req: 8/9] 109.68.173.7 () {42 vars in 880 bytes} [Thu Mar  2 17:19:29 2017] GET /install/?token=123&shop=1234&insales_id=124 => generated 0 bytes in 71 msecs (HTTP/1.1 500) 0 headers in 0 bytes (0 switches on core 0)

nginx:

2017/03/02 09:19:29 [error] 21644#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 109.68.173.7, server: 151-248-112-157.xen.vps.regruhosting.ru, request: "GET /install/?token=123&shop=1234&insales_id=124 HTTP/1.1", upstream: "uwsgi://unix:/home/trackpost.sock:", host: "151-248-112-157.xen.vps.regruhosting.ru"

我的配置文件。 nginx.conf:

    # For more information on configuration, see:                                                                                               
    #   * Official English Documentation: http://nginx.org/en/docs/                                                                             
    #   * Official Russian Documentation: http://nginx.org/ru/docs/                                                                             

    user nginx;
    worker_processes auto;
    error_log /var/log/nginx/error.log;
    pid /var/run/nginx.pid;

    # Load dynamic modules. See /usr/share/nginx/README.dynamic.                                                                                
    include /usr/share/nginx/modules/*.conf;

    events {
        worker_connections  1024;
    }


    http {
        log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                          '$status $body_bytes_sent "$http_referer" '
                          '"$http_user_agent" "$http_x_forwarded_for"';

        access_log  /var/log/nginx/access.log  main;

        sendfile            on;
        tcp_nopush          on;
        tcp_nodelay         on;
        keepalive_timeout   120;
        client_max_body_size 20M;

        uwsgi_read_timeout 86400;
        uwsgi_send_timeout 86400;

        proxy_buffers 8 32k;
        proxy_buffer_size 64k;

        include             /etc/nginx/mime.types;
        default_type        application/octet-stream;

        # Load modular configuration files from the /etc/nginx/conf.d directory.                                                                
        # See http://nginx.org/en/docs/ngx_core_module.html#include                                                                             
        # for more information.                                                                                                                 
        include /etc/nginx/conf.d/*.conf;

        server {
               listen 80;
               server_name 151-248-112-157.xen.vps.regruhosting.ru;
               location = favicon.ico { access_log off; log_not_found off; }
           location /static/ {
               root /home/rajansnow/django/trackpost;
           }
           location / {
              include uwsgi_params;
              uwsgi_pass unix:/home/trackpost.sock;
           }
    }
}

app.ini (uwsgi):

    [uwsgi]
project = trackpost
username = rajansnow
base = /home/%(username)/django

chdir = %(base)/%(project)
home = %(base)/venv
module = %(project).wsgi:application
plugin = python

master = true
processes = 5

uid = rajansnow
socket = /home/trackpost.sock
chown-socket = rajansnow:nginx
chmod-socket = 666
vacuum = true

touch-reload = /home/rajansnow/django/trackpost/uwsgi.ini
py-autoreload = 3
harakiri = 30

我的代码有点糟糕,我知道。但它适用于我以前基于 Debian 的服务器。当前是基于 CentOS 的。我已经尝试了我在 SO 上找到的所有东西,但没有运气。我能做些什么来解决这个问题?

【问题讨论】:

  • 您是否尝试过仅运行python manage.py runserver 并查看 Django 是否在此特定虚拟环境中正确处理 GET 请求?
  • @abcdn 嗯,这是一个想法。我明天试试
  • @abcdn 奇怪的事情:它根本不起作用。访问任何页面时,Firefx 都会显示“无法连接”。最奇怪的是,在我开始配置 nginx 和 uwsgi 之前它确实有效。顺便说一句,我阻止了他们,所以这不是问题。不知道发生了什么,但我会尽力解决它
  • 尝试将其绑定到80以外的任何端口,以确保那里没有任何混乱,然后逐步调试:django、uwsgi、nginx。也许一些防火墙设置?
  • 我花了很多时间和精力来调试它,@abcdn,完整的故事在答案中。谢谢你的反应,伙计!

标签: django nginx uwsgi


【解决方案1】:

是时候揭开谜团了,孩子们!

问题中提到的问题是通过硬检查每个项目文件来解决的。结果发现其中一些包含了意想不到的符号,这些符号曾经使 Django 抛出编码错误,并因此使 uwsgi 停止。

神秘的部分是文件在问题发生之前很长时间被最后一次修改。那么它们怎么会包含额外的符号呢?没人知道。

至少现在,我们可以说它已经解决了。

【讨论】:

猜你喜欢
  • 2015-12-04
  • 1970-01-01
  • 2015-02-08
  • 2019-05-15
  • 2014-04-02
  • 2019-04-04
  • 2015-11-25
  • 2012-12-12
  • 2015-08-08
相关资源
最近更新 更多