nginx uwsgi websockets 502 Bad Gateway upstream 在从上游读取响应标头时过早关闭连接 [英] nginx uwsgi websockets 502 Bad Gateway upstream prematurely closed connection while reading response header from upstream

查看:103
本文介绍了nginx uwsgi websockets 502 Bad Gateway upstream 在从上游读取响应标头时过早关闭连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个问题上纠结了好几天,终于走到了墙角.

I've been banging my head on this issue for days now and have finally reached a brick wall.

我一直试图让我的堆栈运行:

I've been trying to get my stack to run:

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

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

I've been looking at some other SO articles like this one:

nginx - uWSGI HTTP + websocket 配置

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

They seem to have a similar issue i am encountering but the solution does not work for me.

基本上,每当我尝试启动我的 uWSGI 进程时,我都会遇到 nginx 502 bad gateway screen.根据文档中的说明,我有两个单独的 uwsgi 进程正在运行.

Basically, i keep encountering the nginx 502 bad gateway screen whenever i try starting up my uWSGI processes. I have two separate uwsgi processes running, as per instructions in the documentation.

当我运行 websocket uwsgi 实例时,我得到以下信息:

When i run the websocket uwsgi instance, i get the following:

*** 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 进程,那里也没有错误日志......

which tells me that that uwsgi instance is running okay. Then i run my next uwsgi process and no error logs there either...

当我在浏览器中导航到该页面时,该页面在获得 502 Bad Gateway Screen 之前会挂起几秒钟.

When i navigate to the page in the browser, the page with hang for a few seconds, before getting the 502 Bad Gateway Screen.

根据 NGINX 日志,NGINX 说:

According to NGINX logs, NGINX says:

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"

这是我尝试在 Web 浏览器中访问页面时得到的唯一错误日志.

This is the only error log i get when trying to access the page in the web browser.

任何人有什么想法???

Any ideas anyone???

以下是我的一些配置文件:

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进程的两个文件如下:

And two files that are responsible for my uwsgi processes as follows:

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

推荐答案

我发现了问题.

我的 [runserver] 套接字 (app.sock) 应指向 upstream django 下,而我的 [wsserver] 套接字 (django.sock) 应指向 location/ws/像这样:

My [runserver] socket (app.sock) should be pointed under upstream django and my [wsserver] socket (django.sock) should be pointed under location /ws/ like so:

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;
    }
}

这篇关于nginx uwsgi websockets 502 Bad Gateway upstream 在从上游读取响应标头时过早关闭连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆