Manual:Short URL/wiki/Page title -- nginx rewrite--root access

Single wiki server
Modified from the configuration given on the official Nginx wiki.

Environment scenario:
 * MediaWiki has been installed in /var/www/mediawiki directory
 * wiki is to be served from wiki.example.com
 * you want urls formated as /wiki/Page_Title

There are a few things you need to do:

Add the following text to either nginx.conf or in a new file /etc/nginx/sites-available/mediawiki:

If you are using the sites-available approach above, symlink the configuration into sites-enabled

Add the following lines to the end of your LocalSettings.php file:

After reloading the Nginx config, MediaWiki should work with short urls.


 * RHEL 5.4 + Nginx + Mediawiki With HTTPS

Fungiblename's configuration for more than one wiki on a single server/domain/subdomain
If you have more than one wiki on a single server/domain/subdomain, the above configuration will not work properly.

Here is what I did:

1) Create a symlink in the nginx webroot to the path to your MW installation, e.g., cd /var/www/nginx-default ln -s /var/www/some/long/path wikiscriptpath NOTE: Like with Apache, The "wikiscripthpath" MUST be different from your $wgArticlePath. It seems that aliases would not work (at least not for me).

2) In LocalSettings.php, set $wgScriptPath to the location of the symlink relative to the document root, e.g., $wgScriptPath = /wikiscriptpath;

3) Set $wgArticlePath to, e.g., $wgArticlePath = /wiki/$1; Note: It seems that this variable NOT PARSED by the php interpreter before it gets passed to nginx, so it seems that you cannot set your wgArticlePath with another variable. I usually do that with multiple wikis on the same server using Apache - that way, I can use the same LocalSettings.php for each site then just change a few variables at the top - seems that this won't work with ngnix (at least not with version 0.7.65). Also note that this path CANNOT EXIST in reality on your system. If it does exist, it will cause problems and the rewrite will not work correctly.

3.5) Also set $wgUsePathInfo = true;

So, in summary, your path variables should look something like this in LocalSettings.php for each of your wikis (adjusted as appropriate to each wiki):

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

and, optionally, if you want to protect files from viewing without being logged into the system, add these:

$wgUploadPath = "$wgScriptPath/img_auth.php"; #Sort of works; gives an odd error message if not logged in, but serves its purpose well $wgUploadDirectory = "images"; #Needed to keep from breaking nginx rewrites $wgImgAuthPublicTest = false; #Honestly, I'm not exactly sure what this does, but I've had it in my configuration for a while and it doesn't seem to hurt anything

4) Set up a php-fastcgi instance to bind to /tmp/php.socket - it's more secure for single-machine setups and will let you share cgi instances across virtual servers and domains more easily. Here's my php-fastcgi daemon script for Ubuntu 10.04 (copied/adapted from nginx wiki - now included for historical purposes or for people without php5-fpm): (NOTE: This has been replaced by php5-fpm in Ubuntu 12.04 and I was able to use that essentially without modification except to set user, group, and listen parameters in /etc/php5/fpm/php-fpm.conf and /etc/php5/fpm/pool.d/www.conf)

BIND=/tmp/php.socket USER=www-data GROUP=www-data PHP_FCGI_CHILDREN=15 PHP_FCGI_MAX_REQUESTS=1000
 * 1) !/bin/bash

PHP_CGI=/usr/bin/php-cgi PHP_CGI_NAME=`basename $PHP_CGI` PHP_CGI_ARGS="- USER=$USER PATH=/usr/bin PHP_FCGI_CHILDREN=$PHP_FCGI_CHILDREN PHP_FCGI_MAX_REQUESTS=$PHP_FCGI_MAX_REQUESTS $PHP_CGI -b $BIND" RETVAL=0 start { echo -n "Starting PHP FastCGI: " start-stop-daemon --quiet --start --background --chuid "$USER" --exec /usr/bin/env -- $PHP_CGI_ARGS RETVAL=$? echo "$PHP_CGI_NAME." } stop { echo -n "Stopping PHP FastCGI: " killall -q -w -u $USER $PHP_CGI RETVAL=$? echo "$PHP_CGI_NAME." } case "$1" in    start)       start   ;;     stop) stop ;;    restart)       stop       start   ;;     *) echo "Usage: php-fastcgi {start|stop|restart}" exit 1 ;; esac exit $RETVAL

5) Set up /etc/nginx/fastcgi_params:

fastcgi_param GATEWAY_INTERFACE  CGI/1.1; fastcgi_param SERVER_SOFTWARE    nginx; fastcgi_param QUERY_STRING       $query_string; fastcgi_param REQUEST_METHOD     $request_method; fastcgi_param CONTENT_TYPE       $content_type; fastcgi_param CONTENT_LENGTH     $content_length; fastcgi_param SCRIPT_FILENAME    $document_root$fastcgi_script_name; fastcgi_param SCRIPT_NAME        $fastcgi_script_name; fastcgi_param REQUEST_URI        $request_uri; fastcgi_param DOCUMENT_URI       $document_uri; fastcgi_param DOCUMENT_ROOT      $document_root; fastcgi_param SERVER_PROTOCOL    $server_protocol; fastcgi_param REMOTE_ADDR        $remote_addr; fastcgi_param REMOTE_PORT        $remote_port; fastcgi_param SERVER_ADDR        $server_addr; fastcgi_param SERVER_PORT        $server_port; fastcgi_param SERVER_NAME        $server_name;

6) Then, your server config file (e.g., /etc/nginx/sites-available/default) should look something like this (Note: Update as of 2013-11-20 to work with latest MediaWiki on Ubuntu 12.04). The link provided in the warning at the top of this page DID NOT WORK AT ALL FOR MOST PAGES, but it did provide valuable information about how to handle thumbnails (works for MW 1.21.x), which did not work under my previous configuration (which was tested in MW 1.19.x):

server { listen 80; # Set this to whatever you want to call your server server_name SERVERNAME; root /var/www/nginx-default; location / { index index.html index.php index.php5; #The following are optional for image protection via img_auth, but they only seem to work in the root location block: rewrite "^/wikiscriptpath/images(.*)?" /wikiscriptpath/img_auth.php/$1; rewrite "^/wikiscriptpath/img_auth.php/(.*)?" /wikiscriptpath/img_auth.php?path=$1; }   #create a location like this for each wiki on your server, remembering to set symlinks and appropriate LocalSettings.php values location /wiki { index index.php; rewrite "^(wiki)$" $1/ permanent; rewrite "^/wiki/([^?]*)(?:\?(.*))?" /wikiscriptpath/index.php?title=$1&$args last; }	   location ~ \.php5?$ { include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm/www.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }   location ~ \.php?$ { include /etc/nginx/fastcgi_params; fastcgi_pass unix:/var/run/php5-fpm/www.sock; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }   location /wikiscriptpath/images { location ~ ^/wikiscriptpath/images/thumb/(archive/)?[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ { # Thumbnail handler for MediaWiki # This location only matches on a thumbnail's url # If the file does not exist we use @thumb to run the thumb.php script # Do a rewrite here so that thumb.php gets the correct arguments rewrite ^/wikiscriptpath/images/thumb/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ /wikiscriptpath/thumb.php?f=$1&width=$2; rewrite ^/wikiscriptpath/images/thumb/archive/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ /wikiscriptpath/thumb.php?f=$1&width=$2&archived=1; }               # Run the thumb.php script include /etc/nginx/fastcgi_params; fastcgi_param SCRIPT_FILENAME	$document_root/wikiscriptpath/thumb.php; #The following should match whatever you set in your php5-fpm configuration fastcgi_pass unix:/var/run/php5-fpm/www.sock; } }
 * 1) For thumbnail handling for each wiki script path, each with its own location block:

7) Be sure to symlink that file to /etc/nginx/sites-enabled, e.g., cd /etc/nginx/sites-enabled ln -s /etc/nginx/sites-available/default.

(and when you edit, edit only in the sites-available directory, otherwise, any temp files will also get loaded by nginx, which can get messy)

Launch nginx and start your php-fastcgi daemon (sudo /path/to/./php-fastcgi start or on 12.04 - sudo service php5-fpm start)

8) Enjoy. Hope this helps someone else out there...