Jump to content

手册:缩短URL/Nginx

本頁使用了標題或全文手工轉換
From mediawiki.org
This page is a translated version of the page Manual:Short URL/Nginx and the translation is 100% complete.

本指南要求将wiki文件安装在/w/文件夹中。这些文章将在以/wiki/开头的URL下输出。

nginx.conf

将下面的高亮部分配置复制到对应的网站配置文件中,然后重载nginx。

如果 nginx.conf 中没有您的服务器范围,请在 conf.d/ 或 sites-available/ 文件夹中找找。

如果你想要根目录和文章目录不同,你可能需要使用the short URL service by Redwerks来生成配置文件。

server {
	# [...]

	# 用于维基入口的 location
	location ~ ^/w/(index|load|api|thumb|opensearch_desc|rest|img_auth)\.php$ {
		include /etc/nginx/fastcgi_params;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		fastcgi_pass 127.0.0.1:9000; # or whatever port your PHP-FPM listens on
	}

	# 图片
	location /w/images {
		# Separate location for images/ so .php execution won't apply
		# as required starting from v1.40
		add_header X-Content-Type-Options "nosniff";
	}
	location /w/images/deleted {
		# Deny access to deleted images folder
		deny all;
	}
	# MediaWiki 资源(通常是图片)
	location ~ ^/w/resources/(assets|lib|src) {
		try_files $uri =404;
		add_header Cache-Control "public";
		expires 7d;
	}
	# 皮肤跟扩展的资源、脚本及样式
	location ~ ^/w/(skins|extensions)/.+\.(css|js|gif|jpg|jpeg|png|svg|wasm|ttf|woff|woff2)$ {
		try_files $uri =404;
		add_header Cache-Control "public";
		expires 7d;
	}
	# 网站图标(Favicon)
	location = /favicon.ico {
		alias /w/images/6/64/Favicon.ico;
		add_header Cache-Control "public";
		expires 7d;
	}

	# 许可证和感谢文件
	location ~ ^/w/(COPYING|CREDITS)$ {
		default_type text/plain;
	}
	
	## Uncomment the following code if you wish to use the installer/updater
	## installer/updater
	#location /w/mw-config/ {
	#	# Do this inside of a location so it can be negated
	#	location ~ \.php$ {
	#		include /etc/nginx/fastcgi_params;
	#		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
	#		fastcgi_pass 127.0.0.1:9000; # or whatever port your PHP-FPM listens on
	#	}
	#}
	
	# Handling for Mediawiki REST API, see [[mw:API:REST_API]]
	location /w/rest.php/ {
		try_files $uri $uri/ /w/rest.php?$query_string;
	}

	## Uncomment the following code for handling image authentication
	## Also add "deny all;" in the location for /w/images above
	#location /w/img_auth.php/ {
	#	try_files $uri $uri/ /w/img_auth.php?$query_string;
	#}

	# Handling for the article path (pretty URLs)
	location /wiki/ {
		rewrite ^/wiki/(?<pagename>.*)$ /w/index.php;
	}

	# Allow robots.txt in case you have one
	location = /robots.txt {
	}
	# Explicit access to the root website, redirect to main page (adapt as needed)
	location = / {
		return 301 /wiki/Main_Page;
	}

	# Every other entry point will be disallowed.
	# Add specific rules for other entry points/images as needed above this
	location / {
		return 404;
	}
}

LocalSettings.php

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

参见