Setting up Diazo with Nginx (by hand compile)

Diazo is a proxy server able to re-theme any existing website, local or distant. You may find more info on diazo.org. As far as I understand it, outside of Plone, the deployment is always a challenge. The idea is that you take a theme that is an XSL file (the XLS file is generated by Diazo) and HTML output of a web application to have a new web application. Diazo Concept Nginx is a light-weight webserver that can serve this proxy content. In order to do so, Nginx needs a specific module that is not yet incoprporated in the server, a module that allows for sloppy HTML as opposed to XHTML. True XML parsers will not work on HTML content that is not at least XML valid.

I have been using Webfaction as my hosting provider for quite some time. So the instructions here are in my context of that server, but the Nginx compile is generic.

Another avenue I did not explore was using Apache and WSGI. That might have been easier.

Well, anyway, here is what I needed to do in order to get Nginx working for Webfaction. I also included a module to redirect on browser language detection.

Compile Nginx for Diazo

The problem here is that Diazo needs xslt-html-parser to work, but xslt-html-parser is a non-standard part of the http_xslt_module for Nginx. Therefor, we need to patch the Nginx source code (http_xslt_module is in the core source code) with the xslt-html-parser addition. Sometime, I will get around to advocating the inclusion of the patch in the core code. Well, here is what I did…

Download Nginx source code from here: http://nginx.org/en/download.html I used the following source code here:http://nginx.org/download/nginx-1.6.1.zip I unzipped the source code and opened a console to that source-code directory.

Then I applied the following patch for Diazo to work https://github.com/jcu-eresearch/nginx-custom-build/blob/master/nginx-xslt-html-parser.patch With this command patch src/http/modules/ngx_http_xslt_filter_module.c nginx-xslt-html-parser.patch [I have been informed that the latest patch is available here: https://bitbucket.org/lrowe/nginx-xslt-html-parser.]

Then I installed the Nginx accept language that is hosted here: https://github.com/giom/nginx_accept_language_module from the following download link:https://github.com/giom/nginx_accept_language_module/archive/master.zip that I unzipped into a different directory, /home/username/hacks/nginx_accept_language_module-master

I used the following line to compile Nginx 1.6 with the patch on Webfaction

./configure –with-http_xslt_module –with-http_stub_status_module –add-module=/home/[username[/hacks/nginx_accept_language_module-master –with-ipv6 –with-http_ssl_module –with-cc-opt=-Wno-error –prefix=/home/[username]/opt/nginxslt

After the configuration, and creating the installation directory, I did a make and then a make install. The Nginx executable is now in /home/username/nginxslt/sbin/nginx.

Setting Up a Diazo Development Environment on my Windows Machine

Directions for setting up a Diazo development environment are available here:http://docs.diazo.org/en/latest/installation.html

I choose the easy_install methode. First, I installed Python 2 for Windows from here: https://www.python.org/downloads/  or more specificly in my case https://www.python.org/ftp/python/2.7.8/python-2.7.8.msi specifying that I wanted to use the d:\opt\python2 as my installation directory. The installation did not change my environmental variables, and I had to add d:\opt\python2\bin;d:\opt\python2\Scripts to the %PATH% variable manually.

After installation and variable modification, I think I executed easy_install -U diazo without a hitch. For some reason, I was unable to install Diazo on Webfaction.

Compiling of a template Diazo is as follows: diazocompiller -o theme.xsl -r rules.xml where the theme.html files are relative to the compiling directory of rules.xml.

Configuration of Diazo on Webfaction

I am going to be lazy and just show parts of the nginx.conf file from /home/username/nginxslt/conf/nginx.conf.

Global variables

# in http { context
# Augmented head size allocations
proxy_headers_hash_bucket_size 128;
proxy_headers_hash_max_size 1024;


# Set up a map for available languages for the websites
map $http_accept_language $lang {
    default en;
    ~fr fr;
}

The main server was just putting a theme around XHTML content in a folder.

# in http { context
server {
    listen       30742;
    server_name  example.com www.example.com;
    server_name_in_redirect on; # Not sure if this part is necessary
    location / {
        xslt_stylesheet /home/username/webapps/diazoman/theme.xsl
            path='$uri'
            ;
        xslt_html_parser on;
        xslt_types text/html;
        # rewrite / $1/$lang;
        rewrite ^/?$ $scheme://$host/$lang/;
        #rewrite ^/raw(.*)$ $1;
        root   /home/username/webapps/diazoman/html;
        index  index.html index.htm;
    }
    location /blog {
        xslt_stylesheet /home/username/webapps/diazoman/theme.xsl
            path='$uri'
            ;
        xslt_html_parser on;
        xslt_types text/html;
        proxy_headers_hash_max_size 51200;
        proxy_pass http://wordpress.example.com/;
        proxy_set_header Host wordpress.example.com;
        #rewrite ^/blog/(.*)+$ /blog/index.php?$1;
        rewrite ^/blog/(.*)$ /$1 break;
        #proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Diazo "true";
        proxy_set_header Accept-Encoding "";
    }

I had some fun theming a friend’s server in Chinese.

# In http context
server {
    listen 30742;
    server_name joomla.example.com;
    root /home/username/webapps/diazoman/html;

    location / {
    # rewrite /example/example /example;
    xslt_stylesheet /home/username/webapps/diazoman/theme.xsl
        path='$uri'
        ;
    xslt_html_parser on;
    xslt_types text/html;
    proxy_pass http://landdu.com/;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Diazo "true";
    proxy_set_header Accept-Encoding "";
    }

    location /css/ {}
    location /favicon.ico {}
    location /images/images/ {}
}

Starting / Restarting / Stopping of Diazo on Webfaction

To start: /home/username/opt/nginxslt/sbin/nginx

To stop : kill cat /home/username/opt/nginxslt/logs/nginx.pid

Automatic Start and Stop Scripts on Webfaction

Created executable start script in /home/username/opt/nginxslt/bin/start

Created executable stop script in /home/username/opt/nginxslt/bin/stop

Edited crontab with

$ crontab -e

and added the following line

/5 * * * $HOME/opt/nginxslt/bin/start