1

I am trying to get Nginx and Flask up and running on my local machine.

I can server static files fine with Nginx but I keep hitting a connection problem with uwsgi.

My Nginx:

server {
    listen       80;
    server_name  localhost;

location /static {
    alias /path/to/my/static;
    autoindex  on;
}

location /media {
    alias  /media;
    autoindex  on;
}


location / {
    include uwsgi_params;
    uwsgi_pass unix:/tmp/uwsgi.sock;
    uwsgi_param UWSGI_PYHOME /path/to/my/env;
    uwsgi_param UWSGI_CHDIR /path/to/my/app;
    uwsgi_param UWSGI_MODULE application;
    uwsgi_param UWSGI_CALLABLE app;
}

error_page   404              /404.html;

error_page   500 502 503 504  /50x.html;
location = /50x.html {
    root   /usr/share/nginx/html;
}
}

/etc/uwsgi/apps-available/uwsgi.ini:

[uwsgi]
plugins=python
vhost=true
socket=/tmp/uwsgi.sock

but if I try localhost/, I find this in the Nginx error logs:

[error] 1021#0: *16 connect() to unix:/tmp/uwsgi.sock failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:/tmp/uwsgi.sock:", host: "localhost"

I have tried changing permissions, but I can't seem to solve this one. What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186
Darwin Tech
  • 18,449
  • 38
  • 112
  • 187
  • Since others have this working, the formula in your title is wrong. It must be `flask + nginx + your configuration = connection refused` ;-) – baao May 29 '20 at 13:05

1 Answers1

0

Some ideas:

  • Are you sure uWSGI is running?
  • Did you reload it / restart it after editing that file in apps-available?
  • Judging by the name (apps-available) is there another directory called apps-enabled, where you need to put a symlink to the file in apps-available?
  • Is there a logfile where uWSGI is writing its own error messages?

You can use lsof with the uWSGI pid (eg. lsof -n -p 12345) to see what files it has opened. You should see the socket in /tmp as well as a logfile somewhere in /var/log

Tobia
  • 17,856
  • 6
  • 74
  • 93
  • Thanks for these points. So, my uwsgi.ini file is symlinked through sites enabled. If I try and run the file using `uwsgi uwsgi.ini`, I get the following output: http://dpaste.com/hold/1022942/ If I run it as root it seems fine. Is it the permissions or the ownership perhaps that needs changing? – Darwin Tech Mar 14 '13 at 17:39
  • Yes, it's a permission problem on the socket file, /tmp/uwsgi.sock. `unlink(): Permission denied [socket.c]` means that it's trying to remove the existing socket to create a new one, and it fails due to wrong permissions. `bind(): Address already in use [socket.c]` means that it cannot listen on that socket, probably for the same reason. – Tobia Mar 14 '13 at 21:59
  • 3
    Usually a socket is not created in /tmp, because anybody or anything else on that server can create a file named `/tmp/uwsgi.sock` and take your seat. You should make a directory where only the user that will run uwsgi can write, and tell uwsgi to create its socket there. Such a directory is usually made under `/var/run` for system daemons, or you home directory for personal services. – Tobia Mar 14 '13 at 22:01