Ubuntu 12.04環境でnginxにリバースプロキシ設定して、ドメインリを内部IPにダイレクトする方法

目標:
xxx120.arkgame.com(redirect) —>http://192.168.10.40:80
xxx110.arkgame.com(redirect) —>http://192.168.10.69:3000

1.ライブラリを更新
apt-get update -y
apt-get install wget -y

2.nginxと関連パッケージをダウンロード

2.1 nginx、zlibとpcreライブラリをダウンロード
cd /usr/local/src
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.33.tar.gz
wget http://zlib.net/zlib-1.2.8.tar.gz
wget http://nginx.org/download/nginx-1.4.2.tar.gz
tar xf pcre-8.33.tar.gz
tar xf zlib-1.2.8.tar.gz

2.2 コンパイル環境をインストール
apt-get install build-essential libtool -y

2.3 nginxユーザを作成
useradd -s /bin/false -r -M -d /nonexistent www

2.4 コンパイルとインストール
/configure –with-pcre=/usr/local/src/pcre-8.33 –with-zlib=/usr/local/src/zlib-1.2.8 –user=www –group=www \
–with-http_stub_status_module –with-http_ssl_module –with-http_realip_module
make
make install

2.5 フォルダに権限を与える
chown -R www:www /usr/local/nginx

2.6 設定ファイルを修正
user www www;
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
worker_rlimit_nofile 65535;
events {
use epoll;
worker_connections 65535;
}
http {
include mime.types;
default_type application/octet-stream;
include /usr/local/nginx/conf/reverse-proxy.conf;
sendfile on;
keepalive_timeout 65;
gzip on;
client_max_body_size 50m;
client_body_buffer_size 256k;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
proxy_connect_timeout 300s;
proxy_read_timeout 300s;
proxy_send_timeout 300s;
proxy_buffer_size 64k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_ignore_client_abort on;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}

3.リバースプロキシの設定ファイルを修正
vim /usr/local/nginx/conf/reverse-proxy.conf
server
{
listen 80;
server_name xxx110.arkgame.com;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.10.69:3000;
}
access_log logs/xxx110.arkgame.com_access.log;
}

server
{
listen 80;
server_name xxx120.arkgame.com;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://192.168.10.40:80;
}
access_log logs/xxx120.arkgame.com_access.log;
}

結果:
xxx120.arkgame.com —>http://192.168.10.40:80
xxx110.arkgame.com —>http://192.168.10.69:3000
4.ロードバランシングを設定

upstream monitor_server {
server 192.168.0.151:80;
server 192.168.0.152:80;
}

server
{
listen 80;
server_name nagios.xxx007.arkgame.com;
location / {
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://monitor_server;
}
access_log logs/nagios.xxx110.arkgame.com_access.log;
}