pythonnginxuwsgino-framework

Simple nginx + uWSGI setup issue


I have a ton of python scripts that (for convenience mostly) generate html output, so naturally I would like to use a very simple setup to host the scripts in my current test environment. Setting up projects in say, Django, Flask, web2py or whatever, for every silly thing I need is too much of a hassle, I just want to write a .py and browse it without configuring anything else, pretty much as with php.

I've been struggling with this for a couple of days because I'm not sure exactly what is wrong, so I'll just post my current attempt with the config files:

nginx:

location ~ \.py$ {
    uwsgi_pass unix:///path/to/socket;
    uwsgi_param SCRIPT_NAME $uri;
    include uwsgi_params;
}

uWSGI

[uwsgi]
plugins = python3
py-auto-reload = 1 #So I dont have to reload the service every time

test.py

def application(env, start_response):
    start_response('200 OK', [('Content-Type','text/html')])
    return b"Hello World"

I have many many MANY variations in the nginx and uwsgi config but I always get:

uWSGI Error

Python application not found

And the log always shows stuff like these:

[pid: 10423|app: -1|req: -1/10] 10.0.20.101 () {42 vars in 675 bytes} [Sun Oct  6 08:25:51 2013] GET /test.py => generated 48 bytes in 0 msecs (HTTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
-
Sun Oct  6 08:26:44 2013 - unable to load app 0 (mountpoint='/var/www/test.py') (callable not found or import error)
[pid: 10423|app: -1|req: -1/12] 10.0.20.101 () {44 vars in 707 bytes} [Sun Oct  6 08:26:44 2013] GET /test.py => generated 48 bytes in 0 msecs (H
TTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)
-
Sun Oct  6 07:22:36 2013 - unable to load app 0 (mountpoint='/test.py') (callable not found or import error)
[pid: 10423|app: -1|req: -1/12] 10.0.20.101 () {44 vars in 707 bytes} [Sun Oct  6 08:26:44 2013] GET /test.py => generated 48 bytes in 0 msecs (H
TTP/1.1 500) 2 headers in 63 bytes (0 switches on core 0)

Solution

  • This is not how WSGI apps are supposed to work. They are generally long running application to which nginx passes requests to. You are asking for a CGI-like setup, so you have to use the uWSGI CGI module (http://uwsgi-docs.readthedocs.org/en/latest/CGI.html). Those apps obviously have to be CGI-compliant. I am not sure this is what you want, but i strongly suggest you to invest a bit of time in how WSGI app works as this is basically how everything else is working nowadays (perl/PSGI, ruby/Rack...)

    Note: you will probably find (uWSGI) configurations of people managing exactly what you are trying to accomplish, but they are far from the normal way.