nginx

Running Plone with Nginx

Spend few hours to get Plone running behind Nginx through proxy_pass. It supposed to be as simple as:-

server {
    listen      80;
    server_name cvt.int-prokab.com;
    root html;

    rewrite ^/(.*)$ /cvt last;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

The plone site are running on port 8080 at 127.0.0.1:8080/cvt. I want it to be exposed to the outside as http://cvt.int-prokab.com/ but it turn out that Plone generate all url on the site as 127.0.0.1:8080/cvt - the address behind and not the exposed public url. Reading explanation on ZMI Virtual Hosting interface:-

The VHM doesn’t do anything unless it sees one of the following special path elements in a URL: VirtualHostBase sets the protocol and host, while VirtualHostRoot sets the path root. If the URL path of a request begins with ”/VirtualHostBase/http/www.buystuff.com”, for instance, then URLs generated by Zope will start with http://www.buystuff.com. Since the port number was not specified, it is left unchanged. If your Zope is running on port 8080, and you want generated URLs not to include this port number, you must use ”/VirtualHostBase/http/www.buystuff.com:80”. If the URL contains VirtualHostRoot, then all path elements up to that point are removed from generated URLs. For instance, a request with path ”/a/b/c/VirtualHostRoot/d” will traverse “a/b/c/d” and then generate a URL with path /d.

Took me sometimes and hair pulling before I could understand how it would work. The rewrite supposed to be like this:-

rewrite ^/(.*)$ /VirtualHostBase/http/cvt.int-prokab.com:80/cvt/VirtualHostRoot/$1 last;

Test drive new 'Engine'

Nginx (pronounced: EngineX) is a lightweight http server written by Russian, Igor Sysoev. Initially, most of the documentation and discussions were in Rusian but now there’s an English wiki set up for English speaking users. So, through out the day I took a plunge swapping out lighttpd with nginx and watched how it’s going. It’s quite satisfying. I let the process untouched for hours while the site being hit as usual and I can concluded that no memory-leaking type of problem as in lighttpd seem to appear.

The setup was a non-trivial, especially for the PHP/FastCGI thingy but after properly following the example in the wiki and some scattered blog post on the net I got it working. What can I say, nginx configuration syntax is way much verbose compared to lighttpd or apache. It’s programming langguage like syntax make it easy to follow and even vim autoindent work’s perfectly well to auto indent the configuration file. The structure can be logically divided into http, server and location context. Virtual host is just another server context with different server_name directive and location can be nested within any of the main context.

URL rewriting especially for Drupal clean url was a little bit easier. Nginx does provide conditional rewriting so it’s easy to emulate apache rewrite rules come with the Drupal distribution. This forum post on Drupal.org illustrated it well:-

http://drupal.org/node/110224

Unlike Lighttpd, nginx does not automatically spawn FastCGI process for us so we need to invoke PHP as FastCGI externally and then have nginx proxied the request to the process. As suggested by others, I used the spawn-fcgi program from lighttpd to handle the spawning process. Since nginx does not provide any init scripts, I modified the lighttpd init script to handle nginx and PHP FastCGI spawning during system start up and handy daemon start, restart, stop. Just as a note to myself, on Ubuntu (and Debian) init scripts were handled by update-rc.d program: update-rc.d nginx defaults would created proper symlink from the appropriate run level (/etc/rcN.d) to nginx script in /etc/init.d.

Here are some resources on the net that help me to get nginx running on my system:-

http://wiki.codemongers.com/
http://www.nslu2-linux.org/wiki/HowTo/DeployPHPWebAppUsingFastCGI
http://www.google.com.my/search?q=nginx+php

While the early motivation comes from:-

http://hostingfu.com/article/running-drupal-with-clean-url-on-nginx-or-lighttpd http://forum.slicehost.com/comments.php?DiscussionID=787&page=1#Item_10

Syndicate content