Manual:Short URL/Nginx

From MediaWiki.org
Jump to navigation Jump to search

Other languages:
English • ‎日本語 • ‎한국어

This guide expects the wiki files to be installed in folder /w/. The articles will be output under urls starting with /wiki/.

nginx.conf[edit]

Rewriting in nginx is very simple. Just add highlighted settings to server scope and reload nginx, then it works.
If your server scope is not in nginx.conf, find it from conf.d/ or sites-available/ subdirs.

If you would like a different root or article path, you may use the short URL service by Redwerks to automatically generate a configuration.

 1 server {
 2 	# [...]
 3 
 4 	# Location for wiki's entry points
 5 	location ~ ^/w/(index|load|api|thumb|opensearch_desc)\.php$ {
 6 		include /etc/nginx/fastcgi_params;
 7 		fastcgi_param SCRIPT_FILENAME $document_root/w/$fastcgi_script_name;
 8 		fastcgi_pass 127.0.0.1:9000; # or whatever port your PHP-FPM listens on
 9 	}
10 	
11 	# Images
12 	location /w/images {
13 		# Separate location for images/ so .php execution won't apply
14 	}
15 	location /w/images/deleted {
16 		# Deny access to deleted images folder
17 		deny all;
18 	}
19 	# MediaWiki assets (usually images)
20 	location ~ ^/w/resources/(assets|lib|src) {
21 		try_files $uri 404;
22 		add_header Cache-Control "public";
23 		expires 7d;
24 	}
25 	# Assets, scripts and styles from skins and extensions
26 	location ~ ^/w/(skins|extensions)/.+\.(css|js|gif|jpg|jpeg|png|svg)$ {
27 		try_files $uri 404;
28 		add_header Cache-Control "public";
29 		expires 7d;
30 	}
31 	# Favicon
32 	location = /favicon.ico {
33 		alias /w/images/6/64/Favicon.ico;
34 		add_header Cache-Control "public";
35 		expires 7d;
36 	}
37 	
38 	# Uncomment the following code if you wish to use the installer/updater
39 	# installer/updater
40 	#location /w/mw-config/ {
41 	#	# Do this inside of a location so it can be negated
42 	#	location ~ \.php$ {
43 	#		include /etc/nginx/fastcgi_params;
44 	#		fastcgi_param SCRIPT_FILENAME $document_root/w/mw-config/$fastcgi_script_name;
45 	#		fastcgi_pass 127.0.0.1:9000; # or whatever port your PHP-FPM listens on
46 	#	}
47 	#}
48 	
49 	# Handling for the article path (pretty URLs)
50 	location /wiki/ {
51 		rewrite ^/wiki/(?<pagename>.*)$ /w/index.php;
52 		include /etc/nginx/fastcgi_params;
53 		# article path should always be passed to index.php
54 		fastcgi_param SCRIPT_FILENAME	$document_root/w/index.php;
55 		fastcgi_param PATH_INFO $pagename;
56 		fastcgi_param QUERY_STRING $query_string;
57 		fastcgi_pass  127.0.0.1:9000; # or whatever port your PHP-FPM listens on
58 	}
59 
60 	# Allow robots.txt in case you have one
61 	location = /robots.txt {
62 	}
63 	# Explicit access to the root website, redirect to main page (adapt as needed)
64 	location = / {
65 		return 301 https://www.example.com/wiki/Main_page;
66 	}
67 
68 	# Every other entry point will be disallowed.
69 	# Add specific rules for other entry points/images as needed above this
70 	location / {
71 		return 404;
72 	}
73 }

LocalSettings.php[edit]

$wgScriptPath = "/w";
$wgScriptExtension = ".php";
$wgArticlePath = "/wiki/$1";
$wgUsePathInfo = true;

See also[edit]