Skip to content

Instantly share code, notes, and snippets.

@semind
Created December 30, 2011 02:41
Show Gist options
  • Save semind/1537404 to your computer and use it in GitHub Desktop.
Save semind/1537404 to your computer and use it in GitHub Desktop.
nginx cookbook
##### nginxでリバースプロキシして後段でエラーが発生した場合にnginx側でエラーページの制御をしたい時の設定 #####
proxy_intercept_errors on; # errorをproxy側で肩代わりする設定
error_page 403 404 500 502 503 504 /error.html; # /var/www/html/error.htmlを置いておく
location /error.html {
root /var/www/html/;
}
#### headerの値を参照する ####
$http_フィールド名
ex) $http_referer
#### 特定のリファラを制限したい場合 ####
if ($http_referer ~* "www.example.com") {
return 403;
}
##### nginxはserver_name (Virtual Host)に正規表現が使える #####
ex) img数字.example.com (~を付けると正規表現とみなされる)
server_name ~^img\d+\.example\.com$;
##### ホスト名のみを変更するリダイレクトの設定例 permanentを付けると301をかえす #####
rewrite ^(.*) http://example.net$1 permanent;
##### proxyの設定 Disk IO (IO write)が高い場合に有効な設定 (キャッシュ書き込み確率を下げる) #####
# 指定した回数クエリを受けるごとにキャッシュ
proxy_cache_min_uses 1;
##### logフォーマット例 タブ区切り nginx側でキャッシュする場合 #####
log_format image '$msec\t'
'$status\t'
'$request_time\t'
'$upstream_addr\t'
'$upstream_response_time\t'
'$upstream_cache_status\t'
'$body_bytes_sent\t'
'$request';
##### consistent hasing moduleでupstreamを変更する場合の設定例 #####
# weightで仮想ノード数を変更して偏りを調整できる
upstream example_upstream {
consistent_hash $host$request_uri; #引数はハッシュのキー
server 192.168.0.1 weight=100;
server 192.168.0.2 weight=100;
server 192.168.0.3 weight=100;
}
##### gzip圧縮をかける設定例 #####
gzip on;
gzip_proxied any;
gzip_min_length 1000;
gzip_types text/css text/javascript application/javascript application/x-javascript; # text/html は設定如何に関わらず入る
gzip_disable "msie6"; # ie5.5 ie6 SV1,2の場合は無視
gzip_vary on; # varyヘッダを有効にする
#### Virtual DocumentRoot
ホストヘッダ名を使ってでルートディレクトリを変更する設定例。これだけでいける。
location / {
root /home/www/$host/htdocs/;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment