Skip to content

Instantly share code, notes, and snippets.

@tusharbabbar
Last active August 29, 2015 14:22
Show Gist options
  • Save tusharbabbar/9b1f913a360848e19662 to your computer and use it in GitHub Desktop.
Save tusharbabbar/9b1f913a360848e19662 to your computer and use it in GitHub Desktop.
Flask with uWSGI behind Nginx

The Flask App -- app.py

from flask import Flask

app = Flask(__name__)

@app.route('/', methods=['GET'])
def hello():
  return 'HELLO WORLD'

uWSGI configuration -- wsgi.ini

[uwsgi]
chdir = /path/to/app.py
module = app
callable = app

master = true
processes = 5

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

die-on-term = true

Upstart configuration

Create file for configuration. sudo vim /etc/init/myproject.conf

description "uWSGI server instance configured to serve myproject"

start on runlevel [2345]
stop on runlevel [!2345]

setuid ubuntu
setgid www-data

env PATH=/path/to/virtualenv/bin
chdir /path/to/app
exec uwsgi --ini wsgi.ini

Nginx Configuration -- /etc/nginx/sites-available

upstream myproject{
  server unix:/path/to/myproject.sock;
}

server {
    listen       80;
    server_name  _;

    error_log /var/log/nginx/myproject-error.log;
    access_log /var/log/nginx/myproject-access.log combined;

    location / { try_files $uri @yourapplication; }
    location @yourapplication {
      uwsgi_pass myproject;
      include uwsgi_params;
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment