欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

502 Bad Gateway 504 error is shown when accessing password-protected directory: upstream prematurely closed connection 有大用 有大大用

Websites hosted on Plesk server are unavailable with 502 Bad Gateway: upstream sent too big header while reading response header

Follow



Symptoms

  • While accessing a protected directory in a web browser, it fails with 502 Bad Gateway error after entering the login and password.

  • Error entries in /var/log/plesk/panel/log:

    [error] 3671#0: *209872 upstream prematurely closed connection while reading response header from upstream, client: 203.0.113.2, server: example.com, request: "GET /download/debian/ HTTP/1.1", upstream: "http://203.0.113.2:7080/download/debian/", host: "example.com"

Cause

Timeout has been reached.

Resolution

  1. Connect to the server via SSH.

  2. Add following lines to /etc/nginx/nginx.conf :

    http{
    ...
    fastcgi_buffers 8 128k;
    fastcgi_buffer_size 256k;
    ...
    }

  3. Restart Apache service:
    for CentOS-based OSes:

    # service httpd restart

    for Debian-based OSes:

    # service apache2 restart

  4. Restart nginx service:

    # service nginx restart


来自  https://support.plesk.com/hc/en-us/articles/213904865-502-Bad-Gateway-error-is-shown-when-accessing-password-protected-directory-upstream-prematurely-closed-connection


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 config

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

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.

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

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

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

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"

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

Any ideas anyone???

Below are some of my config files:


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

I have the following django.conf file, which extends 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;
    }
}

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

and 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

1 Answer 正确答案

I found the issue.

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;
    }
}
  • Ah, so there needs to be additional configuration on apache2 / nginx? As on the local development machine it's working flawlessly without an issue but on the production I get the same upstream error as you. I'm using Apache, so not quite sure if there's a similar way to solve it. – kiradotee Aug 5 '16 at 10:33
  • I've only configured this through NGINX. This configuration assumes that the webserver and django are running on the same host, if that helps. – Dominooch Jan 31 at 22:26

来自  https://stackoverflow.com/questions/30470448/nginx-uwsgi-websockets-502-bad-gateway-upstream-prematu...


普通分类: