nginx uwsgi websockets 502 Bad Gateway upstream prematurely closed connection while reading response header from upstream

前端 未结 2 1883

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-websock

相关标签:
2条回答
  • 2020-12-29 06:17

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-29 06:19

    I had same issue but it wasn't my NGINX configuration, it was my UWSGI processes causing timeout errors when I posted JSONs from client side to server. I had processes as 5, I changed it to 1 and it solved the issue. For my application, I only needed to have 1 process run at time since AWS didn't need to be overloaded with multiple processes.

    Here is the working UWSGI configuration ini file that solved the timeout issue and thus the 502 gateway issue.

    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
    

    Here's my nginx config too.

    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;
    }
    
    
    0 讨论(0)
提交回复
热议问题