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

From MediaWiki.org
Jump to: navigation, search

Contents

[edit] 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:

 server {
   listen 80;
   server_name wiki.example.com;

   root /var/www/mediawiki;

   location / {
     index index.php5;
     error_page 404 = @mediawiki;
   }

   location @mediawiki {
     rewrite ^/([^?]*)(?:\?(.*))? /index.php5?title=$1&$2 last;
   }

   location ~ \.php5?$ {
     include /etc/nginx/fastcgi_params;
     fastcgi_pass  127.0.0.1:8888;
     fastcgi_index index.php5;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
   }
 }

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

$ sudo ln -s ../sites-available/mediawiki /etc/nginx/sites-enabled/

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

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

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

$ sudo service nginx reload

[edit] 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;

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):

#!/bin/bash
BIND=/tmp/php.socket
USER=www-data
GROUP=www-data
PHP_FCGI_CHILDREN=15
PHP_FCGI_MAX_REQUESTS=1000
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:

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;
        }
   #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/([^?]*)(?:\?(.*))?" /wikiscriptpath/index.php?title=$1&$args last;
       }        
   location ~ \.php5?$ {
     include /etc/nginx/fastcgi_params;
     fastcgi_pass unix:/tmp/php.socket;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       }
   location ~ \.php?$ {
     include /etc/nginx/fastcgi_params;
     fastcgi_pass unix:/tmp/php.socket;
     fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  }
}

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 .

Launch nginx and start your php-fastcgi daemon (sudo /path/to/./php-fastcgi start)

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