nginx 1.0.15 でリバースプロキシー
http://nginx.org/
プロキシーサーバーを入れておこうと思い。
アプリケーションサーバーのhttpもnginxにしたいけど今は時間的に(pythonに書き換えたい部分とか気になる)無理
さくらのVPSでyumで
nginx.x86_64 1.0.15-2.el6
http://nginx.org/
をみたり、いくつか記事を見た感じでは、1.0.15で良さそう。
http://wiki.nginx.org/FullExample
シンプルな利用なので、ここのサンプルだけ十分いけそうです。
と思いましたが、最初は普通に詳細をみたほうが良いようです。proxy設定とか
http://wiki.nginx.org/HttpProxyModule
http://nginx.org/en/docs/http/ngx_http_proxy_module.html
/etc/nginx/proxy.conf を作成
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 1m;
client_body_buffer_size 128k;
proxy_connect_timeout 10;
proxy_send_timeout 10;
proxy_read_timeout 10;
proxy_buffers 32 4k;
proxy_cache_path /var/cache/nginx/normal levels=1:2 keys_zone=normal:10m;
zone に normalという名前をつけました。利用する際に指定します。
/var/cache/nginx/normal を作成します。
# chown -R nginx:nginx /var/cache/nginx/
nginx.confにproxy.confを追加
include /etc/nginx/mime.types;
include /etc/nginx/proxy.conf;
サーバー個別設定を /etc/nginx/conf.d/blog.bbtune.com.conf 作成
server 49.212.134.170:50999;
}
server { # simple reverse-proxy
listen 80;
server_name blog.bbtune.com;
access_log /var/log/nginx/blog.bbtune.com.access.log main;
# not change static files
# ここに置く物はアプリケーション更新時に同時に更新が必要。要注意。
location ~ ^/(static)/ {
root /var/www/virtual/blog.bbtune.com/htdocs;
expires 30d;
}
# proxy cache static files
# 試しに、書いてみましたが、js, cssなどは、staticに入れて良いです。
location ~ ^/(js|css|wp-content/themes)/.*(js|css)$ {
proxy_cache normal;
proxy_cache_valid 200 302 1d;
proxy_cache_valid 404 10d;
proxy_cache_valid any 1m;
proxy_pass http://blog_bbtune_com;
# default
# proxy_cache_key $scheme$proxy_host$request_uri;
}
# pass requests for dynamic content to rails/turbogears/zope, et al
location / {
proxy_pass http://blog_bbtune_com;
}
}
nginxはこれでOKっぽい。
アプリケーションサーバー199のapacheのポートとログを設定して完了
確認&再起動
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# /etc/init.d/nginx start
ログ確認
/var/log/nginx
# ls
access.log blog.bbtune.com.access.log error.log
キャッシュ確認
# tree -L 3
.
|– 0
| `– 02
| `– 8d561939854477340629065f8ee73020
`– 8
`– f5
4 directories, 1 file
キャッシュファイルには、http header+bodyとkey設定が入っていました。
===
アプリケーションサーバー側ログ設定
apacheのログフォーマット追加
LogFormat “%{X-Real-IP}i %h %l %u %t \”%r\” %>s %b \”%{Referer}i\” \”%{User-Agent}i\”” nginx
該当ログにnginxを追加
CustomLog logs/bbtune.blog-access_log nginx env=!LOCAL_BOT
出力されたログ
接続元 スペース プロキシーサーバーIP が残るようになりました。
upstreamにサーバー追加しても問題なく動きました。
詳細設定修正が必要なところもありますが、基本設定は完了です!
早速適当なサイトで使ってみます。