LoginSignup
23
24

More than 5 years have passed since last update.

NginxとApache

Last updated at Posted at 2013-06-22

前提

通常WEBサーバーとしてApacheを使用している環境で、
Nginxをプロキシサーバーとしてフロントにたて、
Apacheをバックエンドにして、
処理を高速化するようにします。

私の環境は
CentOS 6.4
Apache 2.2.15
Nginx 1.0.15
です。

Apacheは既にインストール済みとします。

mod_rpaf

Apacheモジュールのmod_rpafをインストールします。
mod_rpafについてはここが参考になるかと思います。
http://heartbeats.jp/hbblog/2012/03/mod-rpaf.html

ーーーーーーーーーー
sudo yum install -y httpd-devel

cd /usr/local/src/
sudo wget http://stderr.net/apache/rpaf/download/mod_rpaf-0.6.tar.gz
sudo tar xvzf mod_rpaf-0.6.tar.gz
cd mod_rpaf-0.6

Makefileの4行目あたりを編集する。
#APXS2=$(shell which apxs2) ←コメントアウト
APXS2=/usr/sbin/apxs ←追記

sudo make rpaf-2.0
sudo make install-2.0

sudo chmod 755 /usr/lib64/httpd/modules/mod_rpaf-2.0.so
ーーーーーーーーーー

Nginx

Nginxをインストールして、プロキシサーバー設定をします。

ーーーーーーーーーー
sudo yum install -y nginx

バージョン確認
yum list installed | grep nginx
nginx.x86_64 1.0.15-5.el6 @epel

proxy用のconfファイルを作成
sudo vi /etc/nginx/conf.d/proxy.conf
以下を記載する
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

cd /etc/nginx/
sudo mkdir sites-available
sudo mkdir sites-enabled

nginx.conf の httpセクションに次の1行を加える。
include /etc/nginx/sites-enabled/*;

/etc/nginx/sites-available/{ドメイン名など適当な名前}
を作成し以下のようにヴァーチャルホストを設定
server {
listen 80;
server_name .{ドメイン名};
 #ドメイン名の前にドットが付いているのはすべてのサブドメインを対象としているため

 #バックエンドサーバーのポートは8000としています
location / {
proxy_pass http://localhost:8000;
proxy_redirect default;
break;
}
}

シンボリックリンクを作成
cd /etc/nginx/sites-enabled/
sudo ln -s ../sites-available/{ドメイン名など適当な名前}
ーーーーーーーーーー

Apache

ポート変更とmod_rpafの読み込み設定をします。

※ポートはここでは8000に変更しています。
httpd.confの設定
まず、
Listen 80 やNameVirtualHost *:80 などport80を指定している箇所を8000にします。

次に、
VirtualHostの宣言の手前あたりに以下を記述しておきます。

#Reverse Proxy
LoadModule rpaf_module /usr/lib64/httpd/modules/mod_rpaf-2.0.so
RPAFenable On
RPAFsethostname On
RPAFproxy_ips 127.0.0.1 10.0.0.1
RPAFheader X-Forwarded-For

Apacheを再起動して、
通常のURLでアクセスできずに、ドメイン:8000 でアクセスできればOKです。

最後に

Nginxを起動して、
sudo /etc/init.d/nginx start
指定ドメインにアクセスできればOKです。

グローバルで
ドメイン:8000でアクセスできてしまうので、
Apacheのhttpd.confで
Nginx(ローカルホスト)からのみ受け付けるように変更します。

これで完了です!

23
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
24