NGINX 워드프레스 플러그인 WP Super Cache rewrite rule 설정 방법

NGINX

워드프레스 캐시 플러그인중 하나인 WP Super Cache는 적당한 성능과 설정의 간편함 때문에 많은 사용자층을 가지고 있습니다.

웹서버로 많이 사용하는 아파치 같은 경우 .htaccess 파일에 rerite rule이 작성되고 이를 기반으로 고유주소를 맵핑하게 되는데 WP Super Cache의 동작 구조상 한번 읽어들인 페이지를 .html 파일로 저장했다가 다른 방문자가 요청시에는 PHP 연산 없이 .html 파일로 응답함으로써 속도를 올리는 구조이다 보니 WP Super Cache를 설치할 때에도 rewrite rule을 추가해야 합니다. 보통 권한 설정이 잘 되어 있다면 WP Super Cache 설치시 자동으로 추가되게 되는데요.

하지만 NGINX를 웹서버로 사용하는 경우에는 .htaccess를 사용하지 않기 때문에 무용지물이 되어 직접 NGINX의 server 블럭을 설정해야 합니다.

1

NGINX WP Super Cache rewrite rule 설정

# WP Super Cache rules.
# Designed to be included from a 'wordpress-ms-...' configuration file.

set $cache_uri $request_uri;

# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
        set $cache_uri 'null cache';
}
   
if ($query_string != "") {
        set $cache_uri 'null cache';
}   

# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
        set $cache_uri 'null cache';
}   

# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
        set $cache_uri 'null cache';
}

# START MOBILE
# Mobile browsers section to server them non-cached version. COMMENTED by default as most modern wordpress themes including twenty-eleven are responsive. Uncomment config lines in this section if you want to use a plugin like WP-Touch
# if ($http_x_wap_profile) {
#        set $cache_uri 'null cache';
#}

#if ($http_profile) {
#        set $cache_uri 'null cache';
#}

#if ($http_user_agent ~* (2.0\ MMP|240x320|400X240|AvantGo|BlackBerry|Blazer|Cellphone|Danger|DoCoMo|Elaine/3.0|EudoraWeb|Googlebot-Mobile|hiptop|IEMobile|KYOCERA/WX310K|LG/U990|MIDP-2.|MMEF20|MOT-V|NetFront|Newt|Nintendo\ Wii|Nitro|Nokia|Opera\ Mini|Palm|PlayStation\ Portable|portalmmm|Proxinet|ProxiNet|SHARP-TQ-GX10|SHG-i900|Small|SonyEricsson|Symbian\ OS|SymbianOS|TS21i-10|UP.Browser|UP.Link|webOS|Windows\ CE|WinWAP|YahooSeeker/M1A1-R2D2|iPhone|iPod|Android|BlackBerry9530|LG-TU915\ Obigo|LGE\ VX|webOS|Nokia5800)) {
 #       set $cache_uri 'null cache';
#}

#if ($http_user_agent ~* (w3c\ |w3c-|acs-|alav|alca|amoi|audi|avan|benq|bird|blac|blaz|brew|cell|cldc|cmd-|dang|doco|eric|hipt|htc_|inno|ipaq|ipod|jigs|kddi|keji|leno|lg-c|lg-d|lg-g|lge-|lg/u|maui|maxo|midp|mits|mmef|mobi|mot-|moto|mwbp|nec-|newt|noki|palm|pana|pant|phil|play|port|prox|qwap|sage|sams|sany|sch-|sec-|send|seri|sgh-|shar|sie-|siem|smal|smar|sony|sph-|symb|t-mo|teli|tim-|tosh|tsm-|upg1|upsi|vk-v|voda|wap-|wapa|wapi|wapp|wapr|webc|winw|winw|xda\ |xda-)) {
  #      set $cache_uri 'null cache';
#}
#END MOBILE

# Use cached or actual file if they exists, otherwise pass request to WordPress
location / {
        try_files /wp-content/cache/supercache/$http_host/$cache_uri/index.html $uri $uri/ /index.php?$args ;
}

위 코드를 워드프레스가 설치된 도메인의 가상 호스트 설정 부분에 복사해 넣은 다음 WP Super Cache 플러그인을 사용하면 끝입니다. 워드프레스에서 사용하는 테마 대부분이 반응형을 사용하기 때문에 별도의 모바일 테마 플러그인이 필요 없는데 만일 모바일 테마 플러그인을 별도로 사용하는 경우에는 위 코드의 25-42라인 코드의 주석처리(#)를 삭제합니다.

만일 워드프레스 홈페이지가 SSL을 사용한다면 위의 location 블럭 항목을 아래의 코드로 대체합니다.

location / {
        try_files /wp-content/cache/supercache/$http_host/$cache_uri/index-https.html $uri $uri/ /index.php?$args ;
}

2 댓글. Leave new

  • 안녕하세요. 혹시 위에 문구를 어디 파일에 넣어야 하는지 조금만 더 자세히 알 수 있을까요?
    혹시 vim /etc/nginx/app.d/server.webstation-vhost.conf 이 맞는건가요?

    응답
    • 설정파일을 보니 시놀로지의 NGINX 같네요.

      시놀로지에서 NGINX를 사용하지 않아 기본 설정이 어떠한 구조로 되어있는지는 모르겠지만 기본적으로 server { } 블럭에 넣으시면 됩니다.

      만일 오류가 난다면 해당 파일의 설정 구문을 올려주시면 답변에 도움이 됩니다.

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

Fill out this field
Fill out this field
유효한 이메일 주소를 입력해주세요.

nginx title 03
NGINX WebDAV 설정 방법
WebDAV(Web Distributed Authoring and Versioning)는 http를 이용해 웹서버에 저장되어 있는 파일을 여러 사용자가 편집하기 위해 고안된 프로토콜입니다. 실제로는 FTP 대용으로…
windows nginx php setting title
윈도우 NGINX PHP 연동 및 서비스 등록 방법
이전 포스트에서 윈도우에 NGINX를 설치하고 서비스에 등록하는 방법까지 알아봤는데요. NGINX를 개발 환경 또는 웹서버 환경으로 만들기 위해서 PHP를 설치해야 하는…