/ programming

502 Gateway Error running Flask/uWSGI/NGINX on Ubuntu 16.04

I just want to quickly share my experience with this in case somebody runs into this issue.

I recently bought a small VPS from DigitalOcean and have been slowly moving my websites over. This blog, fdraconis.com, was able to port over mostly smoothly. Installing the new ghost v1.0 on a fresh droplet is... mostly painless (just make sure you have your permissions straight). But porting over my other, python/flask based projects was more difficult.

I followed this guide. It's fairly straight forward and quick to run through, and for the most part, things work perfectly.

But when you get to the end where you are using NGINX to access the website, I got a 502 Gateway Error.

An error like this is frustrating because there are so many steps along the way where something could have gone wrong. Looking at pages and pages of frustrated people having the same problem from the same guide seemed especially hopeless because every user seemed to find a different solution that worked for them. Many people seem to have permission issues, and there are different things you can do for that. Maybe make sure your username and the group www-data has permissions for the folder for your app. Maybe make sure that, in your app.ini file, the line chmod-socket = 660 instead reads 666. Unfortunately, none of that worked for me.

If you look at the NGINX error log (which you can peek at running sudo tail -f /var/log/nginx/error.log, you might see that the socket file is not being found. Specifically, I got a message that read

connect() to unix:///...app.sock failed (2: No such file or directory) while connecting to upstream

If you see that, this means that uwsgi failed to run. There should be an actual .sock file in the folder with your app.ini file. If there isn't, then this is not a permission issue.

You can doubly confirm that uwsgi is not working and that the fault is with the service file you created by running systemctl status app.service. If you see it failed, then there is your problem.

It turns out that when I installed uwsgi within the virtual environment, it did not actually install there... Instead, the install location for me went into


You can confirm the install location for you by running which uwsgi.

Where this issue manifests is in the app.service file you create to start and manage uwsgi. There are two lines you will need to adjust: Environment and ExecStart. Basically, change both of those paths to point to where your uwsgi is actually installed. Environment should still end in bin and ExecStart should still end in uwsgi.

For good measure, you might as well point the app.ini file you declare on the ExecStart with its full path, just to make sure no confusion can be had.

In the end, the two lines may look like this:

Environment="PATH=/usr/local/bin" ExecStart=/usr/local/bin/uwsgi --ini /var/www/app/app.ini

After making this change, you will probably need to reload things and start app.service again:

systemctl daemon-reload systemctl start app systemctl enable app

(if you still don't see the app.sock file generated, try running systemctl restart app too!)

After that's all done, hopefully your app will finally be up and running!

I hope this helps prevent a headache somewhere.



I do tactile research—It's a touchy subject. Psych/Neuro grad student and creator of LittleGadget, a channel about Science and Videogames.

Read More