6

UPDATE: I saw questions related to the /tmp directory, and a comment mentioned moving the socket out of /home as well. It didn't fix the problem.

(Additional updates at bottom of post)

I have a ubuntu 16.04 vm on azure I'm using to host a flask application using uwsgi as the server and nginx as the reverse proxy, following this guide...

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-16-04

The problem is I'm getting a 502 Bad Gateway when I try to connect to the server at its ip with a web browser on port 80. When I check the logs it says that nginx cannot find the unix socket I specified in the config file.

The error is...

2016/08/29 23:23:20 [crit] 2792#2792: *120 connect() to unix:///home/me/appname/appname/appname.sock failed (2: No such file or directory) while connecting to upstream, client: ip.goes.in.here, server: here.goes.the.ip, request: "GET /favicon.ico HTTP/1.1", upstream: "uwsgi://unix:///home/me/appname/appname/appname.sock:", host: "the.ip.goes.here", referrer: "http://all.of.teh.ips/"

My server block looks like this...

server {
  listen 80;
  server_name ip.address.goes.here;

  location / {
    include uwsgi_params;
    uwsgi_pass unix:/etc/appname.sock;
  }
}

my app.ini file looks like this...

[uwsgi]

module:wsgi:app

master = true
processes = 5

socket = /etc/appname.sock
chmod-socket = 660
vacuum = true

die-on-term = true

my .service file looks like this...

[Unit]

Description=UWSGI instance to serve app
After=network.target


[Service]

User=me
Group=www-data
WorkingDirectory=/home/me/appname
ExecStart=/home/me/appnam/uwsgi --ini appname.ini


[Install]

WantedBy=multi-user.target

I also have a setup script that does the following (potentially causing problems with duplicate logs?)

sudo ufw allow 'Nginx Full'

export FLASK_APP=appname.py
export APPNAME_SETTINGS=app_settings.cfg

sudo cp service/appname.service /etc/systemd/system/appname.service

sudo cp nginxServerBlock/appname /etc/nginx/sites-available/
sudo ln -s /etc/nginx/sites-available/appname /etc/nginx/sites-enabled
sudo nginx -t #this tests for syntax errors

sudo systemctl start appname
sudo systemctl enable appname

I changed the # of slashes after unix and before home (now etc, the dir was changed), among a bunch of other things I saw that were different in various guides.

I tried to move the socket by changing the location specified in the .ini file and in the server block, but the error is completely unchanged, so nginx seems to be looking for the socket in the same directory.

The directory nginx is searching for to find the socket is not in my configuration anywhere.

[UPDATE] I discovered that there are duplicate logs in the var/log/nginx directory, 8 copies of access.log and 8 copies of error.log. I have been running the setup script after each change (to move the files to the appropriate places after pulling the changes with git) and using daemon reload (I can't remember the exact name) as per the instructions that appear in terminal after running the setup script...could something in my script be causing the duplicates? Could it be possible that I've made some weird duplicate setup with different services by accident, each using a different pair of log files?

Ajacmac
  • 106
  • 1
  • 6
  • It was 3 slashes after unix every time, no matter what the number was I set in my server block file. I'm not sure what's going on. – Ajacmac Aug 30 '16 at 02:04
  • Try move socket outside home directory. – Federico Sierra Aug 30 '16 at 03:22
  • Add the `ls -l /home/me/appname/appname/appname.sock` output to the question. – drookie Aug 30 '16 at 05:16
  • And all parent directories. Sometimes home directory is not accessible to others. – Alexey Ten Aug 30 '16 at 05:34
  • `find / -name appname.sock` tells you where the socket actually exists. – Tero Kilkanen Aug 30 '16 at 09:57
  • I'm moving the socket outside /home, and if that doesn't help I'll try the other suggestions. Does anyone know why this was downvoted? This site honestly confuses me so much sometimes, and I can't even get my reputation high enough to ask what's what in chat. – Ajacmac Aug 30 '16 at 14:45
  • @TeroKilkanen I tried that and just got a longlist of "Permission denied" 's, even when using sudo, the app wasn't running at the time though. I've never tried running multiple simultaneous secure shells, but do I need to have one going to run the server and another to do the search? – Ajacmac Sep 03 '16 at 16:26
  • It depends on if you have set it up to run as a service. You should install it anyway as a service, then you can use standard service tools to start / stop it. You don't need to leave a shell running after starting a service. – Tero Kilkanen Sep 04 '16 at 19:09

3 Answers3

1
User=me
Group=www-data
WorkingDirectory=/home/me/appname
ExecStart=/home/me/appnam/uwsgi --ini appname.ini

That looks like a typo (appnam).

Are you sure the server is running? Can you connect with socat?

MikeyB
  • 38,725
  • 10
  • 102
  • 186
0

ngnix runs as a seperate process, so before you start your application you need to stop the ngnix.

sudo service ngnix stop. Otherwise the socket myapp.socket will not be visible to the ngnix. Even though when you do ls /var/www/myapp/myapp.socket did show up in the terminal results for ls command. Because ngnix was started before you run your app using uwsgi myapp.ini command, ngnix's process doesn't know what that socket myapp.socket is . So the trick to avoid this issue is.

  1. Stop ngnix using sudo service ngnix stop
  2. Start your app uwsgi myapp.ini
  3. Start the ngnix sudo service ngnix start
  • This makes no sense to me. Why would the process be unable to see the socket just because the socket didn't exist when the process started? I don't think it even looks for the socket until it receives a request that uses it. – Kef Schecter Dec 12 '21 at 14:42
0

How about modify as

uwsgi_pass unix:/home/me/appname/appname/appname.sock;
Gnought
  • 176
  • 4
  • The last thing I tried was: ' uwsgi_pass unix:/etc/candidateClicker.sock; ' along with changing the appropriate line in the .ini to ' socket = /etc/candidateClicker.sock ' and it still didn't work. The error in the log also seems to be unchanged. – Ajacmac Sep 02 '16 at 01:10