Latest Version: 0.9.6.2
  Dashboard > Pylons Cookbook > ... > Deployment > Running Pylons with NGINX
  Pylons Cookbook Log In | Sign Up   View a printable version of the current page.  
  Running Pylons with NGINX
Added by Pradeep Gowda, last edited by Andreas Klauer on Feb 22, 2008  (view change) show comment
Labels: 

Name Space Section Version Status Reviewed Author(s)
NGINX for Pylons Pylons Cookbook Deployment 1.0 Draft False 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;

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

Posted by Max Ischenko at Mar 26, 2007 07:24 | Permalink

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; }

Posted by Jamie Becker at Nov 29, 2007 18:12 | Permalink

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.

Posted by Jamie Becker at Nov 29, 2007 18:14 | Permalink

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.

Posted by Jon Rosebaugh at Dec 31, 2007 19:13 | Permalink

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.

Posted by Andreas Klauer at Feb 22, 2008 16:40 | Permalink

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

Posted by Shannon -jj Behrens at May 22, 2008 21:40 | Permalink
Site running on a free Atlassian Confluence Open Source Project License granted to Pylons. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.3.3 Build:#645 Feb 13, 2007) - Bug/feature request - Contact Administrators
Top