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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM