worker_processes 1;
error_log ${buildout:directory}/var/log/nginx-error.log debug;

events {
#    worker_connections  1024;
}

http {
    include ${buildout:directory}/etc/mime.types;
#    gzip on;
    server {
        listen ${port};
        server_name localhost;
        root ${root};
        autoindex on;

        # Decide if we need to filter
        if ($args ~ "^(.*);filter_xpath=(.*)$") {
            set $newargs $1;
            set $filter_xpath $2;
            # rewrite args to avoid looping
            rewrite    ^(.*)$    /_include$1?$newargs?;
        }
        
        location @include500 { return 500; }
        location @include404 { return 404; }
        
        location ^~ /_include {
            # Restrict to subrequests
            internal;
            error_page 404 = @include404;
            
            # Cache in Varnish for 1h
            expires 1h;
            
            # Proxy
            rewrite    ^/_include(.*)$    $1    break;
            proxy_pass http://127.0.0.1:${port};
            
            # Our safety belt.
            proxy_set_header X-Loop 1$http_X_Loop; # unary count
            proxy_set_header Accept-Encoding "";
            error_page 500 = @include500;
            if ($http_X_Loop ~ "11111") {
                return 500;
            }
            
            # Filter by xpath
            xslt_stylesheet ${filter.xsl}
                xpath=$filter_xpath
                ;
            xslt_html_parser on;
            xslt_types text/html;
        }
        
        # Our styled location.
        location /styled {
            alias ${root};
            xslt_stylesheet ${example.xsl};
            xslt_html_parser on;
            xslt_types text/html;
            # Switch on ssi here to enable external includes.
            ssi on;
        }

        # Another styled location.
        location /styled/ploneorg {
            alias ${root}/ploneorg;
            xslt_stylesheet ${ploneorg.xsl};
            xslt_html_parser on;
            xslt_types text/html;
        }
    }
}
