Nginx+Tomcat的服務器端環境配置詳解

Nginx+Tomcat的服務器端環境配置詳解

這篇文章主要介紹了Nginx+Tomcat的服務器端環境配置詳解,包括Nginx與Tomcat的監控開啓方法,需要的朋友可以參考下

Nginx+tomcat是目前主流的java web架構,如何讓nginx+tomcat同時工作呢,也可以說如何使用nginx來反向代理tomcat後端均衡呢?直接安裝配置如下:

1、JAVA JDK安裝:

#下載相應的jdk軟件包,然後解壓安裝,我這裏包名稱爲:jdk-7u25-linux-x64.tar.gz   
    

?
1
tar -xzf jdk-7u25-linux-x64.tar.gz ;mkdir -p /usr/java/ ;mv jdk1.7.0_25/ /usr/java/ 下.

    
#然後配置環境變量,這樣可以任何地方引用jdk,如下配置:   
    
#vi /etc/profile 最後面加入以下語句:   
    

?
1
2
3
4
5
export JAVA_HOME=/usr/java/jdk1.7.0_25
  
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
  
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin

    
#source /etc/profile   #使環境變量馬上生效   
    
#java  --version    #查看java版本,看到jdk1.7.0_25版本即代表java jdk安裝成功。 
2、Nginx安裝:

?
1
2
3
4
5
6
7
8
9
10
11
12
wget http://nginx.org/download/nginx-1.2.6.tar.gz
  
useradd www
  
tar zxvf nginx-1.2.6.tar.gz
  
cd nginx-1.2.6/
  
./configure --user=www --group=www --prefix=/usr/local/nginx
 \--with-http_stub_status_module --with-http_ssl_module
  
make && make install

    
#Nginx安裝完畢,然後使用命令:/usr/local/nginx/sbin/nginx -t 測試OK,代表nginx安裝成功。   
    
/usr/local/nginx/sbin/nginx 回車啓動nginx,可以通過訪問http://ip/看到nginx默認頁面。 
3、Tomcat安裝:

#官方網站下載tomcat 6.0.30或者其他版本:   
    

?
1
cd /usr/src && tar xzf apache-tomcat-6.0.30.tar.gz

    
#直接解壓就可以使用,解壓完成執行,同時拷貝兩個tomcat,命名爲tomcat1 tomcat2   
    

?
1
2
3
mv apache-tomcat-6.0.30 /usr/local/tomcat1
  
cp /usr/local/tomcat1 /usr/local/tomcat2 -r

    
#分別修改tomcat1和tomcat2 端口,這裏有三個端口需要修改,分別如下:   
    
shutdown 端口:8005  主要負責啓動關閉.   
    
ajp端口:8009 主要負責通過ajp均衡(常用於apache和tomcat整合)   
    
http端口:8080 可以通過web頁面直接訪問(nginx+tomcata整合)   
    
#注* 如果tomcat1三個端口分別爲:8005 8009 8080 ,那麼tomcat2端口在此基礎上都+1,即爲:8006 8010 8081   
    
#一臺服務器上,端口不能重複,否則會報錯。   
    
#修改完端口後,然後啓動兩個tomcat,啓動命令爲:   
    
#如何提示沒有這個文件或者權限不足,需要tomcat 的bin目錄對sh文件賦予執行權限:chmod o+x   *.sh   
    

?
1
2
3
/usr/local/tomcat1/bin/startup.sh
  
/usr/local/tomcat2/bin/startup.sh

    
#啓動後,使用netstat -tnl 可以看到6個端口,即代表tomcat1 tomcat2成功啓動。你可以使用http://ip:8080  http://ip:8081訪問tomcat默認頁面。  
#如果需要修改tomcat發佈目錄爲自己制定的目錄,需要做如下調整,創建兩個發佈目錄:

?
1
mkdir -p /usr/webapps/{www1,www2}

編輯vi /usr/local/tomcat1/conf/server.xml 在最後</Host>前一行加下內容:

?
1
<Context path="" docBase="/usr/webapps/www1" reloadable="false"/>

編輯vi /usr/local/tomcat2/conf/server.xml 在最後</Host>前一行加下內容:

?
1
<Context path="" docBase="/usr/webapps/www2" reloadable="false"/>

tomcat1發佈目錄內容:

?
1
2
3
4
5
6
<html>
<body>
<h1>TOMCAT_1 JSP Test Page</h1>
<%=new java.util.Date()%>
</body>
</html>

tomcat2發佈目錄內容:

?
1
2
3
4
5
6
<html>
<body>
<h1>TOMCAT_2 JSP Test Page</h1>
<%=new java.util.Date()%>
</body>
</html>

然後訪問http://ip:8080、8081查看測試內容。

4、Nginx+tomcat整合:

整合主要是修改nginx.conf配置,給一個完整的nginx.conf線上配置,部分參數可以自己根據實際需求修改:

?
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
user www www;
worker_processes 8;
pid /usr/local/nginx/nginx.pid;
worker_rlimit_nofile 102400;
events
{
use epoll;
worker_connections 102400;
}
http
{
 include  mime.types;
 default_type application/octet-stream;
 fastcgi_intercept_errors on;
 charset utf-8;
 server_names_hash_bucket_size 128;
 client_header_buffer_size 4k;
 large_client_header_buffers 4 32k;
 client_max_body_size 300m;
 sendfile on;
 tcp_nopush  on;
   
 keepalive_timeout 60;
   
 tcp_nodelay on;
 client_body_buffer_size 512k;
  
 proxy_connect_timeout 5;
 proxy_read_timeout  60;
 proxy_send_timeout  5;
 proxy_buffer_size  16k;
 proxy_buffers   4 64k;
 proxy_busy_buffers_size 128k;
 proxy_temp_file_write_size 128k;
   
 gzip on;
 gzip_min_length 1k;
 gzip_buffers  4 16k;
 gzip_http_version 1.1;
 gzip_comp_level 2;
 gzip_types  text/plain application/x-javascript text/css application/xml;
 gzip_vary on;
   
###2012-12-19 change nginx logs
log_format main '$http_x_forwarded_for - $remote_user [$time_local] "$request" '
    '$status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" $request_time $remote_addr';
      
upstream web_app {
 server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
 server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
}
  
####chinaapp.sinaapp.com
server {
 listen 80;
 server_name chinaapp.sinaapp.com;
 index index.jsp index.html index.htm;
 #發佈目錄/data/www
 root /data/www;
   
 location /
 {
 proxy_next_upstream http_502 http_504 error timeout invalid_header;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_pass http://web_app;
 expires  3d;
 }
   
 }
  
}

#注* server段 proxy_pass定義的web_app需要跟upstream 裏面定義的web_app一致,否則server找不到均衡。   

#如上配置,nginx+tomcat反向代理負載均衡配置完畢,如果要做動靜分離,只需要在nginx添加如下配置就OK了。

 #配置Nginx動靜分離   
   

?
1
2
3
4
5
6
7
8
9
10
11
location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$
  
{
  
root /data/www;
  
#expires定義用戶瀏覽器緩存的時間爲3天,如果靜態頁面不常更新,可以設置更長,這樣可以節省帶寬和緩解服務器的壓力
  
expires  3d;
  
}

5、開啓nginx的監控
1)、nginx簡單狀態監控

在nginx.conf中添加如下代碼即可監控nginx當前的狀態,然後訪問http://serverip/status即可訪問

?
1
2
3
4
location /status {
stub_status on;
access_log off;
}

一般顯示爲

?
1
2
3
4
Active connections: 16
server accepts handled requests
191226 191226 305915
Reading: 0 Writing: 1 Waiting: 15

ctive connections: 對後端發起的活動連接數.

Server accepts handled requests: Nginx總共處理了24個連接,成功創建24次握手(證明中間沒有失敗的),總共處理了129個請求.

Reading: Nginx 讀取到客戶端的Header信息數.

Writing: Nginx 返回給客戶端的Header信息數.

Waiting: 開啓keep-alive的情況下,這個值等於 active – (reading + writing),意思就是Nginx已經處理完成,正在等候下一次請求指令的駐留連接.
注意的,本模塊默認是不會編譯進Nginx的,如果你要使用該模塊,則要在編譯安裝Nginx時指定:

?
1
./configure –with-http_stub_status_module

 查看已安裝的 Nginx 是否包含 stub_status 模塊

?
1
#/usr/local/nginx/sbin/nginx -V
?
1
2
TLS SNI support disabled
configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-file-aio --with-http_ssl_module

 
2)、nginx的圖形化監控-nginx-RRD stats
 
nginx-rrd是nginx官方推薦的一款Nginx監控工具,利用nginx-rrd可以很方便的生成圖表,便於我們查看。
a、運行環境(centos):

在安裝前需要安裝好rrdtool這個畫圖工具和相應的perl模塊,可以先運行:

?
1
yum install rrdtool libhtml-parser-perl libwww-perl librrds-perl librrd2-dev

確保rrdtool和相應的perl被安裝上。

b、安裝配置

下載:

?
1
wget http://soft.vpser.net/status/nginx-rrd/nginx-rrd-0.1.4.tgz

解壓:

?
1
tar zxvf nginx-rrd-0.1.4.tgz

進入nginx-rrd目錄,

?
1
cd nginx-rrd-0.1.4/

複製主程序:

?
1
cp usr/sbin/* /usr/sbin

複製配置文件

?
1
cp etc/nginx-rrd.conf /etc

複製定時執行文件:

?
1
cp etc/cron.d/nginx-rrd.cron /etc/cron.d

創建nginx-rrd生成目錄:

?
1
2
3
mkdir /home/wwwroot/nginx && mkdir /home/wwwroot/nginx/rrd
 
cp html/index.php /home/wwwroot/nginx

編輯/home/wwwroot/nginx/index.php修改登錄密碼

?
1
2
3
4
5
6
<?php
header("Content-Type: text/html; charset=utf-8");
 
$password = "admin";
 
.........

編輯配置文件nginx-rrd.conf,修改完成後如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
#####################################################
#
# dir where rrd databases are stored
RRD_DIR="/home/wwwroot/nginx-rrd/";
# dir where png images are presented
WWW_DIR="/home/wwwroot/nginx/";
# process nice level
NICE_LEVEL="-19";
# bin dir
BIN_DIR="/usr/sbin";
# servers to test
# server_utl;server_name

多個server,可以SERVERS_URL中空格分開,前部分爲nginx_status的地址,後面爲被監控主機的域名。

SEVERS_URL 格式

注意,nginx.conf虛擬主機server{}中,需要已經加入:

?
1
2
3
4
location /status {
stub_status on;
access_log off;
}

以上設置就完成,可以自行運行一下:/usr/sbin/nginx-collect ,啓動收集程序。cron會15分鐘生成一次數據。

如果沒有定時執行生成數據,可以在/etc/crontab最後面加上:

?
1
2
* * * * * root /usr/sbin/nginx-collect
*/15 * * * * root /usr/sbin/nginx-graph

然後輸入然後訪問http://serverip/nginx/即可訪問。

20151228100048099.png (949×649)

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