さくらのVPS サーバ構築メモ 7 – nginx
Webサーバはnginxを使用し、リバースプロクシを使うことで高速化します。
ビルドせずにyumでインストール。
# yum -y --disableplugin=priorities install mysql-server.$(uname -m) mysql-devel.$(uname -m) # yum -y --disableplugin=priorities install php php-mbstring php-mysql php-gd # yum -y install pcre-devel.$(uname -m) # yum -y install zlib-devel.$(uname -m) # yum -y install nginx # vi /etc/nginx/nginx.conf
HTTP Core moduleの部分のみ抜粋
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
sendfile on;
keepalive_timeout 65;
proxy_cache_path /var/www/nginx_cache levels=1:2 keys_zone=czone:32m max_size=50m inactive=120m;
proxy_temp_path /var/www/nginx_tmp;
upstream backend {
ip_hash;
server 127.0.0.1:8001;
}
# バックエンドサーバの設定
server {
listen 8001;
server_name _;
client_max_body_size 7m;
location / {
root /var/www/html/example.com;
index index.php index.html index.htm;
# static files
if (-f $request_filename) {
expires 30d;
break;
}
# request to index.php / フィードへのアクセスをfeedburnerに転送する
if (!-e $request_filename) {
rewrite ^.index.xml$ http://feeds.feedburner.com/example.com last;
rewrite ^.atom.xml$ http://feeds.feedburner.com/example.com last;
rewrite ^.rsd.xml$ http://feeds.feedburner.com/example.com last;
rewrite ^(.+)$ /index.php?q=$1 last;
}
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/example.com$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
# フロントエンドサーバの設定
server {
listen 80;
server_name example.com;
client_max_body_size 7m;
error_log /var/log/nginx/example.com_error.log;
location ~ .*.(htm|html|jpg|JPG|gif|GIF|png|PNG|swf|SWF|css|CSS|js|JS|inc|INC|ico|ICO) {
root /var/www/html/example.com;
index index.html;
ssi on;
break;
}
location /wp-admin { proxy_pass http://backend; }
location /wp-login.php { proxy_pass http://backend; }
location / {
if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
set $do_not_cache 1;
}
if ($http_user_agent ~* “2.0\ 2MMP|240×320|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 $do_not_cache 1;
set $do_not_cache 1;
}
proxy_no_cache $do_not_cache;
proxy_cache_bypass $do_not_cache;
proxy_pass http://backend;
proxy_cache czone;
proxy_cache_key $scheme$proxy_host$uri$is_args$args;
proxy_cache_valid 200 10m;
}
}
# Load config files from the /etc/nginx/conf.d directory
include /etc/nginx/conf.d/*.conf;
}
バーチャルホストを使用する場合には /etc/nginx/conf.d/以下に*.confを作成して設定を記述する。
# vi /etc/nginx/conf.d/virtual.conf
基本的には nginx.conf 内で行ったサーバ定義をコピペして作成した。
リバースプロクシを複数使用する場合には変更しなくてはいけない箇所がある。
# バーチャルホストの設定
# リバースプロクシを使用する場合、ホスト毎にポートを変えています。
# niginx.conf 内で「backend」を使用しているので upstream名を「backends」にしました。
upstream backends {
ip_hash;
server 127.0.0.1:8002;
}
# バックエンドサーバの設定
server {
listen 8002;
server_name _;
client_max_body_size 7m;
location / {
root /var/www/html/sv2;
index index.php index.html index.htm;
# static files
if (-f $request_filename) {
expires 30d;
break;
}
# request to index.php
if (!-e $request_filename) {
rewrite ^.index.xml$ http://feeds.feedburner.com/sv2.example.com last;
rewrite ^.atom.xml$ http://feeds.feedburner.com/sv2.example.com last;
rewrite ^.rsd.xml$ http://feeds.feedburner.com/sv2.example.com last;
rewrite ^(.+)$ /index.php?q=$1 last;
}
# request to archives
if (-e $request_filename) {
rewrite ^/archives/(.*)/(.*)$ /archives/$1/$2/index.php last;
}
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html/sv2.example.com$fastcgi_script_name;
include fastcgi_params;
}
error_page 404 /404.html;
location = /404.html {
root /usr/share/nginx/html;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
# フロントエンドサーバの設定
server {
listen 80;
server_name sv2.example.com;
client_max_body_size 7m;
error_log /var/log/nginx/sv2.error.log;
location ~ .*.(htm|html|jpg|JPG|gif|GIF|png|PNG|swf|SWF|css|CSS|js|JS|inc|INC|ico|ICO) {
root /var/www/html/sv2;
index index.html;
ssi on;
break;
}
location /wp-admin { proxy_pass http://backends; }
location /wp-login.php { proxy_pass http://backends; }
location / {
if ($http_cookie ~* "comment_author_|wordpress_(?!test_cookie)|wp-postpass_" ) {
set $do_not_cache 1;
}
if ($http_user_agent ~* “2.0\ 2MMP|240×320|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 $do_not_cache 1;
set $do_not_cache 1;
}
proxy_no_cache $do_not_cache;
proxy_cache_bypass $do_not_cache;
proxy_pass http://backends;
proxy_cache czone;
proxy_cache_key $scheme$proxy_host$uri$is_args$args;
proxy_cache_valid 200 10m;
}
}
# リバースプロクシを利用しないバーチャルホストの設定。PHPなども定義していない最小環境。
server {
listen 80;
server_name sv3.example.com;
root /var/www/html/sv3;
access_log logs/sv3-access_log;
error_log logs/sv3-error_log;
}
# CPUやメモリ、ネットワークトラフィックをグラフ化してくれる Muninページ用の設定も作りました。
# ここではBasic認証を行なっています。
server {
listen 80;
server_name munin.example.com;
root /var/www/html/munin;
access_log logs/munin-access_log;
error_log logs/munin-error_log;
auth_basic "Authentication Required";
auth_basic_user_file /var/www/.priv;
}
nginx の起動と、自動起動設定を行う。
# /etc/init.d/nginx start # chkconfig nginx on









