# Python on Nginx

**URL:** <https://www.ruby-forum.com/t/python-on-nginx/212707>\
**Category:** NGINX\
**Created:** [October 11, 2011, 4:12pm UTC](https://www.ruby-forum.com/t/python-on-nginx/212707 "2011-10-11T16:12:10Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [October 11, 2011, 4:12pm UTC](https://www.ruby-forum.com/t/python-on-nginx/212707/1 "2011-10-11T16:12:10Z")

</div>

The instruction given on [http://wiki.nginx.org/PythonFlup](http://wiki.nginx.org/PythonFlup) is to install  
nginx supporting python. But I have a working Nginx with php-fpm  
supporting PHP. Now I want to add support of python too. Thus, my config  
is slightly different. I successfully installed Python 2.6, Django and  
python-flup. Everything went good, but python does not work on web (when  
visiting test.py, it downloads the file instead of running the script).

How should modify the nginx config to support python?

Posted at Nginx Forum:

> **[Python on Nginx](https://forum.nginx.org/read.php?2,216514,216514#msg-216514)**
>
> The instruction given on http://wiki.nginx.org/PythonFlup is to install nginx supporting python. But I have a working Nginx with php-fpm supporting PHP. Now I want to add support of python too. Thus, my config is slightly different. I successfully...

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [October 11, 2011, 8:10pm UTC](https://www.ruby-forum.com/t/python-on-nginx/212707/2 "2011-10-11T20:10:02Z")

</div>

On Tue, Oct 11, 2011 at 10:11:14AM -0400, etrader wrote:

Hi there,

> The instruction given on [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/PythonFlup) is to install  
> nginx supporting python. But I have a working Nginx with php-fpm  
> supporting PHP. Now I want to add support of python too. Thus, my config  
> is slightly different.

nginx doesn’t know (or care) that you are using php or python or  
anything  
else. All it knows is how you have configured different location{}  
blocks.

Each request is handled by one location, so you must make sure that you  
have the right configuration in the location that your request matches.

> How should modify the nginx config to support python?

If you are currently using php-fpm, you probably have something like

location [something that matches urls that should be handled by php] {  
fastcgi\_pass [your php-fastcgi server];  
}

So now that you are adding a separate fastcgi server that is associated  
with python, you’ll want to add a new location block like

location [something that matches urls that should be handled by python]  
{  
fastcgi\_pass [your python-fastcgi server];  
}

The details depend on what you’re trying to do. The documentation on  
“location”, or the debug log, should show you which location is being  
used for each request – if it’s not the right one, you’ll need to  
adjust the config.

Good luck with it,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [October 11, 2011, 8:44pm UTC](https://www.ruby-forum.com/t/python-on-nginx/212707/3 "2011-10-11T20:44:03Z")

</div>

My server is as  
server { server\_name .domain.com;  
listen 80;  
root /home/domain.com;  
index index.html index.php;  
location ~ .php$ {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.php;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
include /opt/nginx/conf/fastcgi\_params;  
}  
}

You mean to add another location as  
location ~ .py$ {  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_index index.py;  
fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
include /opt/nginx/conf/fastcgi\_params;  
}

Posted at Nginx Forum:

> **[Re: Python on Nginx](https://forum.nginx.org/read.php?2,216514,216533#msg-216533)**
>
> The instruction given on http://wiki.nginx.org/PythonFlup is to install nginx supporting python. But I have a working Nginx with php-fpm supporting PHP. Now I want to add support of python too. Thus, my config is slightly different. I successfully...

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [October 11, 2011, 9:06pm UTC](https://www.ruby-forum.com/t/python-on-nginx/212707/4 "2011-10-11T21:06:58Z")

</div>

This is what I’m using with Debian Squeeze & fcgiwrap installed, not  
just  
for python.

location ~ (.cgi|.py|.sh|.pl|.lua)$ {  
gzip off;  
root /var/www/$server\_name;  
autoindex on;  
fastcgi\_pass unix:/var/run/fcgiwrap.socket;  
include /etc/nginx/fastcgi\_params;  
fastcgi\_param DOCUMENT\_ROOT /var/www/$server\_name;  
fastcgi\_param SCRIPT\_FILENAME  
/var/www/$server\_name$fastcgi\_script\_name;  
}

Also I chmod +x the python scripts and add  
#! /usr/bin/python  
to the beginning of the python script.  
Change the path if python is located elsewhere.

On Tue, 11 Oct 2011 19:59:50 +0100, Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [October 11, 2011, 9:00pm UTC](https://www.ruby-forum.com/t/python-on-nginx/212707/5 "2011-10-11T21:00:09Z")

</div>

On Tue, Oct 11, 2011 at 02:43:32PM -0400, etrader wrote:

> You mean to add another location as  
> location ~ .py$ {  
> fastcgi\_pass 127.0.0.1:9000;  
> fastcgi\_index index.py;  
> fastcgi\_param SCRIPT\_FILENAME $document\_root$fastcgi\_script\_name;  
> include /opt/nginx/conf/fastcgi\_params;  
> }

If all of your python urls end in .py, yes.

Although you probably don’t want 127.0.0.1:9000, since that’s  
your php-fastcgi server. You’ll want whatever your python-fastcgi  
server address is: probably 127.0.0.1:8080, if you followed

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [October 12, 2011, 11:10am UTC](https://www.ruby-forum.com/t/python-on-nginx/212707/6 "2011-10-12T11:10:33Z")

</div>

On Tue, Oct 11, 2011 at 07:09:32PM +0100, Francis D. wrote:

> else. All it knows is how you have configured different location{} blocks.  
> }  
> used for each request – if it’s not the right one, you’ll need to  
> adjust the config.
> 
> Good luck with it,

BTW, I know that some people prefer to use uWSGI server for python.

–  
Igor S.

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [October 13, 2011, 2:04am UTC](https://www.ruby-forum.com/t/python-on-nginx/212707/7 "2011-10-13T02:04:34Z")

</div>

On Oct 12, 2011, at 5:09 AM, Igor S. wrote:

> BTW, I know that some people prefer to use uWSGI server for python.

I prefer frameworks like gunicorn that use plain HTTP.

–Brian

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [October 11, 2011, 9:33pm UTC](https://www.ruby-forum.com/t/python-on-nginx/212707/8 "2011-10-11T21:33:37Z")

</div>

On Tue, 2011-10-11 at 20:06 +0100, Keith Fernie wrote:

> This is what I’m using with Debian Squeeze & fcgiwrap installed, not just  
> for python.

fcgiwrap is a completely different beast and doesn’t apply one whit to  
running Django. You need to follow the directions you linked to at

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

1. Set up a location for your PHP applications
2. Set up a _different_ location for your Django application and use the  
directions from the wiki for getting it configured.

Cliff

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [October 13, 2011, 5:56am UTC](https://www.ruby-forum.com/t/python-on-nginx/212707/9 "2011-10-13T05:56:47Z")

</div>

> On Oct 12, 2011, at 5:09 AM, Igor S. wrote:
> 
> > BTW, I know that some people prefer to use uWSGI server for python.
> 
> I prefer frameworks like gunicorn that use plain HTTP.

uWSGI suggests using the uwsgi protocol to improve performance and to  
add  
some specific feature, but you can run it in http, fastcgi and zeromq  
mode.

> 

–  
Roberto De Ioris

> **[unbit - Overview](https://github.com/unbit)**
>
> makers of uWSGI and many more open source projects - unbit

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [October 13, 2011, 3:11am UTC](https://www.ruby-forum.com/t/python-on-nginx/212707/10 "2011-10-13T03:11:10Z")

</div>

On Wed, 2011-10-12 at 13:09 +0400, Igor S. wrote:

> > nginx doesn’t know (or care) that you are using php or python or anything  
> > fastcgi\_pass [your php-fastcgi server];  
> > “location”, or the debug log, should show you which location is being  
> > used for each request – if it’s not the right one, you’ll need to  
> > adjust the config.
> > 
> > Good luck with it,
> 
> BTW, I know that some people prefer to use uWSGI server for python.

[http://blog.zacharyvoase.com/2010/03/05/django-uwsgi-nginx/](http://blog.zacharyvoase.com/2010/03/05/django-uwsgi-nginx/)

There’s lots of articles on setting up Django + uwsgi + Nginx out there.

Cliff

---

<div class="post-metadata">

**Author:** ![etrader](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/b5e925/32.png) [@etrader](https://www.ruby-forum.com/u/etrader)\
**Post date:** [October 13, 2011, 1:00pm UTC](https://www.ruby-forum.com/t/python-on-nginx/212707/11 "2011-10-13T13:00:27Z")

</div>

On Oct 12, 2011, at 11:49 PM, Roberto De Ioris wrote:

> uWSGI suggests using the uwsgi protocol to improve performance and to add  
> some specific feature, but you can run it in http, fastcgi and zeromq  
> mode.

Of course uWSGI suggest using uwsgi 😉

HTTP is easier to debug and do other “normal things” with it. I just  
use the normal nginx proxy.
