Use IkiWiki in nginx with httpauth and 404 plugin

Though it's trivial to use IkiWiki with nginx you need some tricks to make it work together with the httpauth module and the 404 module.

To make it short, here's a working example (as in "works for me"). The blue lines are required for httpauth while the green lines are required for 404.

location /wiki/ {
    error_page 404 @ikiwiki404;
}

# External reachable ikiwiki.cgi
location /wiki/ikiwiki.cgi {
    # Do basic authentication. Here I pass this task to PAM
    auth_pam              "Admin authentication";
    auth_pam_service_name "nginx";
    fastcgi_param REMOTE_USER $remote_user;

    fastcgi_pass  unix:/var/run/fcgiwrap.socket;
    fastcgi_index ikiwiki.cgi;
    fastcgi_param SCRIPT_FILENAME   /srv/www/htdocs/wiki/ikiwiki.cgi;
    fastcgi_param DOCUMENT_ROOT      /srv/www/htdocs/wiki;

    include /etc/nginx/fastcgi_params;
}

# Another definition for ikiwiki.cgi, only reachable by error_page 404.
location @ikiwiki404 {
    fastcgi_pass  unix:/var/run/fcgiwrap.socket;
    fastcgi_index ikiwiki.cgi;
    fastcgi_param SCRIPT_FILENAME   /srv/www/htdocs/wiki/ikiwiki.cgi;
    fastcgi_param DOCUMENT_ROOT      /srv/www/htdocs/wiki;

    # For 404 plugin
    fastcgi_param QUERY_STRING "";
    fastcgi_param REQUEST_METHOD "get";
    fastcgi_param REDIRECT_STATUS 404;
    fastcgi_param REDIRECT_URL $uri;

    include /etc/nginx/fastcgi_params;
}