Nginx and uWSGI Flask app Connection Refused

deploymentflasknginxuwsgiwsgi

Trying to set up a Flask application with uWSGI and Nginx.

I keep getting a 502 Bad Gateway error (as far as I know this means uWSGI and Nginx can't communicate correctly)

This is the line that keeps showing up in my /var/log/nginx/error.log:

2016/07/29 17:07:12 [error] 24958#24958: *2 connect() to unix:/home/lit/howlit/how_lit_restapi.sock failed (111: Connection refused) while connecting to upstream, client:

This is my project directory and it's permissions:

(env) root@digitalocean:/home/lit/howlit# ls -l 
total 24
drwx---r-x 6 lit www-data 4096 Jul 29 11:47 env
-rwx---r-x 1 lit www-data  141 Jul 29 17:00 howlit.ini
-rwx---r-x 1 lit www-data 1175 Jul 29 11:52 how_lit_restapi.py
-rwx---r-x 1 lit www-data 1781 Jul 29 11:54 how_lit_restapi.pyc
srwx---r-x 1 lit www-data    0 Jul 29 16:46 how_lit_restapi.sock
-rwx---r-x 1 lit www-data   73 Jul 29 11:54 wsgi.py
-rwx---r-x 1 lit www-data  218 Jul 29 11:54 wsgi.pyc

This is my howlit.ini file:

(env) root@digitalocean:/home/lit/howlit# cat howlit.ini
[uwsgi]
module = wsgi:app


master = true
processes = 5


socket = how_lit_restapi.sock

chmod-sock = 660

vaccum = true

die-on-term = true

this is my /etc/nginx/sites-available/how_lit file:

(env) root@digitalocean:/home/lit/howlit# cat /etc/nginx/sites-available/how_lit 
server {
    listen 80;
    server_name XXX.XX.XX.XXX;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/lit/howlit/how_lit_restapi.sock;
    }

}

*the xxxs are to protect my server IP; there is a real IP address there.

Here is the nginx conf file:

user lit;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

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

Permissions of directory with socket:

root@digitalocean:~# ls -l /home/lit/howlit/

total 24
drwx---r-x 6 lit www-data 4096 Jul 29 11:47 env
-rwx---r-x 1 lit www-data  141 Jul 29 19:01 howlit.ini
-rwx---r-x 1 lit www-data 1175 Jul 29 11:52 how_lit_restapi.py
-rwx---r-x 1 lit www-data 1781 Jul 29 11:54 how_lit_restapi.pyc
srwx---r-x 1 lit www-data    0 Jul 29 16:46 how_lit_restapi.sock
-rwx---r-x 1 lit www-data   73 Jul 29 11:54 wsgi.py
-rwx---r-x 1 lit www-data  218 Jul 29 11:54 wsgi.pyc

I've tried changing permissions and the files alot. What is my problem here? Why is the connection being refused???

UPDATE:

So when I run my application manually:

root@digitalocean:/home/lit/howlit# python how_lit_restapi.py
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

and I go to my ip address at:

XXX.XX.XX:5000 

my app shows up, so I know uwwsgi is running and serving. But the nginx hand off is not work.

Here is my how_lit.service file as well.

root@digitalocean:/home/lit/howlit# vim /etc/systemd/system/how_lit.service 

[Unit]
Description=uWSGI instance to serve how lit rest api
After=network.target


[Service]
User=lit
Group=www-data
WorkingDirectory=/home/lit/howlit/
Environment="PATH=/home/lit/howlit/env/bin"
ExecStart=/home/lit/howlit/env/bin/uwsgi --ini howlit/howlit.ini

[Install]
WantedBy=multi-user.target

Okay then now I think I have it. The how_lit service is failing.

Best Answer

I'd move towards specifying an 'upstream' where you set your server/socket and reference that in your virtual host, like:

upstream django {
  server unix:///var/run/whatever.sock; # for a file socket
}

server {
   listen   80
   location / {
       uwsgi_pass django;
   }
}

You can use lsof to make sure things are writing to / reading from the socket.