Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask, nginx, and uwsgi

My flask app looks like this...
myapp.py

from flask import Flask  
app = Flask(__name__) 

@app.route("/")  
def hello():  
   return "Hello World!"  

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

My nginx setup

server {
         root /home/admin.jeremylspencer.com;
         server_name admin.jeremylspencer.com;

         location / { try_files $uri @yourapplication; } 
         location @yourapplication {
                 include uwsgi_params;
                 uwsgi_pass unix:/tmp/uwsgi.sock;
         }       

         #error_page 404 /404.html;
         #error_page 500 502 503 504 /50x.html;
         location = /50x.html {
                 root /usr/share/nginx/www;
         }        
         location ~ /\.ht {
                 allow all;
         }
}

Then finally I restart nginx and run this:

sudo uwsgi -s /tmp/uwsgi.sock --module myapp --callable app And this is the output

*** Starting uWSGI 1.4.3 (64bit) on [Mon Dec 10 15:41:00 2012] ***
compiled with version: 4.6.3 on 10 December 2012 13:06:15
os: Linux-3.2.0-32-generic #51-Ubuntu SMP Wed Sep 26 21:33:09 UTC 2012
nodename: jeremylspencer.com
machine: x86_64
clock source: unix
detected number of CPU cores: 2
current working directory: /home/admin.jeremylspencer.com
detected binary path: /usr/local/bin/uwsgi
uWSGI running as root, you can use --uid/--gid/--chroot options
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) *** 
*** WARNING: you are running uWSGI without its master process manager ***
your processes number limit is 31285
your memory page size is 4096 bytes
detected max file descriptor number: 1024
lock engine: pthread robust mutexes
uwsgi socket 0 bound to UNIX address /tmp/uwsgi.sock fd 3
Python version: 2.7.3 (default, Aug  1 2012, 05:25:23)  [GCC 4.6.3]
*** Python threads support is disabled. You can enable it with --enable-threads ***
Python main interpreter initialized at 0x1dfa790
your server socket listen backlog is limited to 100 connections
mapped 72392 bytes (70 KB) for 1 cores
*** Operational MODE: single process ***
WSGI app 0 (mountpoint='') ready in 0 seconds on interpreter 0x1dfa790 pid: 13645 (default app)
*** uWSGI is running in multiple interpreter mode ***
spawned uWSGI worker 1 (and the only) (pid: 13645, cores: 1)

But yet all I get is a 502 error... any how can i fix this?

like image 484
Jeremy Spencer Avatar asked Dec 10 '12 20:12

Jeremy Spencer


People also ask

What is uWSGI Nginx Flask Docker?

This Docker image allows you to create Flask web applications in Python that run with uWSGI and Nginx in a single container. The combination of uWSGI with Nginx is a common way to deploy Python Flask web applications. It is widely used in the industry and would give you decent performance.

What is the difference between Nginx and uWSGI?

Nginx is “a web server which can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache”. uWSGI is an implementation of the WSGI spec, which describes how a web server should communicate with a web app, which makes uWSGI also a type of web server.

Is Nginx a uWSGI?

Nginx natively includes support for upstream servers speaking the uwsgi protocol since version 0.8.


1 Answers

unix sockets are filesystem objects, so nginx need write permissions to /tmp/uwsgi.sock

You are running uWSGI as root (why ???) so /tmp/uwsgi.sock will be owned by root while nginx generally runs as nobody or www-data.

If you do not want to take in account permissions just use TCP sockets, but obviously do not run your app as root.

like image 77
roberto Avatar answered Sep 20 '22 06:09

roberto