Nginx and uWSGI Flask app Connection Refused

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) [email protected]:/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 -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 -rwx---r-x 1 lit www-data  218 Jul 29 11:54 wsgi.pyc 

This is my howlit.ini file:

(env) root[email protected]:/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) [email protected]:/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/;  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/*; } 

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


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.

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.

Category: nginx Time: 2016-07-29 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.193 (s). 12 q(s)