Tomcat系列之服务器的基本配置及Nginx反向代理tomcat服务——(二)

来源:互联网 发布:实体店铺装修设计软件 编辑:程序博客网 时间:2024/05/29 12:30

6.部署JSP网站案例

首页,我们来修改一下配置文件,

1
2
3
4
5
[root@tomcat conf]# vim server.xml
<Host name="www.test.com" appBase="/web"
   unpackWARs="true" autoDeploy="true">
 <Context path="/" docBase="webapp" reloadable="true"/>
</Host>

注,增加一下虚拟主机,文件目录为/web/webapp。

下面我们来解压一下我们下载好的JavaCenter Home网站程序,

1
[root@tomcat src]# tar xf JavaCenter_Home_2.0_GBK.tar.bz2

接下来将解压好的JavaCenter Home程序移动到/web/webapp下,

1
2
[root@tomcat src]# cd JavaCenter_Home_2.0_GBK
[root@tomcat JavaCenter_Home_2.0_GBK]# mv * /web/webapp/

下面我们来测试一下配置文件并启动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
31
32
[root@tomcat ~]# service tomcat configtest
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr/java/jdk1.6.0_21
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Sep 23, 2013 5:31:18 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/jdk1.6.0_21/jre/lib/amd64/server:/usr/java/jdk1.6.0_21/jre/lib/amd64:/usr/java/jdk1.6.0_21/jre/../lib/amd64:/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Sep 23, 2013 5:31:20 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-80"]
Sep 23, 2013 5:31:20 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Sep 23, 2013 5:31:20 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 2493 ms
[root@tomcat ~]# service tomcat 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/jdk1.6.0_21
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State    PID/Program name
tcp    0   0 0.0.0.0:22         0.0.0.0:*          LISTEN   1026/sshd  
tcp    0   0 127.0.0.1:25        0.0.0.0:*          LISTEN   1256/master 
tcp    0   0 127.0.0.1:6010       0.0.0.0:*          LISTEN   1339/sshd  
tcp    0   0 0.0.0.0:3306        0.0.0.0:*          LISTEN   1165/mysqld 
tcp    0   0 :::80            :::*            LISTEN   1499/java  
tcp    0   0 :::22            :::*            LISTEN   1026/sshd  
tcp    0   0 ::1:25           :::*            LISTEN   1256/master 
tcp    0   0 ::1:6010          :::*            LISTEN   1339/sshd  
tcp    0   0 :::8009           :::*            LISTEN   1499/java

下面我们用浏览器访问一下,http://www.test.com(注,要想用域名访问,必须配置本机有hosts文件,Windows7hosts文件目录,C:\Windows\System32\drivers\etc\hosts)

error

注,上面的错误说明我们连接Mysql数据库失败。因为我们这里还没有安装与配置嘛。下面我们赶快来配置一下,

先安装数据库,我们这里就用yum安装一下,

1
[root@tomcat ~]# yum install -y mysql-server

下面来启动并配置mysql,

1
2
3
4
5
6
7
8
9
10
11
12
[root@tomcat ~]# service mysqld start
Starting mysqld:                      [ OK ]
[root@tomcat ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.1.69 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

好了,到这里我们mysql就安装完成了,下面我们来看一下我们的程序目录,

1
2
3
4
5
6
7
[root@tomcat ~]# cd /web/webapp/
[root@tomcat webapp]# ls
admin    avatar.jsp     data     help.jsp  js.jsp    rss.jsp  userapp.jsp
admincp.jsp config.properties do.jsp    image    link.jsp   source   WEB-INF
api     contact.jsp    editor.jsp  index.jsp  magic.jsp  space.jsp xmlrpc.jsp
app.jsp   cp.jsp       errors    install   META-INF   template
attachment  crossdomain.xml  favicon.ico invite.jsp network.jsp theme

大家可以看到里面有个install的目录,下面我们用浏览器访问一下,http://www.test.com/install,会跳出一个安装界面,如下图

jc1

从图中,我们可以看出,所以环境配置完成,都符合要求。下面我们点击“接受授权协议,开始安装JavaCenter Home”,会跳出下一界面,如下图

js2

从图中,我们可以看出得输入,数据库名称、数据库用户名、数据库密码。下面我们就来增加一下,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[root@tomcat ~]# mysql
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.1.69 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> create database jcenter;
Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on jcenter.* to jcenter@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on jcenter.* to jcenter@'127.0.0.1' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

上面创建一个jcenter数据库,授权访问一个jcenter用户,密码为123456。下面我们继续配置,

js3

我们输入刚才设置的数据库用户名、数据库密码、数据库名称。点击“设置完毕,检测我的数据库配置”,图显示的3和4会看自动进行安装,我们只等一会即可。安装完成的效果如下图,

js4

下面我们开通一个管理员空间,用户名和密码都为admin,如下图

js5

点击“开通管理员空间”,会跳出另一个界面,如下图

js6

我们点击“进入空间首页”,效果如下图

js7

好了,到这里我们的JavaCenter Home就全部配置完成了,我们第一阶段的基本配置就这里全部完成,下面我们主要讲解Nginx反向代理Tomcat服务器。

二、Nginx反向代理Tomcat服务器

0.测试环境准备阶段

下面先看一下实验拓扑,

tomcat 实验拓扑

接着来同步各节点时间,

1
2
[root@tomcat ~]# ntpdate 202.120.2.101
[root@nginx ~]# ntpdate 202.120.2.101

下面我们来安装nginx服务器,首先来解决nginx的依赖关系,

1
2
[root@nginx ~]# yum groupinstall -y "Development Tools" "Server Platform Deveopment"
[root@nginx ~]# yum install -y openssl-devel pcre-devel

下面我们来新建nginx用户,

1
2
3
4
[root@nginx ~]# groupadd -r -g 108 nginx
[root@nginx ~]# useradd -r -g 108 -u 108 nginx
[root@nginx ~]# id nginx
uid=108(nginx) gid=108(nginx) 组=108(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
[root@nginx src]# tar xf nginx-1.4.2.tar.gz
[root@nginx src]# cd nginx-1.4.2
[root@nginx nginx-1.4.2]# ls
auto CHANGES CHANGES.ru conf configure contrib html LICENSE man README src
[root@nginx nginx-1.4.2]# ./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@nginx nginx-1.4.2]# make && make install

说明:

  • Nginx可以使用Tmalloc(快速、多线程的malloc库及优秀性能分析工具)来加速内存分配,使用此功能需要事先安装gperftools,而后在编译nginx添加--with-google_perftools_module选项即可。

  • 如果想使用nginx的perl模块,可以通过为configure脚本添加--with-http_perl_module选项来实现,但目前此模块仍处于实验性使用阶段,可能会在运行中出现意外,因此,其实现方式这里不再介绍。如果想使用基于nginx的cgi功能,也可以基于FCGI来实现,具体实现方法请参照网上的文档。

下面我们为nginx提供SysV init脚本,

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
[root@nginx ~]# cat /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
[root@nginx ~]# chmod +x /etc/init.d/nginx

添加至服务管理列表,并让其开机自动启动, 

1
2
[root@nginx ~]# chkconfig --add nginx
[root@nginx ~]# chkconfig nginx on


而后就可以启动服务并测试了,

1
2
3
4
5
6
7
8
9
10
11
12
[root@nginx ~]# service nginx start
正在启动 nginx:                      [确定]
[root@nginx ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State    PID/Program name
tcp    0   0 0.0.0.0:80         0.0.0.0:*          LISTEN   14006/nginx 
tcp    0   0 0.0.0.0:22         0.0.0.0:*          LISTEN   1029/sshd  
tcp    0   0 127.0.0.1:25        0.0.0.0:*          LISTEN   1105/master 
tcp    0   0 127.0.0.1:6010       0.0.0.0:*          LISTEN   1345/sshd  
tcp    0   0 :::22            :::*            LISTEN   1029/sshd  
tcp    0   0 ::1:25           :::*            LISTEN   1105/master 
tcp    0   0 ::1:6010          :::*            LISTEN   1345/sshd

下面是测试结果,

nginx测试截图

好了,到这里我们准备工作就全部完成了,下面们来简单的配置一下Nginx反向代理Tomcat服务器。

1.Nginx将请求反向代理到后端Tomcat

首先,我们来修改一下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
[root@nginx ~]# cd /etc/nginx/
[root@nginx nginx]# cp nginx.conf nginx.conf.bak
[root@nginx nginx]# vim nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid    logs/nginx.pid;
events {
  worker_connections 1024;
}
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;
  sendfile    on;
  #tcp_nopush   on;
  #keepalive_timeout 0;
  keepalive_timeout 65;
  #gzip on;
  server {
    listen    80;
    server_name localhost;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {
      #root  html;
      #index index.html index.htm;
      proxy_pass http://192.168.18.201/; #注释默认两行,新增一行。
    }
    #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;
    }
  }
}

重新加载一下配置文件,

1
2
3
4
[root@nginx ~]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新载入 nginx:                      [确定]

下面进行测试一下,(注,首先你得保证你的tomcat服务器能正常访问,下面我们先来访问一下tomcat服务器,如下图)

t1

大家可以看到我们的tomcat服务器可以正常访问,下面我们来看测试一下nginx可不可以进行反向代理。(注,还有问题,这里为了方便测试我们先将tomcat的默认主机设置为www.test.com

1
2
[root@tomcat ~]# vim /usr/local/tomcat/conf/server.xml
  <Engine name="Catalina" defaultHost="www.test.com">

重新启动一下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
[root@tomcat ~]# service tomcat stop
Using CATALINA_BASE:  /usr/local/tomcat
Using CATALINA_HOME:  /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:    /usr/java/jdk1.6.0_21
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# service tomcat 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/jdk1.6.0_21
Using CLASSPATH:    /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
[root@tomcat ~]# netstat -ntulp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address        Foreign Address       State    PID/Program name
tcp    0   0 0.0.0.0:22         0.0.0.0:*          LISTEN   1026/sshd  
tcp    0   0 127.0.0.1:25        0.0.0.0:*          LISTEN   1256/master 
tcp    0   0 127.0.0.1:6010       0.0.0.0:*          LISTEN   1339/sshd  
tcp    0   0 127.0.0.1:6011       0.0.0.0:*          LISTEN   2744/sshd  
tcp    0   0 0.0.0.0:3306        0.0.0.0:*          LISTEN   2382/mysqld 
tcp    0   0 :::80            :::*            LISTEN   3299/java  
tcp    0   0 :::22            :::*            LISTEN   1026/sshd  
tcp    0   0 ::1:25           :::*            LISTEN   1256/master 
tcp    0   0 ::1:6010          :::*            LISTEN   1339/sshd  
tcp    0   0 ::1:6011          :::*            LISTEN   2744/sshd  
tcp    0   0 :::8009           :::*            LISTEN   3299/java

下面我们来访问测试一下tomcat服务器,

t2

下面我们来测试看nginx是否能代理tomcat服务器,

t3

好了,大家可以看到我们成功设置了nginx反向代理tomcat服务器。好了,大家可以看到,我们网站上有很多的图片,每次访问都要去后端的tomcat服务器上去取,很消耗服务器资源。我们下面将设置在nginx服务器上缓存图片。

2.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
[root@nginx nginx]# cat nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid    logs/nginx.pid;
events {
  worker_connections 1024;
}
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;
  sendfile    on;
  #tcp_nopush   on;
  #keepalive_timeout 0;
  keepalive_timeout 65;
  #gzip on;
  proxy_cache_path /nginx/cache levels=1:2 keys_zone=first:10m inactive=24h max_size=1G; #新建缓存路径与相关属性
  upstream backend { #建立后端tomcat服务器
  server 192.168.18.201 weight=1;
  }
  server {
    listen    80;
    server_name localhost;
    #charset koi8-r;
    #access_log logs/host.access.log main;
    location / {
      #root  html;
      #index index.html index.htm;
      #proxy_pass http://192.168.18.201/; #注释原来的代理设置
      proxy_pass http://backend/#启动后端服务器
    }
  location ~* "\.(jpg|jpeg|png|gif|html|css|js)$" #缓存图片与静态内容
    proxy_pass http://backend;
    proxy_cache first;
    proxy_cache_valid 200 24h; #200状态缓存24小时
    proxy_cache_valid 302 10m; #302状态缓存10分钟
    add_header X-Cache-Status $upstream_cache_status; #在http头部增加一个字段显示是否命令缓存
  }
    #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;
    }
  }
}

下面我们来新建缓存目录,

1
2
3
[root@nginx ~]# mkdir -pv /nginx/cache
mkdir: 已创建目录 "/nginx"
mkdir: 已创建目录 "/nginx/cache"

测试一下配置文件是否有错,

1
2
3
[root@nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重新加载配置文件,

1
2
3
4
[root@nginx ~]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新载入 nginx:                      [确定]

那么下面我们就来测试一下,

t4

大家可以看到我们访问的所有的静态内容都是命中的,X-Cache-Status: HIT,下面们来看一下缓存的目录,

1
2
3
[root@nginx ~]# cd /nginx/cache/
[root@nginx cache]# ls
0 1 2 3 4 5 6 7 8 9 b c d e

大家可以看到,缓存目录当中有我们缓存的内容,好了到这里我们的nginx缓存服务就配置完成了,下面我们看一下如何实现动静分离。

3.Nginx将请求实现动静分离

首先,我们来说一下我们要实现的效果,上面我们已经将静态内容缓存在nginx服务器上,我们想让用户请求的静态内容到nginx去取,动态内容到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
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
[root@nginx nginx]# cat nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid    logs/nginx.pid;
events {
  worker_connections 1024;
}
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;
  sendfile    on;
  #tcp_nopush   on;
  #keepalive_timeout 0;
  keepalive_timeout 65;
  #gzip on;
  proxy_cache_path /nginx/cache levels=1:2 keys_zone=first:10m inactive=24h max_size=1G;
  upstream backend {
  server 192.168.18.201 weight=1;
  }
  server {
    listen    80;
    server_name localhost;
    #charset koi8-r;
    #access_log logs/host.access.log main;
  index index.jsp index.html;
    location ~* "\.(jsp|do)$" #当请求的是jsp或do文件时直接到tomcat上去取
      #root  html;
      #index index.html index.htm;
      #proxy_pass http://192.168.18.201/;
      #proxy_pass http://backend/;
      proxy_pass http://backend;
    }
  location = / {
    root html;
    rewrite ^/ http://192.168.18.201/index.jsp last;
  }
  location ~* "\.(jpg|jpeg|png|gif|html|css|js)$" {
    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;
  }
    #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;
    }
  }
}

下面我们来检查一下配置文件是否有误,

1
2
3
[root@nginx ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重新加载一下配置文件,

1
2
3
4
[root@nginx ~]# service nginx reload
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
重新载入 nginx:                      [确定]

下面我们来访问测试一下,

t5

大家可以看到我们的静态内容来自缓存,动态内容全部代理到后端的tomcat服务器上了,说明我们动态分离配置完成,好了到这里我们的tomcat的基本配置与nginx反向代理tomcat的配置就全部完成了,最后希望大家有所收获^_^……


本文出自 “Share your knowledge …” 博客,请务必保留此出处http://freeloda.blog.51cto.com/2033581/1300915

原创粉丝点击