본문 바로가기
미디어위키

Manual Short URL Nginx Debian2

by 다움위키 2024. 1. 1.

영문 위키피디아에서 사용하는 것과 같은 경로를 이용하기 위해 설정을 변경합니다. 왜냐하면, 다른 서비스 설정을 좀 더 쉽게 이해/설정하기 위함입니다.

어느 부분이 문제인지 모르겠지만, Manual:Short URL/Nginx의 설정이 동작하지 않습니다. 다음과 같은 환경에서 동작하도록 일부 수정을 했습니다.

  • 미디어위키 설치 디렉토리: /var/www/html/w/
  • 웹 접근 경로: wiki.example.com/w/ (설정으로 wiki.example.com 으로 변경합니다.)
  • 기사 접근 경로: /wiki/Page_Title

미디어위키를 /var/www/html/w 아래에 설치합니다. 위의 nginx 설정에서 php를 로딩하는 부분만 남기고 전부 지웁니다.

server {
...
	root /var/www/html;
	index index.html index.htm index.nginx-debian.html index.php;
	location ~ \.php$ {
		include fastcgi.conf;
		fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
	}
...    
}

그리고 LocalSettings.php도 수정합니다.

$wgScriptPath = "/w";       
$wgScriptExtension = ".php";

이 설정 아래에서 미디어위키가 접근되는지 확인을 합니다.

정상적으로 동작을 하면, https://shorturls.redwerks.org/ 를 가서 다음과 같이 입력하고, 아래쪽의 Submit을 누릅니다.

server {
	# [...]

	# Location for the wiki's root
	location /w/ {
		# Do this inside of a location so it can be negated
		location ~ \.php$ {
			try_files $uri $uri/ =404; # 이 설정 snippets/fastcgi-php.conf에 이미 정의되어 있습니다.
			include snippets/fastcgi-php.conf;
			fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
		}
	}
	
	location /w/images {
		# Separate location for images/ so .php execution won't apply
		
		location ~ ^/w/images/thumb/(archive/)?[0-9a-f]/[0-9a-f][0-9a-f]/([^/]+)/([0-9]+)px-.*$ {
			# Thumbnail handler for MediaWiki
			# This location only matches on a thumbnail's url
			# If the file does not exist we use @thumb to run the thumb.php script
			try_files $uri $uri/ @thumb;
		}
	}
	location /w/images/deleted {
		# Deny access to deleted images folder
		deny	all;
	}
	
	# Deny access to folders MediaWiki has a .htaccess deny in
	location /w/cache       { deny all; }
	location /w/languages   { deny all; }
	location /w/maintenance { deny all; }
	location /w/serialized  { deny all; }
	
	# Just in case, hide .svn and .git too
	location ~ /.(svn|git)(/|$) { deny all; }
	
	# Hide any .htaccess files
	location ~ /.ht { deny all; }
	
	# Uncomment the following code if you wish to hide the installer/updater
	## Deny access to the installer
	location /w/mw-config { deny all; }
	
	# Handling for the article path
	location /wiki {
		include snippets/fastcgi-php.conf;
		# article path should always be passed to index.php
		fastcgi_param SCRIPT_FILENAME	$document_root/w/index.php;
		fastcgi_pass  unix:/var/run/php/php7.2-fpm.sock;
	}
	
	# Thumbnail 404 handler, only called by try_files when a thumbnail does not exist
	location @thumb {
		# Do a rewrite here so that thumb.php gets the correct arguments
		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;
		
		# Run the thumb.php script
		include snippets/fastcgi-php.conf;
		fastcgi_param SCRIPT_FILENAME	$document_root/w/thumb.php;
		fastcgi_pass  unix:/var/run/php/php7.2-fpm.sock;
	}
	
	# [...]
}

그런 다음 미디어위키 설정 LocalSettings.php를 다음과 같이 수정합니다.

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

설정이 완료되면, nginx 서버 설정을 테스트하고, 이상 없으면, 서비스를 재시작합니다. Varnish 또는 Squid를 운영하면 같이 재시작합니다.

  • sudo nginx -t
  • sudo systemctl restart nginx varnish

잘 작동하는 것처럼 보이지만, 실제 어떤 기사로 접근하면 템플릿을 제대로 가져오지 못하는 문제가 생깁니다. 직전의 nginx 설정에서 다음을 변경합니다.

  • 수정전: include /etc/nginx/fastcgi_params;
  • 수정후: include snippets/fastcgi-php.conf;

Nginx 서비스 테스트를 하면, 설정에 오류가 있다고 나올 것입니다. /etc/nginx/snippets/fastcgi-php.conf에서 해당 줄을 다음과 같이 주석처리 합니다.

#try_files $fastcgi_script_name =404;

만약 /var/www/html에 접근할 때, 직접 미디어위키로 보내고 싶다면, 다음 설정을 추가하는 것이 좋습니다.

	# Explicit access to the root website, redirect to main page (adapt as needed)
	location = / {
		return 301 http://dawoum.duckdns.org/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;
	}
	# Allow robots.txt in case you have one
	location = /robots.txt {
	}

추가 작업

이렇게 설정을 바꾸면, $wgScriptPath가 변경되었기 때문에 이전 서비스들이 제대로 동작하지 않을 수 있습니다. 다음 서비스 설정을 수정해야 합니다.