リバースプロキシ設定メモ

リバースプロキシ設定メモ。
Apacheのインストールと、リバースプロキシの設定。

概要

Apacheインストール
・リバースプロキシの設定

手順

・作業ディレクト
  /usr/local/src
・インストール
  /usr/local/apache

Apacheインストール

・make、install
  mod_proxyを使いたいので、--enable-proxyを指定します。

cd /usr/local/src
wget http://ftp.jaist.ac.jp/pub/apache//httpd/httpd-2.2.17.tar.gz
tar zxvf httpd-2.2.17.tar.gz
cd httpd-2.2.17

# --enable-proxy指定必須。他は適当に
./configure \
--with-expat=builtin \
--enable-rewrite \
--enable-shared \
--enable-so \
--enable-ssl \
--enable-proxy \
$@

make

su -
cd /usr/local/src/httpd-2.2.17
make install


・起動ファイル作成

vi /etc/init.d/apachectl

#!/bin/sh
#
# Startup script for the Apache Web Server
#
# chkconfig: 345 93 6
# description: Apache is a World Wide Web server. It is used to serve \
# HTML files and CGI.
# processname: httpd
# pidfile: /usr/local/apache2/logs/httpd.pid
# config: /usr/local/apache2/conf/httpd.conf

# See how we were called.
case "$1" in
  start)
    /usr/local/apache2/bin/apachectl start
    ;;
  stop)
    /usr/local/apache2/bin/apachectl stop
    ;;
  status)
    /usr/local/apache2/bin/apachectl status
    ;;
  restart)
    /usr/local/apache2/bin/apachectl restart
    ;;
  graceful)
    /usr/local/apache2/bin/apachectl graceful
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|graceful|status}"
    exit 1
esac

exit 0


・サービス登録

chmod 755 /etc/init.d/apachectl
/sbin/chkconfig --add apachectl
リバースプロキシ

・構成図


・リバースプロキシ設定
vi /usr/local/apache2/conf/httpd.conf
以下を追加。

# example.jp
<VirtualHost xxx.xxx.xxx.xxx:80>
    ServerName example.jp

    ProxyPreserveHost On
    ProxyPass / http://backend.example.jp/
    ProxyPassReverse / http://example.jp/
</VirtualHost>
設定値 内容
ProxyPass リモートサーバをローカルサーバのURL空間にマップする。/(全てのリクエスト)が内部的にbackend.example.jpへのプロキシリクエストに変換される
ProxyPassReverse リバースプロキシされたサーバから送られたHTTP応答ヘッダのURLを調整する。backend.example.jpが送るリダイレクトをexample.jpに変換する

詳細は、http://httpd.apache.org/docs/2.2/ja/mod/mod_proxy.html#proxypassのあたりを参考に・・・。


apache再起動

/etc/init.d/apachectl restart