MediaWiki nginx configuration file

Scenario

  • You have a nginx webserver
  • You have several MediaWiki installation on this server
  • You would like to have a simple and clear configuration

Solution

You want a configuration file you can include in every server {} block MediaWiki is available

Implementation

  1. Create a includes subdirectory in your nginx configuration directory (by default, /usr/local/etc/nginx or /etc/nginx).
    This directory can welcome every configuration block you don’t want to repeat in each server block.
  2. You put in this directory mediawiki-root.conf, mediawiki-wiki.conf or your own configuration block.
  3. In each server block, you can now add the following line:
    Include includes/mediawiki-root.conf;

Configuration I – MediaWiki in the root web directory, /article path

This is mediawiki-root.conf on my server:

        # Common settings for a wiki powered by MediaWiki with the following configuration:
        #   (1) MediaWiki is installed in $root folder
        #   (2) Article path is /<title>
        #   (3) LocalSettings.php contains $wgArticlePath = "/$1"; $wgUsePathInfo = true;

        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ ^/images/thumb/(archive/)?[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ {
            #Note: this doesn't work with InstantCommons.
            if (!-f $request_filename) {
                rewrite ^/images/thumb/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ /thumb.php?f=$1&width=$2;
                rewrite ^/images/thumb/archive/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ /thumb.php?f=$1&width=$2&archived=1;
            }
        }

        location /images/deleted    { deny all; }
        location /cache             { deny all; }
        location /languages         { deny all; }
        location /maintenance       { deny all; }
        location /serialized        { deny all; }
        location ~ /.(svn|git)(/|$) { deny all; }
        location ~ /.ht             { deny all; }
        location /mw-config         { deny all; }

Configuration II – MediaWiki in the /w directory, /wiki/article path

This is mediawiki-wiki.conf on my server:

        # Common settings for a wiki powered by MediaWiki with the following configuration:
        #   (1) MediaWiki is installed in $root/w folder
        #   (2) Article path is /wiki/<title>
        #   (3) LocalSettings.php contains $wgArticlePath = "/wiki/$1"; $wgUsePathInfo = true;

        location /wiki {
            try_files $uri $uri/ /w/index.php?$query_string;
        }

        location ~ ^/w/images/thumb/(archive/)?[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ {
            #Note: this doesn't work with InstantCommons.
            if (!-f $request_filename) {
                rewrite ^/w/images/thumb/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ /w/thumb.php?f=$1&width=$2;
                rewrite ^/w/images/thumb/archive/[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ /w/thumb.php?f=$1&width=$2&archived=1;
            }
        }

        location /w/images/deleted  { deny all; }
        location /w/cache           { deny all; }
        location /w/languages       { deny all; }
        location /w/maintenance     { deny all; }
        location /w/serialized      { deny all; }
        location ~ /.(svn|git)(/|$) { deny all; }
        location ~ /.ht             { deny all; }
        location /w/mw-config       { deny all; }

Example of use

www.wolfplex.org serves other application is subdirectories and MediaWiki for /wiki URLs.

This server block:

  1. is a regular one
  2. includes our includes/mediawiki-wiki.conf configuration file (scenario II)
  3. contains a regular php-fpm block
  4. contains other instructions
    server {
        listen          80;
        server_name     www.wolfplex.org

        access_log      /var/log/www/wolfplex.org/www-access.log main;
        error_log       /var/log/www/wolfplex.org/www-error.log;
        root            /var/wwwroot/wolfplex.org/www;
        index           index.html index.php index.htm;

        [...]

        include         includes/mediawiki-wiki.conf;

        location / {
            #Link to the most relevant page to present the project
            rewrite /presentation/?$ /w/index.php?title=Presentation last;

            #Link to the most relevant page for bulletin/news information:
            rewrite /b/?$ /w/index.php?title=Bulletin:Main last;

            [...]
        }

        [...]

        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass   127.0.0.1:9010;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

Some notes

  • Configuration is based on Daniel Friesen’s , who collected various working nginx ones. There are some differences in the rewrite, our goal here is to have a generic configuration totally agnostic of the way .php files are handled.
  • Our configuration (not the one generated by the builder) uses a if for the thumbnails handler. The nginx culture is a culture where you should try something else than an if. See this nginx wiki page and this post about the location if way of work for more information.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.