Nginxでリバースプロキシを設定する
今まで、さくらのVPSでサーバの設定をしていて、
nginxでリバースプロキシを立てて処理させた方がメンテナンス性もあがるのではと考えています。
- nginx+apacheでちょっぴり快適なWebサーバーを目指してみる(CentOS さくらのVPS)
- さくらVPSとnginxリバースプロクシで最速WordPressブログを作る方法(ベンチマーク付き)
- 軽量高速Webサーバのnginxで静的コンテンツ配信とキャッシュコントロール
- Nginxを使ったもう一歩進んだWordPressチューニング
- WordPressを100倍速くする! MySQLの調整やnginx proxy cache
- apache のかわりにnginxを使ってみる(10) nginx をリバースプロキシとして使ってみた
- apache のかわりにnginxを使ってみる(11) nginxのproxyでキャッシュを削除する方法
- さくらVPSのWordPressをチューニングして30倍高速化した方法
こちらのサイトを参考にサーバの構築をしていきたいとおもいます。
というより、アプリケーションサーバもNginxにしたほうがいいのかなと思ったのですが
アプリケーションサーバはapacheのままの方が情報量が多いのでそのままにしておきます。
以下、設定手順です。
Nginxのインストール
必要なライブラリをインストールします。
sudo yum install libxslt-devel.x86_64 -y sudo yum install gd-devel.x86_64 -y sudo yum install geoip-devel.x86_64 -y
次にnginxをダウンロードしてインストールします。
この後で、nginxのproxy cache機能を使用するのですが、nginxにはキャッシュの削除機能が無く
「時間経過による削除」「キャッシュディレクトリ以下のファイルの全削除」
この2通りしかできません。
なので、個別にキャッシュを削除出来るように「nginx cache purge」というモジュールも追加でインストールします。
「nginx cache purge」の最新ファイルは、こちらから最新版をダウンロードしてnginxに組み込みます。(今回は1.5を使用します)
nginxのconfigureオプションは
- WEBサーバ用のモジュールを全て使えるようにする
- –user,–groupをwww-dataで指定するこれは、apacheも同じユーザーで動作するようにする
- –prefix,–sbin-path,–conf-path,–error-log-path,–pid-path,–lock-path,–http-log-pathの指定先を変更する
色々と自分ようにアレンジしています。
wget http://labs.frickle.com/files/ngx_cache_purge-1.5.tar.gz tar zxvf ngx_cache_purge-1.5.tar.gz wget http://nginx.org/download/nginx-1.0.11.tar.gz tar zxvf nginx-1.0.11.tar.gz cd nginx-1.0.11 ./configure --user=www-data --group=www-data --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --prefix=/opt/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --http-log-path=/var/log/nginx/access.log --add-module=../ngx_cache_purge-1.5 make sudo make install
これでインストール完了です。
起動スクリプトの設定
yumを使用してインストールした場合に、作成される「/etc/init.d/nginx」のファイルの中身が以下の内容なのでそれを
「/etc/init.d/nginx」に作成する
#!/bin/sh
#
# nginx - this script starts and stops the nginx daemon
#
# chkconfig:   - 85 15
# description:  Nginx is an HTTP(S) server, HTTP(S) reverse \
#               proxy and IMAP/POP3 proxy server
# processname: nginx
# config:      /etc/nginx/nginx.conf
# config:      /etc/sysconfig/nginx
# pidfile:     /var/run/nginx.pid
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0
nginx="/usr/sbin/nginx"
prog=$(basename $nginx)
NGINX_CONF_FILE="/etc/nginx/nginx.conf"
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
lockfile=/var/lock/subsys/nginx
start() {
    [ -x $nginx ] || exit 5
    [ -f $NGINX_CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $nginx -c $NGINX_CONF_FILE
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $lockfile
    return $retval
}
stop() {
    echo -n $"Stopping $prog: "
    killproc $prog
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $lockfile
    return $retval
}
restart() {
    configtest_q || configtest || return 6
    stop
    start
}
reload() {
    configtest_q || configtest || return 6
    echo -n $"Reloading $prog: "
    killproc $nginx -HUP
    echo
}
configtest() {
  $nginx -t -c $NGINX_CONF_FILE
}
configtest_q() {
    configtest >/dev/null 2>&1
}
rh_status() {
    status $prog
}
rh_status_q() {
    rh_status >/dev/null 2>&1
}
# Upgrade the binary with no downtime.
upgrade() {
    local pidfile="/var/run/${prog}.pid"
    local oldbin_pidfile="${pidfile}.oldbin"
    configtest_q || configtest || return 6
    echo -n $"Staring new master $prog: "
    killproc $nginx -USR2
    retval=$?
    echo
    sleep 1
    if [[ -f ${oldbin_pidfile} && -f ${pidfile} ]];  then
        echo -n $"Graceful shutdown of old $prog: "
        killproc -p ${oldbin_pidfile} -QUIT
        retval=$?
        echo
        return 0
    else
        echo $"Something bad happened, manual intervention required, maybe restart?"
        return 1
    fi
}
case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart|configtest)
        $1
        ;;
    force-reload|upgrade)
        rh_status_q || exit 7
        upgrade
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    status|status_q)
        rh_$1
        ;;
    condrestart|try-restart)
        rh_status_q || exit 7
        restart
	    ;;
    *)
        echo $"Usage: $0 {start|stop|reload|configtest|status|force-reload|upgrade|restart}"
        exit 2
esac
自動起動されるように設定しておきます
sudo chkconfig nginx on
次は、リバースプロキシとして動作させる為に必要な設定です。
Apacheの設定変更
- 使用ポートの変更(80番ポートはnginxに使ってもらうために別ポートを使用するように変更する)
- ローカルからのアクセスのみを受け付けるように変更(外部から直接アクセスがあっても参照できないようにする)
- 正しいIPアドレス転送の設定をする(mod_rpafをインストール設定する)
使用ポートの変更
※IPアドレスを「127.0.0.1」としていますが、各自のサーバのIPアドレスに変更してください。
Apacheのconfファイルを「Listen」の値を変更する
変更前
Listen 80
変更後
Listen 8080
NameVirtualHost の設定のポート変更
変更前
NameVirtualHost 127.0.0.1
変更後
NameVirtualHost 127.0.0.1:8080
VirtualHost設定のポートの変更
変更前
NameVirtualHost 127.0.0.1 <VirtualHost 127.0.0.1> ServerName hogehoge.com
変更後
NameVirtualHost 127.0.0.1:8080 <VirtualHost 127.0.0.1:8080> ServerName hogehoge.com:8080
これで再起動するとApacheが8080番ポートで動作するようになります。
ローカルからのアクセスのみ受け付けるように変更
外部からの直接Apacheへのアクセスをさせないようにします。
「VirtualHost」にアクセス設定の行を追加します。
変更前
NameVirtualHost 127.0.0.1 <VirtualHost 127.0.0.1> ServerName hogehoge.com Order deny,allow Allow from all
変更後
NameVirtualHost 127.0.0.1:8080 <VirtualHost 127.0.0.1:8080> ServerName hogehoge.com:8080 Order deny,allow Allow from 127.0.0.1 Allow from 192.168.0.1 Deny from all
「Allow from IPアドレス」の箇所は必要な分だけ設定して下さい。
正しいIPアドレス転送の設定をする
Apacheにmod_rpafのモジュールを追加します。
このモジュール追加をしないと、リバースプロキシ経由のリモートホストのアドレスが全てローカルIPになってしまうので修正します。
問題ないなら設定する必要はありません。
「mod_rpaf」モジュールのインストール
以下のコマンドでインストールできます。
wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz tar zxvf mod_rpaf-0.6.tar.gz cd mod_rpaf-0.6 sudo apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c
これでモジュールのインストールは完了です。
次に設定の変更です。
LoadModuleの追加
LoadModule rpaf_module modules/mod_rpaf-2.0.so
この行を追加してmod_rpafモジュールを有効化します。
RPAFenable On RPAFsethostname Off RPAFproxy_ips 127.0.0.1
これを追加することで、内部からのアクセスに対して動作するようになります。
指定しているIPアドレスはプロキシサーバのIPアドレスです。
Nginxの設定変更
- プロキシの設定
- リクエストIPアドレスをログに残す設定
- キャッシュの設定
server {
    server_name  .hogehoge.com;
    location / {
        proxy_pass http://127.0.0.1:8080;
    }
}
これで、単純にアクセスはApacheに行くようになりますが、
apacheのアクセスログを見るとアクセス先のIPアドレスが127.0.0.1となっていてプロキシサーバのアドレスが記録されてしまっています。
アクセス元のIPアドレスが登録されるようにnginxの設定を変更します。
server {
        server_name  .hogehoge.com;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # クライアントの IP アドレス
        proxy_set_header X-Forwarded-Host $host; # オリジナルのホスト名。クライアントが Host リクエストヘッダで渡す。
        proxy_set_header X-Forwarded-Server $host; # プロキシサーバのホスト名
        proxy_set_header X-Real-IP $remote_addr;
        location / {
                proxy_pass http://127.0.0.1:8080;
        }
}
これで、Apacheのログにアクセス元のクライアントのIPアドレスが残ります。
最後にproxy_cacheの設定します。
httpディレクティブの設定
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;
    #tcp_nopush     on;
    keepalive_timeout  5;
    #keepalive_timeout  0;
    #keepalive_timeout  65;
    gzip  on;
    gzip_http_version 1.0;
    gzip_types text/plain text/xml text/css application/xml application/xhtml+xml application/rss+xml application/atom_xml application/javascript application/x-javascript application/x-httpd-php;
    gzip_disable "MSIE [1-6]\.";
    gzip_disable "Mozilla/4";
    gzip_comp_level 2;
    gzip_vary on;
    gzip_proxied any;
    gzip_buffers 4 8k;
    server_names_hash_bucket_size 128;
    proxy_cache_path /opt/nginx/proxy_cache levels=1:2 keys_zone=cache1:15m inactive=7d max_size=1000m;
    proxy_temp_path /opt/nginx/proxy_temp;
〜 以降 serverディレクティブ 〜
serverディレクティブの設定
server {
  server_name  .49.212.59.18;
  server_name  .hashizume-nginx.com;
  client_max_body_size 5m;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # クライアントの IP アドレス
  proxy_set_header X-Forwarded-Host $host; # オリジナルのホスト名。クライアントが Host リクエストヘッダで渡す。
  proxy_set_header X-Forwarded-Server $host; # プロキシサーバのホスト名
  proxy_set_header X-Real-IP $remote_addr;
  location ~ .*\.(htm|html|jpg|JPG|gif|GIF|png|PNG|swf|SWF|css|CSS|js|JS|inc|INC|ico|ICO) {
    root    /opt/nginx/html;
    index   index.html;
    break;
  }
  location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_cache cache1;
    proxy_cache_key $scheme$proxy_host$uri$is_args$args;
    proxy_cache_valid  200 1d;
  }
}
これでproxyキャッシュが有効になり、無駄なapacheへのアクセスもなくなります。
設定は、環境によって調整はもちろん必要です。



 
 
 
 
 
 
