I have a Flask app running in a python virtual environment and working with uWSGI and Nginx.
I want to use systemd socket activation to manage the startup of the app.
here is the relevant configurations:
uwsgi-flask.service
[Unit]
Description=a uWSGI app
After=syslog.target
[Service]
Type=notify
User=www-data
Group=www-data
WorkingDirectory=/home/www-data/my-app
ExecStart=/bin/bash -c "source venv/bin/activate && uwsgi \
--ini flask-uwsgi.ini \
--socket /dev/shm/flask.sock"
Restart=always
KillSignal=SIGQUIT
StandardError=syslog
NotifyAccess=all
uwsgi-flask.socket
[Unit]
Description=Socket for uWSGI app
[Socket]
ListenStream=/dev/shm/flask.sock
SocketUser=www-data
SocketGroup=www-data
SocketMode=0660
[Install]
WantedBy=sockets.target
nginx.conf
server {
listen 80;
server_name localhost;
location / {
root /home/www-data/my-app/public;
try_files $uri @flask;
}
location @flask {
uwsgi_pass unix:/dev/shm/flask.sock;
include uwsgi_params;
}
}
When I started the systemd socket and make requests:
systemctl start uwsgi-flask.socket
The first request always gets a 504 response like below:
504 Gateway Time-out
But the second and subsequent requests are normal. As if the unix socket was not ready when the first request was made.
The weird thing is that if I start the uwsgi-flask.service directly there is no problem:
systemctl start uwsgi-flask.service
What could be the reason for the above problem?