Report this

What is the reason for this report?

bad gateway flask nginx uwsgi

Posted on April 12, 2021

I’m trying to expand from a basic one file flask app and restructure into a proper application. I followed and initial set up on a digital ocean server from a tutorial I found here: https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uswgi-and-nginx-on-ubuntu-18-04

Then I tried to restructure the application following this tutorial: https://pythonise.com/series/learning-flask/flask-application-structure

I am getting a 502 bad gateway error.

my current folder structure is as follows:

sieroApp
       |
       |
       |-sieroApp
       |      |
       |      |-- __init__.py
       |      |__ views.py
       |
       |-sieroApp.ini
       |-sieroAppenv
       |-wsgi.py

content of files: init.py:

from flask import Flask
app = Flask(__name__)

from app import views
content of views.py:

from app import app

@app.route("/")
def index():
    return "Hello world"

@app.route("/about")
def about():
    return "All about Flask"

content of sieroApp.ini:

[uwsgi]
module = wsgi:app

master = true
processes = 5

socket = sieroApp.sock
chmod-socket = 660
vacuum = true

die-on-term = true

content of wsgi.py:

from sieroApp import app

if __name__ == "__main__":
        app.run()

content of etc/systemd/system/sieroApp.service:

[Unit]
Description=uWSGI instance to serve sieroApp
After=network.target

[Service]
User=max
Group=www-data
WorkingDirectory=/home/max/sieroApp/sieroApp
Environment="PATH=/home/max/sieroApp/sieroAppenv/bin"
ExecStart=/home/max/sieroApp/sieroAppenv/bin/uwsgi --ini sieroApp.ini

[Install]
WantedBy=multi-user.target
```

content of etc/nginx/sites-available/sieroApp:

```nginx
    server {
        listen 80;
        server_name pixelbots.net www.pixelbots.net;

    location / {
        include uwsgi_params;
        uwsgi_pass unix:/home/max/sieroApp/sieroApp/sieroApp.sock;
    }
}
```


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi @eventhorizon02,

I can’t see any issues with the files you’ve provided. Having said that, the issue might be somewhere else, what does your Nginx error_log say?

Usually, when you are not sure what exactly the error is and you are seeing something generic like 502 Bad Gateway, the best option is always to go and check the the service’s error log. Regarding Nginx, you should be able to see the log at /var/log/.

Please check it there and see if you can further troubleshoot your application. If you are not sure how to continue, post the error here and we’ll try to help out!

Regards, KFSys

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.