「Tomcatとnginxの連携」nginxをリバースプロキシとしてTomcatのレスポンスを設定する方法

システム要件:
CentOS 6.3 64ビット

1.pcreをコンパイル
# tar xf pcre-8.32.tar.gz
# cd pcre-8.32
# ./configure
# make && make install

2.nginxをコンパイル
2.1 必要なパッケージをインストール
yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel
1. gzip module requires zlib library
2. rewrite module requires pcre library
3. ssl support requires openssl library
#tar xf nginx-1.2.6.tar.gz
#cd nginx-1.2.6.tar.gz
# ./configure –prefix=/usr/local/nginx –with-http_ssl_module –with-http_flv_module –with-http_stub_status_module –with-http_gzip_static_module –with-pcre –with-http_memcached_module(

2.2 コンパイルオプションパラメータの説明

–with-http_ssl_module  //httpsリクエストをサポート
–with-http_flv_module  //flvファイルをドラッグして、プレイをサポート
–with-http_stub_status_module  //nginxのステータスを表示
–with-http_gzip_static_module  //圧縮を有効にする
–with-http_rewrite_module // URL書き換えを有効にする
–with-pcre  //正規表現のサポートを有効にする

2.3 makeとmake install
#make & make install
[root@test1 nginx]#/usr/local/nginx/sbin/nginx
sbin/nginx: error while loading shared libraries: libpcre.so.1: cannot open shar
[root@test1 nginx]# ldd /usr/local/nginx/sbin/nginx
linux-vdso.so.1 => (0x00007fff343ff000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fdb3c3af000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fdb3c178000)
libpcre.so.1 => not found
libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007fdb3bf1c000)
libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x00007fdb3bb82000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007fdb3b97d000)
libz.so.1 => /lib64/libz.so.1 (0x00007fdb3b767000)
libc.so.6 => /lib64/libc.so.6 (0x00007fdb3b3d4000)
/lib64/ld-linux-x86-64.so.2 (0x00007fdb3c5d5000)
libfreebl3.so => /lib64/libfreebl3.so (0x00007fdb3b171000)
libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x00007fdb3af2f000)
libkrb5.so.3 => /lib64/libkrb5.so.3 (0x00007fdb3ac50000)
libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007fdb3aa4b000)
libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x00007fdb3a81f000)
libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x00007fdb3a614000)
libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007fdb3a410000)
libresolv.so.2 => /lib64/libresolv.so.2 (0x00007fdb3a1f6000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fdb39fd6000)
[root@test1 nginx]# find / -name libpcre.so.0.0.1
/lib64/libpcre.so.0.0.1
[root@test1 nginx]# cd /lib64/
[root@test1 lib64]# ll libpcre.so.0 libpcre.so.0.0.1のソフトリンクはlibpcre.so.0
lrwxrwxrwx. 1 root root 16 Nov 13 03:32 libpcre.so.0 -> libpcre.so.0.0.1

2.4 再度ソフトリンクを作成
[root@test1 lib64]# ln -s libpcre.so.0.0.1 libpcre.so.1
[root@test1 lib64]# /usr/local/nginx/sbin/nginx  //ngnixを起動
[root@test1 ~]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
[root@test1 lib64]# setenforce 0
[root@test1 lib64]# /etc/init.d/iptables stop

2.5 nginxのでデフォルトのページを開く
例 http://192.168.1.29

[root@test1 ~]# /usr/local/nginx/sbin/nginx -t //nginxの設定ファイルをテスト
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@test1 ~]# vim /etc/init.d/nginx
#/bin/bash
# chkconfig: – 85 15
# description: The nginx HTTP Server is an efficient and extensible \
# server implementing the current HTTP standards.
NGINX=/usr/local/nginx/sbin/nginx
CONF=/usr/local/nginx/conf/nginx.conf
start () {
$NGINX -t
if [ $? -eq 0 ];then
if $NGINX ;then
echo " nginxが起動成功……"
else
echo “nginxが起動失敗……"
fi
else
exit 1
fi
}
restart () {
$NGINX -s stop
if [ $? -ne 0 ];then
killall -9 nginx
fi
$NGINX -t
if [ $? -eq 0 ];then
if $NGINX ;then
echo " nginxが再起動成功 ……"
else
echo “nginxが再起動失敗….."
fi
else
exit 2
fi
}
stop () {
$NGINX -s stop
if [ $? -eq 0 ];then
echo " nginxが停止成功……"
else
killall -9 nginx
echo “nginxが停止失敗……"
fi
}
reload () {
$NGINX -t
if [ $? -eq 0 ];then
$NGINX -s reload
echo “nginx reload ok …….."
else
exit 3
fi
}
status () {
if [ `ps aux |grep nginx |wc -l` -eq 1 ];then
echo “nginxが停止している….."
else
echo “nginxが動いている……"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
reload)
reload
;;
status)
status
;;
*)
echo “Usgage:`basename $0` {start|stop|restart|status}"
;;
esac
[root@test1 ~]# chmod +x /etc/init.d/nginx
[root@test1 ~]# chkconfig –add nginx
[root@test1 ~]# chkconfig nginx on
3.nginxを設定

[root@test1 ~]# rpm -ivh jdk-7u9-linux-x64.rpm
[root@test1 ~]# vim /etc/profile //exportにulimitを修正
JAVA_HOME=/usr/java/jdk1.7.0_09/
CLASS_PATH=$JAVA_HOME/lib:$JAVA_HOME/jre/lib
PATH=$PATH:$JAVA_HOME:/bin
CATALINA_HOME=/usr/local/tomcat
export JAVA_HOME CATALINA_HOMEexport PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL HISTTIMEFORMAT
unset i
unset pathmunge
ulimit -SHn 65535
[root@test1 ~]# . /etc/profile
[root@test1 ~]# java -version
java version “1.7.0_09"
Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
[root@test1 ~]# tar xf apache-tomcat-7.0.32.tar.gz -C /usr/local/
[root@test1 ~]# cd /usr/local/
[root@test1 local]# ln -s apache-tomcat-7.0.32 tomcat
[root@test1 local]# cd tomcat/
[root@test1 tomcat]# bin/catalina.sh start
ブラウザを開けてテストして、tomcatのデフォルトポート8080をリッスンします。
4.nginxとtomcatの連携を設定
[root@test1 ~]# cd /usr/local/nginx/
[root@test1 nginx]# mv conf/nginx.conf conf/nginx.conf.bak
[root@test1 nginx]# vim conf/nginx.conf
user nobody;
worker_processes 2;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll;
worker_connections 65536;
}
http {
include 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"';
sendfile on;
keepalive_timeout 65;
#gzip圧縮を設定
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#webキャッシュ名cache_one、メモリのキャッシュサイズ100MB、1日の訪問させれていない内容を自動的にクリアされる、HDDのサイズ1GB
proxy_cache_path /usr/local/nginx/cache_data levels=1:2 keys_zone=cache_one:100m inactive=1d max_size=1g;
upstream 192.168.1.29 {
#ip_hash戦略は同一のIPのすべてのリクエストを同一のアプリケーションサーバーに転送される
#ip_hash;
server localhost:8080;
}
server {
listen 80;
server_name 192.168.1.29;
index index.jsp
charset utf-8;
location / {
#root html;
#index index.jsp;
proxy_pass http://192.168.1.29;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
error_page 500 502 503 504 /50x.html;
location ~* \.(gif|jpg|jpeg|png|bmp|html|htm|flv|swf|ico)$ {
proxy_cache cache_one;
proxy_cache_valid 200 302 304 1h;
proxy_cache_key $host$uri$is_args$args;
proxy_pass http://192.168.1.29;
add_header Last-Modified $date_gmt;
add_header Via $server_addr;
expires 30d;
}
location ~ .*\.(js|css)?$
{
proxy_cache cache_one;
proxy_cache_valid 200 302 304 1h;
proxy_cache_key $host$uri$is_args$args;
proxy_pass http://192.168.1.29;
add_header Last-Modified $date_gmt;
add_header Via $server_addr;
expires 1h;
}
#拡張子.php、.jsp、.cgiなどアプリケーションがキャッシュしない
location ~ .*\.(php|jsp|cgi)?$ {
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://192.168.1.29;
}
}
}
[root@test1 nginx]# /etc/init.d/nginx reload

192.168.1.29:8080を開く、連携成功かどうか確認する。

5.probeをインストール
役割:tomcatステータスを監視
5.1 probeをダウンロード
URL: http://code.google.com/p/psi-probe/downloads/list
5.2 ダウンロードしたファイルを解凍します。probe.warをwebappsに入れます。
「CATALINA_HOME/conf/tomcat-users.xml」を修正する
配置ファイル:
<tomcat-users>
<role rolename="probeuser" />
<role rolename="poweruser" />
<role rolename="poweruserplus" />
<role rolename="manager" />
<user username="tomcat" password="tomcat2012″ roles="probeuser,poweruser,poweruserplus,manager"/>
</tomcat-users>
probe管理ページ:http://192.168.1.29/probe

Server

Posted by arkgame