NGINX with Gunicorn: error 502 bad gateway

Viewed 30

I'm trying to run a web application with a SSL certificate on port 443. To do so, I'm using Nginx as web server and Gunicorn as application server (running a Flask project). I'm using a socket as proxy pass but I keep getting 502 bad gateway. Here is my command with which I launch Gunicorn (works fine until I remove the port from the URL):

   gunicorn --workers 4 --bind unix:webapp.sock -m 007 --certfile "/etc/ssl/certs/domain.chained.crt" --keyfile "/etc/ssl/certs/domain.key" 'wsgi:app'

And this is my 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 /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/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;
    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    types_hash_max_size 4096;
    client_body_timeout 999;
    client_header_timeout 999;
    keepalive_timeout 999;
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    fastcgi_connect_timeout 999;
    fastcgi_send_timeout 999;
    fastcgi_read_timeout 999;

    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;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
        location = /404.html {
        }

        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        }
    }


# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2;
#        listen       [::]:443 ssl http2;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }
    include /etc/nginx/sites-enabled/*.conf;
    server_names_hash_bucket_size 64;
}

And my web application conf in the sites-available folder:

server {
        listen 443 ssl;
        server_name domain www.domain;
        ssl_certificate /etc/ssl/certs/domain.chained.crt;
        ssl_certificate_key /etc/ssl/certs/domain.key;
 
        location / {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_pass http://unix:/path/to/sock/webapp.sock;
        }
}

This conf has been symbolically linked. I have no idea why this is not working, as Nginx is not even logging anything! I tried multiple things but I keep gateway the 502 error. Does anyone have any idea? Thanks in advance

0 Answers
Related