Skip to content

Instantly share code, notes, and snippets.

@hfm
Created July 27, 2012 16:16
Show Gist options
  • Save hfm/3188940 to your computer and use it in GitHub Desktop.
Save hfm/3188940 to your computer and use it in GitHub Desktop.
Wordpress and NGINX with Reverse Proxy
server {
listen ****; # 80番以外の競合しないポート
server_name blog.hifumi.info;
client_max_body_size 20M;
# Root
location / {
root /usr/local/www/wordpress;
index index.html index.htm index.php;
if (-f $request_filename) {
expires 30d;
break;
}
if (!-e $request_filename) { rewrite ^(.+)$ /index.php?q=$1 last; }
}
try_files $uri $uri/ /index.php?q=$uri&$args;
# Error Page
error_page 404 /404.html;
location = /404.html { root /usr/local/www/wordpress; }
error_page 500 502 503 504 /50x.html;
location = /50x.html { root /usr/local/www/wordpress; }
# PHP-FPM
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/www/wordpress$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include /usr/local/etc/nginx/fastcgi_params;
}
}
server {
listen 80;
server_name blog.hifumi.info;
client_max_body_size 20M;
access_log /var/log/nginx/wp_rproxy.access.log;
error_log /var/log/nginx/wp_rproxy.error.log;
# Proxy Cache for mobile
set $mobile "";
if ($http_user_agent ~* '(DoCoMo|J-PHONE|Vodafone|MOT-|UP\.Browser|DDIPOCKET|ASTEL|PDXGW|Palmscape|Xiino|sharp pda browser|Windows CE|L-mode|WILLCOM|SoftBank|Semulator|Vemulator|J-EMULATOR|emobile|mixi-mobile-converter|PSP)') { set $mobile "@ktai"; }
if ($http_user_agent ~* '(iPhone|iPod|Opera Mini|Android.*Mobile|NetFront|BlackBerry)') { set $mobile "@mobile"; }
if ($http_cookie ~* "wptouch(_switch_cookie=normal|-pro-view=desktop)") { set $mobile "@mobile.off"; }
# Proxy Cache
location /wp-admin { proxy_pass http://backend; }
location ~ .*\.php { proxy_pass http://backend; }
location ~ .*\.(txt|xml|html?|js|css|gz|ico|jpe?g|JPE?G|gif|GIF|png|PNG|wmv|WMV|flv|FLV|mpg|MPG|svg|SVG) {
root /usr/local/www/wordpress/;
index index.php;
if (-f $request_filename) {
expires 30d;
break;
}
}
location / {
if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_") { set $do_not_cache 1; }
proxy_no_cache $do_not_cache;
proxy_cache_bypass $do_not_cache;
proxy_cache czone;
proxy_cache_key "$scheme://$host$request_uri$is_args$args$mobile";
proxy_cache_valid 200 301 302 10m;
proxy_cache_valid 404 5m;
proxy_pass http://backend;
}
# Purge Proxy Cache
location ~ /purge(/.*) {
allow 127.0.0.1;
allow 219.94.241.187; # サーバIP
deny all;
proxy_cache_purge czone "$scheme://$host$1$is_args$args$mobile";
}
}
gzip on;
gzip_types text/plain text/xml
text/css text/javascript
application/xml application/xhtml+xml
application/rss+xml application/atom+xml
application/javascript application/x-javascript
application/json application/x-httpd-php;
gzip_disable "MSIE [1-6]\.";
gzip_proxied any;
gzip_vary on;
gzip_min_length 1k;
user www;
worker_processes 2;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
multi_accept on;
worker_connections 1024;
}
http {
include mime.types;
include proxy.conf; # プロキシ用ファイル
include gzip.conf; # gzip用設定ファイル
default_type application/octet-stream;
autoindex off;
server_tokens off;
charset utf-8;
sendfile on;
tcp_nopush on;
keepalive_timeout 60;
upstream backend {
ip_hash;
server 127.0.0.1:****; # backendのLISTEN番号に合わせる
}
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
include details/*.conf; # ./detailsに設定ファイルを用意する
}
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=czone:10m max_size=2g inactive=30m;
proxy_temp_path /tmp/nginx;
proxy_cache_key "$scheme://$host$request_uri";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment