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

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章