Nginx+Tomcat构建动、静分离WEB架构

一、简介

二、环境介绍

三、后端服务器安装配置

四、安装论坛

五、安装配置前端Nginx服务器

六、验证服务


一、Tomcat简介

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。由于有了Sun 的参与和支持,最新的Servlet 和JSP 规范总是能在Tomcat 中得到体现,Tomcat 5 支持最新的Servlet 2.4 和JSP 2.0 规范。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。目前最新版本是8.0

Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,可以这样认为,当在一台机器上配置好Apache 服务器,可利用它响应对HTML 页面的访问请求。实际上Tomcat 部分是Apache 服务器的扩展,但它是独立运行的,所以当你运行tomcat 时,它实际上作为一个与Apache 独立的进程单独运行的

当配置正确时,Apache 为HTML页面服务,而Tomcat 实际上运行JSP 页面和Servlet。另外,Tomcat和IIS、Apache等Web服务器一样,具有处理HTML页面的功能,另外它还是一个Servlet和JSP容器,独立的Servlet容器是Tomcat的默认模式。不过,Tomcat处理静态HTML的能力不如Apache服务器。目前Tomcat最新版本为8.0.0-RC3 (alpha) Released

Tomcat 很受广大程序员的喜欢,因为它运行时占用的系统资源小,扩展性好,支持负载平衡与邮件服务等开发应用系统常用的功能;而且它还在不断的改进和完善中,任何一个感兴趣的程序员都可以更改它或在其中加入新的功能

Tomcat最初是由Sun的软件构架师詹姆斯·邓肯·戴维森开发的。后来他帮助将其变为开源项目,并由Sun贡献给Apache软件基金会。由于大部分开源项目O'Reilly都会出一本相关的书,并且将其封面设计成某个动物的素描,因此他希望将此项目以一个动物的名字命名。因为他希望这种动物能够自己照顾自己,最终,他将其命名为Tomcat(英语公猫或其他雄性猫科动物)。而O'Reilly出版的介绍Tomcat的书籍(ISBN 0-596-00318-8)[1]的封面也被设计成了一个公猫的形象。而Tomcat的Logo兼吉祥物也被设计成了一只公猫


二、环境介绍

Nginx+Tomcat构建动、静分离WEB架构

系统版本: CentOS 6.4_x86_64

Nginx版本: nginx-1.4.2 点此下载

JDK版本: jdk-7u40-linux-x64 点此下载

Tomcat版本:apache-tomcat-7.0.42 点此下载

论坛版本: JspRun!_6.0.0_GBK 点此下载

环境介绍:

当用户访问时"www.allen.com"网站时,通过Nginx服务器代理,将请求的静态内容(如:.html|.jpg等)让web服务器来响应,而请求的动态内容都让java服务器来响应;这样就实现了动、静的分离


三、后端服务器的安装与配置

1、安装java服务器

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
######1、安装JDK并设置环境变量
[root@java ~]# rpm -ihv jdk-7u40-linux-x64.rpm
[root@java ~]# vim /etc/profile.d/java.sh
export JAVA_HOME=/usr/java/latest
export PATH=$JAVA_HOME/bin:$PATH
[root@java ~]# . /etc/profile.d/java.sh
######验证JDK是否安装成功
[root@java ~]# java -version
java version "1.7.0_40"
Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
Java HotSpot(TM) 64-Bit Server VM (build 24.0-b56, mixed mode)
========================================================================
######2、安装Tomcat
[root@java ~]# tar xf apache-tomcat-7.0.42.tar.gz -C /usr/local/
[root@java ~]# cd /usr/local/
[root@java local]# ln -s apache-tomcat-7.0.42 tomcat
[root@java local]# vim /etc/profile.d/tomcat.sh
export CATALINA_HOME=/usr/local/tomcat
export PATH=$CATALINA_HOME/bin:$PATH
[root@java local]# . /etc/profile.d/tomcat.sh
######验证Tomcat是否安装成功
[root@java local]# catalina.sh version
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Server version: Apache Tomcat/7.0.42
Server built:   Jul 2 2013 08:57:41
Server number:  7.0.42.0
OS Name:        Linux
OS Version:     2.6.32-358.el6.x86_64
Architecture:   amd64
JVM Version:    1.7.0_40-b43
JVM Vendor:     Oracle Corporation
----------------------------------------------------------------------
[root@java ~]# catalina.sh start

2、访问测试Java服务器

1
2
3
######查看监听端口,Tomcat默认监听8080端口
[root@java ~]# ss -tanlp | grep 8080
LISTEN     0      100                      :::8080                    :::*      users:(("java",6163,40))

 

Nginx+Tomcat构建动、静分离WEB架构

3、安装web服务器

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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
######安装nginx依赖环境
[root@web ~]# yum -y install pcre-devel gcc gcc-c++ openssl-devel
[root@web ~]# useradd -r nginx
[root@web ~]# tar xf nginx-1.4.2.tar.gz
[root@web ~]# cd nginx-1.4.2
[root@web nginx-1.4.2]# ./configure \
   --prefix=/usr/local/nginx \
   --sbin-path=/usr/local/nginx/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@web nginx-1.4.2]# make && make install
======================================================================
######为Nginx提供Sysv服务脚本
[root@web ~]# 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/local/nginx/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
----------------------------------------------------------------------
[root@web ~]# chmod +x /etc/init.d/nginx
[root@web ~]# chkconfig --add nginx
[root@web ~]# service nginx start
[root@web ~]# chkconfig nginx on

4、访问测试web服务器

1
2
[root@web nginx-1.4.2]# ss -tanlp | grep 80
LISTEN     0      128      *:80       *:*      users:(("nginx",30503,6),("nginx",30504,6))
Nginx+Tomcat构建动、静分离WEB架构

5、安装Mysql服务器

1
2
3
4
5
6
7
root@mysql ~]# yum -y install mysql-server
root@mysql ~]# service mysqld start
[root@mysql ~]# mysql
mysql> create database bbs;
mysql> grant all on bbs.* to 'bbsuser'@'172.16.%.%' identified by 'bbspass';
mysql> flush privileges;
######注释:创建一个数据库并授权用户,方便后面安装论坛使用到

6、测试授权用户是否能使用"172.16.0.0"网段的主机连接,远程主机请确定安装了mysql客户端

1
2
3
4
5
6
7
8
9
[root@java ~]# mysql -ubbsuser -pbbspass -h 172.16.14.4 -e 'show databases';
+--------------------+
| Database           |
+--------------------+
| information_schema |
| bbs                |
test               |
+--------------------+
######可以远程查询到数据说明已连接成功

四、安装论坛

1、在java服务器上安装论坛程序

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
######创建网站存放目录
[root@java ~]# mkdir /web
[root@java ~]# unzip JspRun\!_6.0.0_GBK.zip
[root@java ~]# mv upload /web/bbs
------------------------------------------------------------------------
######修改Tomcat的默认访问端口8080为80
    <Connector port="80" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" />
######修改tomcat主配置文件添加虚拟主机
    <Engine name="Catalina" defaultHost="www.allen.com">
      <Host name="www.allen.com"  appBase="/web/bbs"
            unpackWARs="true" autoDeploy="true">
        <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
               prefix="bbs_access_log." suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
        <Context path="" docBase="/web/bbs" />
      </Host>
注释:将"<Host>"标记的段添加到"Engine"中,而"Engine"中的默认主机改为定义的主机名称;
name:主机名称
defaultHost:默认访问的主机名称
appBase:网站目录
unpackWARS:是否自动解压"war"格式的包
autoDeploy:是否自动部署应用程序
className:类名称
prefix:日志名称
suffix:日志后缀名
pattern:日志格式
"Context"标记中path:虚拟目录名称,docBase:虚拟目录路径
======================================================================
######修改论坛配置文件,指定数据库主机,数据用户名密码及数据库名称
[root@java conf]# cd /web/bbs/
[root@java bbs]# vim config.properties
dbhost = 172.16.14.4
dbport=3306
dbuser = bbsuser
dbpw = bbspass
dbname = bbs
======================================================================
######启动Tomcat服务并查看端口
[root@java ~]# catalina.sh start
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr/java/latest
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@java ~]# ss -tanlp | grep 80
LISTEN     0      100      :::80    :::*      users:(("java",7493,40))
LISTEN     0      1        ::ffff:127.0.0.1:8005   :::*      users:(("java",7493,53))
LISTEN     0      100      :::8009  :::*      users:(("java",7493,41))

2、安装论坛程序

Nginx+Tomcat构建动、静分离WEB架构

3、点击下一步-->我同意-->下一步,进入数据库配置页面

Nginx+Tomcat构建动、静分离WEB架构

4、点下一步-->(填写管理员密码)下一步-->进入创建数据表阶段-->然后点Nginx+Tomcat构建动、静分离WEB架构-->进入首页

Nginx+Tomcat构建动、静分离WEB架构

5、将论坛程序拷贝到web服务器上面一份,因为WEB服务器要解析静态文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
######创建网站存放目录
[root@web ~]# mkdir /web
[root@web ~]# scp -r 172.16.14.3:/web/bbs /web/
######修改Nginx主配置文件
[root@web ~]# vim /etc/nginx/nginx.conf
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
        access_log  logs/host.access.log  main;
        location / {
            root   /web/bbs;
            index  index.html index.htm;
######注释:开启访问日志功能;修改虚拟主机指定网站网站目录为"/web/bbs"
----------------------------------------------------------------------
######创建一个静态测试页方便后面测试
[root@web ~]# echo '<h1>WEB:172.16.14.2</h1>' > /web/bbs/index.html
[root@web ~]# mkdir /usr/local/nginx/logs  #创建日志存放目录
[root@web ~]# service nginx restart

五、安装配置Nginx前端服务器

1、安装方法与第三部分的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
[root@nginx ~]# vim /etc/nginx/nginx.conf
user  nginx nginx;
worker_processes  2;
worker_rlimit_nofile 51200;
#error_log  logs/error.log;
#pid        logs/nginx.pid;
events {
    use epoll;
    worker_connections  51200;
}
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"';
    access_log  logs/access.log  main;
    client_max_body_size 20m;
    client_header_buffer_size 16k;
    large_client_header_buffers 4 16k;
    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;
    gzip  on;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_proxied   any;
    gzip_http_version 1.1;
    gzip_comp_level 3;
    gzip_types text/plain application/x-javascript text/css application/xml;
    gzip_vary on;
    proxy_temp_path   /tmp/proxy_temp;
    proxy_cache_path  /tmp/proxy_cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=3g;
    proxy_connect_timeout    50;
    proxy_read_timeout       600;
    proxy_send_timeout       600;
    proxy_buffer_size        128k;
    proxy_buffers           16 256k;
    proxy_busy_buffers_size 512k;
    proxy_temp_file_write_size 1024m;
    proxy_next_upstream error timeout invalid_header http_500 http_503 http_404 http_502 http_504;
    server {
        listen       80;
        server_name  www.allen.com;
        access_log  logs/host.access.log  main;
        location / {
        proxy_pass http://172.16.14.3;
        }
    location ~* \.(|html|htm|jpg|jpeg|png|gif|bmp|swf|ico)$ {
        proxy_pass http://172.16.14.2;
    }
    location ~* \.(jsp|css|do)$ {
        proxy_pass http://172.16.14.3;
    }
        #error_page  404              /404.html;
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
======================================================================
[root@nginx ~]# service nginx start

2、修改Nginx主配置文件


六、验证服务

1、在客户端使用"www.allen.com"域名访问测试,注意:由于没有"DNS"服务器解析需要修改hosts文件来解析

1
2
3
######添加如下内容解析
C:\Windows\System32\drivers\etc\hosts
172.16.14.1    www.allen.com

 

Nginx+Tomcat构建动、静分离WEB架构
2、访问静态测试页
Nginx+Tomcat构建动、静分离WEB架构
 

2、查看web服务器上nginx访问日志

1
2
3
4
5
6
7
8
[root@web ~]# tail -5 /usr/local/nginx/logs/host.access.log
172.16.14.1 - - [23/Sep/2013:22:38:39 +0800] "GET /images/common/softkeyboard.gif HTTP/1.0" 304 0 "http://www.allen.com/include/css/keyboard.css" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0" "-"
172.16.14.1 - - [23/Sep/2013:22:38:39 +0800] "GET /images/default/link.gif HTTP/1.0" 304 0"http://www.allen.com/forumdata/cache/style_1.css" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0" "-"
172.16.14.1 - - [23/Sep/2013:23:04:02 +0800] "GET /index.html HTTP/1.0" 304 0 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0" "-"
172.16.14.1 - - [23/Sep/2013:22:38:39 +0800] "GET /images/default/forumbox_head.gif HTTP/1.0"304 0 "http://www.allen.com/forumdata/cache/style_1.css" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0" "-"
172.16.14.1 - - [23/Sep/2013:23:04:01 +0800] "GET /index.html HTTP/1.0" 200 25 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0" "-"
----------------------------------------------------------------------
######由上可见,访问"www.allen.com"域名时,所有静态内容都转发到web服务器上;如图片

3、查看java服务器上Tomcat访问日志

1
2
3
4
5
6
7
8
[root@java ~]# tail -5 /usr/local/tomcat/logs/bbs_access_log.2013-09-14.txt
172.16.14.1 - - [14/Sep/2013:23:26:57 +0800] "GET /forumdata/cache/style_1.css HTTP/1.0" 304 -
172.16.14.1 - - [14/Sep/2013:23:26:57 +0800] "GET /forumdata/cache/style_1_append.css HTTP/1.0"304 -
172.16.14.1 - - [14/Sep/2013:23:27:02 +0800] "GET /member.jsp?action=list HTTP/1.0" 200 6839
172.16.14.1 - - [14/Sep/2013:23:27:02 +0800] "GET /forumdata/cache/style_1_append.css HTTP/1.0"304 -
172.16.14.1 - - [14/Sep/2013:23:27:02 +0800] "GET /forumdata/cache/style_1.css HTTP/1.0" 304 -
------------------------------------------------------------------------
######由上可见,访问的动态内容都转发到Java服务器来解析;如jsp结尾的文件

到此,Nginx+Tomcat实现WEB网站的动、静分离已经完成;后面将会更新Apache+Tomcat如何实现Apache代理Tomcat及如何实现会话保存...

上一篇:数据市中心全省中国mysql脚本


下一篇:Operating System Concepts 项目:生产者-消费者问题 线程