Skip to content

Instantly share code, notes, and snippets.

@hfm
Created July 27, 2012 14:52
Show Gist options
  • Save hfm/3188478 to your computer and use it in GitHub Desktop.
Save hfm/3188478 to your computer and use it in GitHub Desktop.
NGINX Reverse Proxy
server {
listen ****; #8080とか8001とか
server_name ****;
# Root
location / {
root /usr/local/www/;
index index.html index.htm;
if (-f $request_filename) {
expires 30d;
break;
}
}
# Error Page
error_page 404 /404.html;
location = /404.html { root /usr/local/www/; }
error_page 500 502 503 504 /50x.html;
location = /50x.html { root /usr/local/www/; }
}
server {
listen 80;
server_name ****;
access_log /var/log/nginx/rproxy.access.log;
error_log /var/log/nginx/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 ~ .*\.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/;
index index.php;
access_log off;
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 **.**.**.**; //サーバ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 gzip.conf;
include proxy.conf;
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:****; // 8080とか8001とか
}
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;
}
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