I had 3 apps in a server running on Ubuntu 22.04. The first one was a Django app, the second a Flask appp under a subdomain and the third one a FastAPI under another subdomain. When I tried to add a fourth one (Flask) I don’t know how I messed up but now none of them work. All the apps run perfectly in my computer.
When I try to access the page through the browser in my computer it returns ERR_CONNECTION_REFUSED
and when accessing throught my phone it returns a 502 Bad Gateway nginx/1.18.0 (Ubuntu)
error. The first one doesn’t log anything in the nginx’s error.log
but the second one does.
My current server config is as follows:
server {
server_name host.es www.host.es;
location ~ /.ht { deny all; }
location ~/.git { deny all; }
location = /favicon.ico { access_log off; log_not_found off; }
location /media { alias /var/www/host.es/myapp/media; }
location /static { alias /var/www/host.es/myapp/static; }
location / {
include proxy_params;
proxy_pass http://unix:/run/myapp.sock;
}
# The next configuration applies to the 3 subdomain apps
location /subdomain1 {
include porxy_params;
proxy_set_header SCRIPT_NAME /subdomain1;
proxy_pass https://unix:/run/mysecondapp.sock;
}
}
server {
if ($host = www.host.es) {
return 301 https://$host$request_uri;
}
if ($host =host.es) {
return 301 https://$host$request_uri;
}
listen 80;
server_name onemade.es www.onemade.es;
return 404;
}
My Django service which is the main app is as follows:
[Unit]
Description=Gunicorn for app
Requires=myapp.sock
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/path/to/myapp
ExecStart=/path/to/myapp/env/bin/gunicorn --workers 3 --preload --bind unix:/run/myapp.sock myapp.wsgi:application
[Install]
WantedBy=multi-user.target
My Flask services which are in subdomains are as follows:
[Unit]
Description=Gunicorn for app
Requires=mysecondapp.sock
After=network.target
[Service]
User=www-data
Group=www-data
WorkingDirectory=/path/to/mysecondapp
ExecStart=/path/to/mysecondapp/env/bin/gunicorn --workers 3 --preload --bind unix:/run/mysecondapp.sock web.wsgi:app
[Install]
WantedBy=multi-user.target
All sockets have the same configuration
[Unit]
Description=My apps socket
[Socket]
ListenStream=/run/myapp.sock
SocketUser=www-data
[Install]
WantedBy=sockets.target
And both .sockets
and .service
files are owned by the root
user and stored in /etc/systemd/system/
The .sock
files are found in /run/
and are owned by www-data
.
When running the below commands for all the services and sockets they are active and without any errors and same with nginx.
sudo systemctl start
sudo systemctl enable
sudo systemctl status
Under the sudo journalctl
command everything works fine. When checking the /var/log/nginx/access.log
it shows a 502 error when trying to GET
the page. When checking the /var/log/nginx/error.log
it shows the next line:
connect() to unix:/bin/myapp.sock failed (2: No such file or directory) while connecting to upstream
In the error it seams like nginx is looking for the .sock file in the /bin
directory instead of the /run
directory. I don’t understand why is this happening or why it changed when trying to add a new app.