MediaWiki Short URLs with Nginx

One of the sites I develop/administer includes a MediaWiki instance. Most of the MediaWiki docs and setup examples assume you're running Apache, but Nginx is my HTTP daemon of choice. I spent a lot of time persuading MediaWiki and Nginx to work together, especially setting up "pretty URLs", but since then it has just worked.

Sometime in the past couple months (the wiki is not a highly-trafficked part of the site) my "pretty URLs" setup got borked. It's working again, but only after a lot more brow-furrowing and beard-stroking (and some screaming and head-banging). I offer this narrative in hopes that you, dear reader, will be spared some of the trouble I experienced today.

<!-- SUMMARY_END -->

My clients wanted URLs in this format:

http://www.mydomain.com/wiki/MyPage

Not either of these formats, especially the second:

http://www.mydomain.com/wiki/index.php/MyPage
http://www.mydomain.com/wiki/index.php?title=MyPage

This format was also unacceptable, which is too bad since it's the sample config from the Nginx Wiki:

http://wiki.mydomain.com/MyPage

As a side note, that last combination seems to be officially "not recommended" on the MediaWiki Short URL page.

After some testing I saw that my Nginx config was not the problem -- I could point URLs in the desired config and they worked. Here's the Nginx config -- it's basically the same as the one in the Nginx wiki with a few path changes. Note that I've only included the parts that are relevant to MediaWiki:

server {
    ...
    root  /var/www/html/www.mydomain.com;
    index  index.php index.html index.htm;
    ...
    location  /wiki/skins/  {
        alias  /var/www/html/mediawiki/1.13.1/skins/;
    }
    location  /wiki {
        error_page  404 = @wiki;
        index  index.php5;
    }
    location  @wiki {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index  index.php5;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/mediawiki/1.13.1/index.php5;
        fastcgi_param  QUERY_STRING  title=$fastcgi_script_name;
        include  /etc/nginx/fastcgi_params;
    }
    location  ~ .php$ {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /var/www/html/mediawiki/1.13.1$fastcgi_script_name;
        fastcgi_param  QUERY_STRING     $query_string;
        fastcgi_param  REQUEST_METHOD   $request_method;
        fastcgi_param  CONTENT_TYPE     $content_type;
        fastcgi_param  CONTENT_LENGTH   $content_length;
    }
    location  / {
        ...
    }
}

The problem was with the particular combination of MediaWiki settings I was using. I ended up replacing these settings:

$wgScriptPath = "/wiki";
$wgScript = "$wgScriptPath/$1";
$wgUsePathInfo = true;
$wgRedirectScript = "$wgScriptPath/redirect.php";

...with these:

$wgScriptPath = "";
$wgArticlePath = "/wiki/$1";
$wgUsePathInfo = true;

Your mileage may vary. :)