본문 바로가기
미디어위키

Manual Short URL Nginx

by 다움위키 2024. 1. 1.

이 안내서는 위키 파일이 폴더 /w/에 설치된 것으로 예상합니다. 기사는 /wiki/로 시작하는 url 아래에서 출력될 것입니다.

nginx.conf

엔진엑스에서 rewriting은 매우 간단합니다. 아래의 강조 표시된 설정을 server 범위에 추가하고 엔진엑스를 재시작하면, 작동합니다.

만약 서버 범위가 nginx.conf에 없으면, conf.d/ 또는 sites-available/ 하위 디렉토리에서 찾으십시오.

만약 다른 루트 또는 기사 경로를 원하면, Redwerks에서 제공하는 짧은 URL 서비스를 사용하여 자동으로 구성을 생성할 수 있습니다.

server {
	# [...]

	# Location for wiki's entry points
	location ~ ^/w/(index|load|api|thumb|opensearch_desc)\.php$ {
		include /etc/nginx/fastcgi_params;
		fastcgi_param SCRIPT_FILENAME $document_root/w/$fastcgi_script_name;
		fastcgi_pass 127.0.0.1:9000; # or whatever port your PHP-FPM listens on
	}
	
	# Images
	location /w/images {
		# Separate location for images/ so .php execution won't apply
	}
	location /w/images/deleted {
		# Deny access to deleted images folder
		deny all;
	}
	# MediaWiki assets (usually images)
	location ~ ^/w/resources/(assets|lib|src) {
		try_files $uri 404;
		add_header Cache-Control "public";
		expires 7d;
	}
	# Assets, scripts and styles from skins and extensions
	location ~ ^/w/(skins|extensions)/.+\.(css|js|gif|jpg|jpeg|png|svg)$ {
		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;
	}
	
	## 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/w/mw-config/$fastcgi_script_name;
	#		fastcgi_pass 127.0.0.1:9000; # or whatever port your PHP-FPM listens on
	#	}
	#}
	
	# Handling for the article path (pretty URLs)
	location /wiki/ {
		rewrite ^/wiki/(?<pagename>.*)$ /w/index.php;
		include /etc/nginx/fastcgi_params;
		# article path should always be passed to index.php
		fastcgi_param SCRIPT_FILENAME $document_root/w/index.php;
		fastcgi_param PATH_INFO $pagename;
		fastcgi_param QUERY_STRING $query_string;
		fastcgi_pass 127.0.0.1:9000; # or whatever port your PHP-FPM listens on
	}

	# 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 https://www.example.com/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";
$wgScriptExtension = ".php";
$wgArticlePath = "/wiki/$1";
$wgUsePathInfo = true;