PylonsHQ.

Layout: Fixed-width

Running Pylons with NGINX

Skip to end of metadata
Go to start of metadata
Unknown macro: {metadata-list}
Name NGINX for Pylons
Space Pylons Cookbook
Section Deployment
Version 1.0
Status Draft
Reviewed False
Author(s) Pradeep Gowda

Introduction

This article describes how to configure an NGINX server to proxy to a Pylons application run using the paster serve command.

location / {
            include /usr/local/nginx/conf/proxy.conf;
            proxy_pass  http://127.0.0.1:8080;
            proxy_redirect  default; 
        }

The complete configuration is listed below. Copy this file to /etc/nginx or /usr/local/nginx/conf/ depending on your installation path.

nginx.conf

worker_processes  2;
events {
    worker_connections  1024;
}


http {
    include       conf/mime.types;
    default_type  application/octet-stream;
    access_log    logs/mysite.access.log ;
    error_log     logs/mysite.error.log;
    gzip          on;
    sendfile      on;
    tcp_nopush    on;
    tcp_nodelay   on;
    keepalive_timeout  75 20;

    server {
        listen 80;
        server_name mysite.com;        
        
        #site runs on Pylons 
        location / {
            include /usr/local/nginx/conf/proxy.conf;
            proxy_pass  http://127.0.0.1:8080;
            proxy_redirect  default; 
        }
                
        #Blog runs on wordpress, 
        #so pass the control to apache running on port 81
        location /blog{
            proxy_pass http://127.0.0.1:81;
            proxy_redirect default;
        }
        
        #URL Rewrite example
        rewrite ^/feed/(.*)$ /blog/feed/$1 permanent;        
    }
}

The proxy file referred above is given below.

proxy.conf

proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr;
proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffer_size       4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

Labels

nginx nginx Delete
deploy deploy Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.
  1. Mar 26, 2007

    Max Ischenko says:

    Shouldn't this page belong to "Deployment" section of the cookbook?

    Shouldn't this page belong to "Deployment" section of the cookbook?

  2. Nov 29, 2007

    Jamie Becker says:

    It will be faster to use FastCGI instead of proxying. Nginx just hands off the f...

    It will be faster to use FastCGI instead of proxying. Nginx just hands off the fastcgi connection to a FastCGI server running on port x, and then a FastCGI server (pylons) responds on that port. Check the documentation for nginx to find out how to set up FastCGI, but here's a start:

    server {
    listen 80;
    server_name localhost;

    access_log /var/log/nginx/localhost.access.log;

    location /

    Unknown macro: { # root /var/www/nginx-default; # index index.html index.htm; include /etc/nginx/fastcgi_params; }
    1. Nov 29, 2007

      Jamie Becker says:

      Ok, that paste didn't go so well. Trying again. In your nginx.conf, in the locat...

      Ok, that paste didn't go so well. Trying again. In your nginx.conf, in the location / { section,
      enter some fastcgi_params (or include them in another file as shown above):

      fastcgi_param QUERY_STRING $query_string;
      fastcgi_param REQUEST_METHOD $request_method;
      fastcgi_param CONTENT_TYPE $content_type;
      fastcgi_param CONTENT_LENGTH $content_length;

      fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;

      1. for php:
      2. fastcgi_param SCRIPT_FILENAME /scripts/$fastcgi_script_name;
        fastcgi_param SCRIPT_NAME $fastcgi_script_name;
        fastcgi_param REQUEST_URI $request_uri;
        fastcgi_param DOCUMENT_URI $document_uri;
        fastcgi_param DOCUMENT_ROOT $document_root;
        fastcgi_param SERVER_PROTOCOL $server_protocol;

      fastcgi_param GATEWAY_INTERFACE CGI/1.1;
      fastcgi_param SERVER_SOFTWARE nginx;

      fastcgi_param REMOTE_ADDR $remote_addr;
      fastcgi_param REMOTE_PORT $remote_port;
      fastcgi_param SERVER_ADDR $server_addr;
      fastcgi_param SERVER_PORT $server_port;
      fastcgi_param SERVER_NAME $server_name;

      1. PHP only, required if PHP was built with --enable-force-cgi-redirect
      2. fastcgi_param REDIRECT_STATUS 200;

      fastcgi_index index;
      fastcgi_pass 127.0.0.1:9000;

      Then configure your Pylons to listen for FastCGI requests instead of pure HTTP requests. Search for fastcgi in the above search box on this site.

    2. Dec 31, 2007

      Jon Rosebaugh says:

      Where are your benchmarks showing it's faster? In my view, the performance benef...

      Where are your benchmarks showing it's faster? In my view, the performance benefits of FastCGI are small or nonexistent, and are balanced out by the configuration problems and opaqueness of FastCGI.

  3. Feb 22, 2008

    Andreas Klauer says:

    include conf/mime.types; this line tries to include conf/conf/mime.types ...

    include conf/mime.types;

    this line tries to include conf/conf/mime.types for me, so I have to 'include mime.types'. I'm using the latest development version of nginx, maybe something changed, or is it just an error in the pasted config here?

    There was also a } missing at the end of the file so I took liberty to add it to the wiki page directly.

  4. May 22, 2008

    Shannon -jj Behrens says:

    I figured out how to tell nginx to handle static content, but proxy everything e...

    I figured out how to tell nginx to handle static content, but proxy everything else:

        location / {
                root    .../myapp/myapp/public;
                if (!-e $request_filename) {
                    proxy_pass    http://127.0.0.1:5000;
                }
        }

    I have no clue if this is THE RIGHT THING.  I just took a guess and it worked

  5. Jul 09, 2008

    Shannon -jj Behrens says:

    There's a thread on the mailing list about this: http://groups.google.com/group/...
  6. Apr 10, 2010

    Tony Landis says:

    Here is how to do it using uWSGI.

    Here is how to do it using uWSGI.


Powered by Pylons - Contact Administrators