0

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.

Kyle Calica-St
  • 103
  • 1
  • 6

3 Answers3

1

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.

Wedge Martin
  • 111
  • 3
  • How does this help with the question asked? – gxx Jul 29 '16 at 22:50
  • could you please explain an upstream? and why this would solve my problem? I thought the ini file takes care of that – Kyle Calica-St Jul 29 '16 at 23:05
  • Upstream is for defining an upstream handler for a virtual host. The ini file takes care of things from the uwsgi side, but uwsgi is upstream from nginx. It's a different approach to the config, but it's what has worked for me in many situations. – Wedge Martin Jul 31 '16 at 00:35
0

Which user is running uwsgi? In your config file, you've specified:

chmod-sock = 660

This means, the user in question and his group are able to read / write, all other users aren't able to do anything. Try changing it to

chmod-sock = 666

and see how this goes.

A better alternative might be to put the uwsgi user and the one running nginx into the same group.

Edit: Does the socket exist at the path you've specified? If not, change the socket directive so that it uses a full / absolute path.

gxx
  • 5,591
  • 2
  • 22
  • 42
0

These are some of the troubleshooting steps you might need to perform -

1). Check the uwsgi logs, usually under /var/log/uwsig - look for errors and make sure the app you specified in your configuration as module = wsgi:app is loading.

2). Check for any permission issues. To make this easier, start uwsgi under the same user/group as nginx. You can add the line below to your uwsgi config -

uid = www-data
gid = www-data

3). You might need to install uwsgi-plugin-python package and add plugins = python to your uwsgi config. I had cases where the app was not loading due to missing plugin.

Daniel t.
  • 9,291
  • 1
  • 33
  • 36
  • hmm i dont have a var/log/uwsgi is that because i only installed it through pip? – Kyle Calica-St Aug 01 '16 at 20:13
  • Possibly, you can still specify the log file for uwsgi using `logto = /var/log/uwsgi/%n.log` for instance. Make sure the user under which uwsgi is running has read/write access to the log directory/file. – Daniel t. Aug 02 '16 at 22:14
  • its actually my service that i made to execute uwsgi that is failing. the service is never up. – Kyle Calica-St Aug 04 '16 at 04:28
  • what do the logs say? Add `--logto2 /tmp/mylog.log` to your `ExecStart` line and restart the service and report what you see in the /tmp/mylog.log file. – Daniel t. Aug 04 '16 at 23:08