cynical biscuit
cynical biscuit

Reputation: 353

Redirecting dynamic URLS with NGINX and flask

This is my first time using NGINx, and uwsgi. I have read up on several tutorials, mainly this one (https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-14-04), however, it's not particularly helpful when I have dynamic urls and multiple application routes defined in my flask application. For the sake of simplicity, here are my app.py (flask), and my ngingx configurations below:

app.py (simplified version for StackExchange)

from flask import Flask
application = Flask(__name__)

#works!
@application.route("/")
def hello():
    return "<h1 style='color:blue'>Hello There!</h1>"

#DOESN'T WORK
@application.route("/<userId>")
def hello_user():
    return "<h1 style='color:blue'>Hello some user!</h1>"

if __name__ == "__main__":
    application.run(host='0.0.0.0')

nginx configuration:

server {
listen 9090;
server_name myapp.new;

location / {
    include uwsgi_params;
    uwsgi_pass unix:/home/user1/NGINxApp/app.sock;
    }

}

How can I allow dynamic urls (using variable rules) here?

Upvotes: 1

Views: 1042

Answers (1)

cynical biscuit
cynical biscuit

Reputation: 353

I was able to find the solution here:

http://flask.pocoo.org/docs/0.10/deploying/uwsgi/

I changed the nginx configurations slightly, and now it works:

location / { try_files $uri @yourapplication; }
location @yourapplication {
    include uwsgi_params;
    uwsgi_pass unix:/home/user1/NGINxApp/app.sock;    
}

Upvotes: 1

Related Questions