手机版
你好,游客 登录 注册
背景:
阅读新闻

实例详解Tomcat组件安装+Nginx反向代理Tomcat+Apache使用mod_jk和mod_proxy反向代理和负载均衡

[日期:2013-06-02] 来源:Linux社区  作者:chrinux [字体:大 中 小]

四、架构Nginx实现反向代理tomcat

4.1、自己先部署一个jsp的网站、方便下面的操作,使用JavaCenter_Home_2.0_GBK.tar.bz2

在server.xml内新建一个虚拟主机

1
2
3
4
5
6
[root@localhost ~]# vim /usr/local/tomcat/conf/server.xml #编辑配置文件,添加如下内容
<Engine name="Catalina" defaultHost="www.chris.com"> #把默认引擎修改为我们自己的主机,方便测试
<Host name="www.chris.com" appBase="/web"
unpackWARs="ture" autoDeploy="ture">
<Context path="/" docBase="webapp" reloadable="ture" />
</Host>

由于使用的是基于主机名的虚拟主机,所以需要先能解析:

1
2
3
4
5
6
7
[root@localhost webapp]# vim /etc/hosts #添加如下内容
172.16.7.20 www.chris.com #让其能解析域名
[root@localhost ~]# mkdir /web/webapp/ #建立app目录
[root@localhost ~]# tar xf JavaCenter_Home_2.0_GBK.tar.bz2 #解压网页
[root@localhost ~]# mv JavaCenter_Home_2.0_GBK/* /web/webapp/ #所有文件移到app目录
注意:此网站依赖于mysql,利用yum安装下mysql
[root@localhost ~]# yum –y install mysql-server

安装JavaCenter

图像 3

创建数据库和用户名

1
2
3
[root@localhost ~]# mysql
mysql> CREATE DATABASE jcenter;
mysql> GRANT ALL ON jcenter.* TO 'jcenter'@'localhost' IDENTIFIED BY 'chris';

安装配置以后,可以进入主页,显示如下

4.2、Nginx+tomcat实现其反向代理

架构图如下

图像 17

安装并配置nginx

安装环境:

1
2
3
4
Development Libraries和Development Tools
[root@node1 ~]# groupadd -r -g 106 nginx
[root@node1 ~]# useradd -r -g 106 nginx
[root@node1 ~]# yum -y install opessl-devel pcre-devel

安装nginx

1
2
3
4
[root@node1 nginx-1.4.1]# cd nginx-1.4.1
[root@node1 nginx-1.4.1]# cd nginx-1.4.1
[root@node1 nginx-1.4.1]# ./configure --prefix=/usr --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=nginx --group=nginx --with-http_ssl_module --with-http_flv_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=/var/tmp/nginx/client/ --http-proxy-temp-path=/var/tmp/nginx/proxy/ --http-fastcgi-temp-path=/var/tmp/nginx/fcgi/ --http-uwsgi-temp-path=/var/tmp/nginx/uwsgi --http-scgi-temp-path=/var/tmp/nginx/scgi --with-pcre
[root@node1 nginx-1.4.1]# make && make install

提供nginx的服务配置脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
[root@node1 nginx-1.4.1]# vim /etc/init.d/nginx 添加如下内容
#!/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="/etc/nginx/nginx.conf"
 
[ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx
 
lockfile=/var/lock/subsys/nginx
 
make_dirs() {
# make required directories
user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -`
options=`$nginx -V 2>&1 | grep 'configure arguments:'`
for opt in $options; do
if [ `echo $opt | grep '.*-temp-path'` ]; then
value=`echo $opt | cut -d "=" -f 2`
if [ ! -d "$value" ]; then
# echo "creating" $value
mkdir -p $value && chown -R $user $value
fi
fi
done
}
 
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
make_dirs
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 -QUIT
retval=$?
echo
[ $retval -eq 0 ] && rm -f $lockfile
return $retval
}
 
restart() {
configtest || return $?
stop
sleep 1
start
}
 
reload() {
configtest || return $?
echo -n $"Reloading $prog: "
killproc $nginx -HUP
RETVAL=$?
echo
}
 
force_reload() {
restart
}
 
configtest() {
$nginx -t -c $NGINX_CONF_FILE
}
 
rh_status() {
status $prog
}
 
rh_status_q() {
rh_status >/dev/null 2>&1
}
 
case "$1" in
start)
rh_status_q && exit 0
$1
;;
stop)
rh_status_q || exit 0
$1
;;
restart|configtest)
$1
;;
reload)
rh_status_q || exit 7
$1
;;
force-reload)
force_reload
;;
status)
rh_status
;;
condrestart|try-restart)
rh_status_q || exit 0
;;
*)
echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}"
exit 2
esac

增加权限并加入服务列表

1
2
3
[root@node1 nginx-1.4.1]# chmod +x /etc/init.d/nginx
[root@node1 nginx-1.4.1]# chkconfig --add nginx
[root@node1 nginx]# service nginx start

保证nginx是能正常访问

图像 6

修改配置文件,让请求其连接都代理至后端tomcat服务器、并定义使用缓存来缓存静态内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
proxy_cache_path /nginx/cache levels=1:2 keys_zone=first:10m inactive=24h max_size=1g;
upstream backend {
server 192.168.0.1 weight=1;
}
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
index index.jsp index.html;
#access_log logs/host.access.log main;
location ~* "\.(jsp|do)$" {
proxy_pass http://backend;
}
location / {
root html;
rewrite / http://172.16.7.6/index.jsp last;
}
location ~* "\.(jpg|jpeg|png|gif|html|css)$" {
proxy_pass http://backend;
proxy_cache first;
proxy_cache_valid 200 24h;
proxy_cache_valid 302 10m;
add_header X-Cache-Status $upstream_cache_status;
}

nginx.conf内只需要修改上面的内容即可,其详细解释如下图

图像 10

通过以上配置我们实现了nginx反向代理tomcat的架构!测试如下:

图像 15

至此我们的nginx实现tomcat的反向代理已经搭建成功,并且实现了动态网页代理到后端tomcat服务器上,静态网页缓存到本地!

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款