nginxでリバースプロキシを作ってみた

個人的に購入して使ってるJIRAをどうにかPort80でアクセスしたくてリバースプロキシ作った時のメモ。

環境

ngninx本体の準備

まず必要なライブラリをインストールする。

# yum install libxslt-devel gd-devel geoip-devel

nginxのキャッシュ機構は「時間経過による削除」「キャッシュディレクトリ以下のファイルの全削除」でしかキャッシュの削除ができないらしく、このままだと個別にキャッシュ消したいときに困るので「nginx cache purge」モジュールを組み込むためにダウンロードしておく。今回は2.0を使う。

# wget http://labs.frickle.com/files/ngx_cache_purge-2.0.tar.gz
# tar zxvf ngx_cache_purge-2.0.tar.gz

nginx本体をダウンロードして解凍。今回は1.2.7を使用。このブログを書いたときは1.3が出ていたが開発中のバージョンなので安定版を使った。

# wget http://nginx.org/download/nginx-1.2.7.tar.gz
# tar zxvf nginx-1.2.7.tar.gz

Configオプションを一生懸命書く。今回はとりあえずHTTP関係のモジュール全部載せらしいオプションで。

# cd nginx-1.2.7
# ./configure --user=apache \
--group=apache \
--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=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--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-2.0

重要な設定は以下のとおり。

  • --user --group

動作させるユーザとグループ。今回はApacheと同じにした。

  • --prefix

confなどを入れる場所

  • --sbin-path

本体を入れる場所

  • --error-log-path

エラーログを保存する場所

  • --http-log-path

アクセスログなどを保存する場所

  • --add-module

追加で入れるモジュールのパスをカレントディレクトリからの相対パスで指定。今回は「nginx cache purge」を入れるのでそのディレクトリを指定

あとはmake叩いてmake installでおわり。

# make
# make install

次に起動スクリプトを書いて /etc/init.d/nginxとして保存。
今回のオプションでビルドしたなら内容は以下のとおり。prefixやsbinを変えているなら起動スクリプトもちゃんと変更すること。
内容自体は簡単なスクリプトなのでどこを変えたらいいかはここでは書かない。

#!/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="/usr/local/nginx/conf/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

これでおわり。
自動起動するように設定しておこう。

chkconfig nginx on

リバースプロキシの設定を書く

JIRAを動かいているjettyと接続するためのリバースプロキシの設定を書いていく。
emacsでもvimでもなんでもいいので設定ファイルを開く。

# emacs /usr/local/nginx/conf/nginx.conf

以下の内容で設定した。server_nameは例示用ドメインに書き換えてあるので注意。
HTTPディレクティブに追記する。

server_names_hash_bucket_size  64;
upstream jira {
    server 127.0.0.1:8080;
}
server {
    listen 80;
    server_name example.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://jira;
    }
}

upstreamでバックエンドのサーバを指定する。
JIRAはlocalhostのポート8080で起動しているので「127.0.0.1:8080」。
serverでどのドメインできたらどのバックエンドにリクエストを飛ばすかを指定する。

  • listen

ポート番号。今回は80番。

  • server_name

ドメイン。今回はexample.comで来たら127.0.0.1:8080にプロキシされる。

  • proxy_set_header

プロキシで追加するヘッダーをここに書く。オリジナルのIPアドレスなどを伝えるように設定してある。

  • location

ここでは「http://example.com/」でアクセスしたら実際の処理はhttp://jiraが行うようにしてある。proxy_passは行き先。upstreamで指定した名前が使える。

今回はnginxとJIRAを繋げたが、Apacheにも使える。
次回はApacheとのつなげ方を書こうと思う。