nginx uwsgi websockets 502 Bad Gateway上游过早关闭连接,同时从上游读取响应头

时间:2015-05-26 23:04:01

标签: django nginx websocket redis uwsgi

我已经在这个问题上挣扎了好几天,终于到了一堵砖墙。

我一直试图让我的筹码运行:

http://django-websocket-redis.readthedocs.org/en/latest/running.html#django-with-websockets-for-redis-behind-nginx-using-uwsgi

我一直在看其他一些SO这样的文章:

nginx - uWSGI HTTP + websocket config

他们似乎遇到了类似的问题,但解决方案对我不起作用。

基本上,每当我尝试启动uWSGI进程时,我都会遇到nginx 502坏网关屏幕。根据文档中的说明,我有两个单独的uwsgi进程在运行。

当我运行websocket uwsgi实例时,我得到以下内容:

*** running gevent loop engine [addr:0x487690] ***
[2015-05-27 00:45:34,119 wsgi_server] DEBUG: Subscribed to channels: subscribe-broadcast, publish-broadcast

告诉我那个uwsgi实例运行正常。 然后我运行我的下一个uwsgi进程并且没有错误记录那里......

当我导航到浏览器中的页面时,页面会挂起几秒钟,然后才能获得502 Bad Gateway屏幕。

根据NGINX日志,NGINX说:

2015/05/26 22:46:08 [error] 18044#0: *3855 upstream prematurely closed connection while reading response header from upstream, client: 192.168.59.3, server: , request: "GET /chat/ HTTP/1.1", upstream: "uwsgi://unix:/opt/django/django.sock:", host: "192.168.59.103:32768"

这是我尝试在网络浏览器中访问该页面时唯一的错误日志。

任何人的想法???

以下是我的部分配置文件:

nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/django.conf;
}

我有以下django.conf文件,它扩展了nginx.conf

upstream django {
    server unix:/opt/django/django.sock;
}

server {
    listen 80 default_server;
    charset utf-8;
    client_max_body_size 20M;
    sendfile on;
    keepalive_timeout 0;
    large_client_header_buffers 8 32k;

location /media  {
    alias /opt/django/app/media/media;  
}

location /static {
    alias /opt/django/app/static;
}

location / {
    include /opt/django/uwsgi_params; 
}

location /ws/ {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://unix:/opt/django/app.sock;
        proxy_buffers 8 32k;
        proxy_buffer_size 64k;
    }
}

两个负责我的uwsgi进程的文件如下:

runserver_uwsgi.ini:

[uwsgi]
ini = :runserver

[default]
userhome = /opt/django
chdir = %dapp/
master = true
module = chatserver.wsgi:application
no-orphans = true
threads = 1
env = DJANGO_SETTINGS_MODULE=myapp.settings
vacuum = true

[runserver]
ini = :default
socket = /opt/django/app.sock
module = wsgi_django
buffer-size = 32768
processes = 4
chmod-socket=666

和wsserver_uwsgi.ini

[uwsgi]
ini = :wsserver

[default]
userhome = /opt/django
chdir = %dapp/
master = true
module = chatserver.wsgi:application
no-orphans = true
threads = 1
env = DJANGO_SETTINGS_MODULE=chatserver.settings
vacuum = true

[wsserver]
ini = :default
http-socket = /opt/django/django.sock
module = wsgi_websocket
http-websockets = true
processes = 2
gevent = 1000
chmod-socket=666

2 个答案:

答案 0 :(得分:6)

我发现了这个问题。

我的[runserver]套接字(app.sock)应该指向upstream django,我的[wsserver]套接字(django.sock)应该指向location /ws/,如下所示:

upstream django {
    server unix:/opt/django/app.sock;
}

server {
    listen 80 default_server;
    charset utf-8;
    client_max_body_size 20M;
    sendfile on;
    keepalive_timeout 0;
    large_client_header_buffers 8 32k;

location /media  {
    alias /opt/django/app/media/media;  
}

location /static {
    alias /opt/django/app/static;
}

location / {
    include /opt/django/uwsgi_params; 
}

location /ws/ {
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://unix:/opt/django/django.sock;
        proxy_buffers 8 32k;
        proxy_buffer_size 64k;
    }
}

答案 1 :(得分:2)

我有同样的问题,但这不是我的NGINX配置,这是我的UWSGI进程在我将JSON从客户端发布到服务器时导致超时错误。我的流程为5,将其更改为1,这样就解决了问题。对于我的应用程序,我只需要一次运行一个进程,因为AWS不需要被多个进程超载。

这是工作的UWSGI配置ini文件,它解决了超时问题,从而解决了502网关问题。

autoboot.ini

#!/bin/bash

[uwsgi]
socket          = /tmp/app.sock

master          = true

chmod-socket    = 660
module          = app.wsgi
chdir           = home/app

close-on-exec = true # Allow linux shell via uWSGI

processes = 1
threads = 2
vacuum = true

die-on-term = true

这也是我的Nginx配置。

nginx.conf

# the upstream component nginx needs to connect to
upstream django {
    server unix:///app/tmp/app.sock; # for a file socket
    # server 127.0.0.1:6000; # for a web port socket (we'll use this first)
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80;

    # the domain name it will serve for
    server_name XXX.XXX.XX.X #actual IP in here
    charset     utf-8;

    # max upload size
    client_max_body_size 75M;   # adjust to taste

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    # Finally, send all non-media requests to the Django server.
    location / {
        uwsgi_pass  django;
        include     uwsgi_params;
    }

    location /static {
        autoindex on;
        alias app/static; # your Django project's static files - amend as required
    }

    error_page 502 /502.html;
    location = /502.html {
        alias app/templates/502autoreload.html;
    }

    client_body_timeout 100s;
    uwsgi_read_timeout 500s;
    keepalive_timeout 300;
}