How-to: Bazaar smart server on Nginx

This is a recipe which will teach you how to set-up a Bazaar smart server on top of the great Nginx HTTP server. I will not deal with optimization or fancy setups at all, the goal is just having your Bazaar branches published, feel free to comment if you feel like the proposed setup could be improved.

First of all, you will need the following:

  • An Un*x box. It does not matter which particular incarnation you prefer (*BSD, GNU/Linux, MacOS X…), just make sure the following components work with your operating system of choice.
  • Bazaar 1.0 or newer.
  • Nginx 0.6.x with the latest mod_wsgi builtin (Manlio did a great job bringing WSGI support o life!)
  • Your text editor of choice.

Now, the recipe for a read-only repository of Bazaar branches:

  1. Install Nginx with mod_wsgi. If you are a fortunate Gentoo user, you can use the Nginx ebuilds from my overlay the Connectical Overlay, just make sure you set python USE-flag when emerging www-servers/nginx.
  2. Create a directory where you will store your branches. I myself decided to place them under /home/bzr. Make sure that Nginx will be able of reading its contentse e.g. by making the directory and its contents world-readable.
  3. Write a small Python script which creates a WSGI application for the smart server and save it to /home/bzr/smartserver.wsgi. It should look like the following:
          from bzrlib.transport.http import wsgi
    
          application = wsgi.make_app(
             root="/home/bzr",
             prefix="/bzr",
             path_var="REQUEST_URI",
             readonly=True)
  4. Edit /etc/nginx/nginx.conf and add the directives into a server section, be sure to use the same location as in the prefix variable above:
          location /bzr {
             wsgi_pass /home/bzr/smartserver.wsgi;
          }
  5. Restart Nginx and enjoy: Now you can access a branch stored at /home/bzr/mybranch using the bzr+http://host/bzr/mybranch URL.

Final note: you must have include wsgi_vars somewhere in your Nginx configuration file, in a server section. That’s all for today. And remember… Bazaar and Nginx are a joy to work with! 😉

Feed redirect

A while ago while watching logs in order to debug a little CGI-script I am coding for ezmlm I noticed some feed readers were trying to access the old URLs. I added a permanent redirect in my Nginx configuration file. As an interesting side effect, now Planet GPUL is catching my feeds again 😉

Just in case someone wonders how an unconditional redirect is written in nginx.conf, the snippet is as follows:

    location ~ ^/blog/(rss2_xml|rdf10_xml|rdf91_xml) {
      rewrite .* https://hario.wordpress.com/feed permanent;
    }