Nginx+Tomcat 負載均衡集羣方案
該方案是我以前作過的一個項目生產應用的,目前運行良好,如何您生產使用,請先作好測試。 javascript
轉載:http://www.unixhot.com/wiki/doku.php?id=nginx_tomcat php
下載軟件包 html
1
2
3
4
5
6
7
8
9
|
"
]
[
root
@
Nginx
-
node1
src
]
# cd /usr/local/src
[
root
@
Nginx
-
node1
src
]
# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.02.tar.gz
[
root
@
Nginx
-
node1
src
]
# wget http://nginx.org/download/nginx-0.8.34.tar.gz
[
root
@
Nginx
-
node1
src
]
# wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz
[
root
@
Nginx
-
node1
src
]
# chmod +x *
[
root
@
Nginx
-
node1
src
]
# ls -l
-
rwxr
-
xr
-
x
1
root
root
241437
10
-
01
17
:
25
keepalived
-
1.1.19.tar.gz
-
rwxr
-
xr
-
x
1
root
root
621534
03
-
04
01
:
00
nginx
-
0.8.34.tar.gz
-
rwxr
-
xr
-
x
1
root
root
1247730
03
-
31
16
:
31
pcre
-
8.02.tar.gz
|
安裝Nginx
安裝Nginx依賴的包 java
1
2
3
4
|
"
]
[
root
@
Nginx
-
node1
src
]
# tar zxvf pcre-8.02.tar.gz
[
root
@
Nginx
-
node1
src
]
# cd pcre-8.02
[
root
@
Nginx
-
node1
pcre
-
8.02
]
# ./configure
[
root
@
Nginx
-
node1
pcre
-
8.02
]
# make && make install
|
安裝Nginx node
1
2
3
4
5
6
7
8
|
"
]
[
root
@
Nginx
-
node1
pcre
-
8.02
]
# cd ../
[
root
@
Nginx
-
node1
src
]
# tar zxvf nginx-0.8.34.tar.gz
[
root
@
Nginx
-
node1
src
]
# cd nginx-0.8.34
[
root
@
Nginx
-
node1
nginx
-
0.8.34
]
# ./configure --prefix=/usr/local/nginx \
&
gt
;
--
with
-
http_stub_status
_module
\
&
gt
;
--
with
-
http_ssl
_module
[
root
@
Nginx
-
node1
nginx
-
0.8.34
]
# make && make install
[
root
@
Nginx
-
node1
~
]
# vim /usr/local/nginx/conf/nginx.conf
|
Nginx 配置文件 linux
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
|
"
]
user
website
website
;
worker
_processes
4
;
error_log
logs
/
error
.log
;
pid
logs
/
nginx
.pid
;
worker_rlimit
_nofile
65535
;
events
{
use
epoll
;
worker
_connections
10240
;
}
http
{
include
mime
.types
;
default_type
application
/
octet
-
stream
;
server_names_hash_bucket
_size
128
;
client_header_buffer
_size
32k
;
large_client_header
_buffers
4
32k
;
client_max_body
_size
8m
;
sendfile
on
;
tcp_nopush
on
;
keepalive
_timeout
60
;
tcp_nodelay
on
;
gzip
on
;
gzip_min
_length
1k
;
gzip
_buffers
4
16k
;
gzip_http
_version
1.0
;
gzip_comp
_level
2
;
gzip_types
text
/
plain
application
/
x
-
javascript
text
/
css
application
/
xml
;
gzip_vary
on
;
server_tokens
off
;
upstream
web
#設置web集羣池
{
ip_hash
;
#
server
192.168.0.141
:
8080
;
server
192.168.0.142
:
8080
;
server
192.168.0.143
:
8080
;
server
192.168.0.144
:
8080
;
server
192.168.0.145
:
8080
;
server
192.168.0.146
:
8080
;
}
upstream
wap
#設置wap集羣池
{
ip_hash
;
server
192.168.0.151
:
8080
;
server
192.168.0.152
:
8080
;
server
192.168.0.153
:
8080
;
server
192.168.0.154
:
8080
;
server
192.168.0.155
:
8080
;
server
192.168.0.156
:
8080
;
}
server
{
listen
80
;
server_name
www
.
*
*
*
.com
;
location
/
{
root
html
;
index
index
.html
index
.htm
;
proxy_redirect
off
;
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
;
#注意設置在這裏
}
error
_page
500
502
503
504
/
50x.html
;
location
=
/
50x.html
{
root
html
;
}
}
server
{
listen
80
;
server_name
wap
.
*
*
*
.com
;
location
/
{
root
html
;
index
index
.html
index
.htm
;
proxy_redirect
off
;
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
:
/
/
wap
;
#注意:設置在這裏
}
error
_page
500
502
503
504
/
50x.html
;
location
=
/
50x.html
{
root
html
;
}
}
}
|
Nginx Upstream支持的分配方法
nginx的upstream目前支持5種方式的分配 nginx
*1.輪詢(默認) web
每一個請求按時間順序逐一分配到不一樣的後端服務器,若是後端服務器down掉,能自動剔除。
*2.weight (帶權重的) 算法
指定輪詢權重,weight和訪問比率成正比,用於後端服務器性能不均的狀況。
例如:
1
2
3
4
|
"
]
upstream
bakend
{
server
192.168.0.141
weight
=
10
;
server
192.168.0.142
weight
=
10
;
}
|
*3.ip_hash
每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器,能夠解決session的問題。
例如:
1
2
3
4
5
|
"
]
upstream
bakend
{
ip_hash
;
server
192.168.0.151
:
80
;
server
192.168.0.152
:
80
;
}
|
*4.fair(第三方)
按後端服務器的響應時間來分配請求,響應時間短的優先分配。
1
2
3
4
5
|
"
]
upstream
backend
{
server
server1
;
server
server2
;
fair
;
}
|
*5.url_hash(第三方)
按訪問url的hash結果來分配請求,使每一個url定向到同一個後端服務器,後端服務器爲緩存時比較有效。
例:在upstream中加入hash語句,server語句中不能寫入weight等其餘的參數,hash_method是使用的hash算法
1
2
3
4
5
6
|
"
]
upstream
backend
{
server
squid1
:
3128
;
server
squid2
:
3128
;
hash
$request_uri
;
hash_method
crc32
;
}
|
*設置說明:
每一個設備的狀態設置爲:
1.down 表示單前的server暫時不參與負載
2.weight 默認爲1.weight越大,負載的權重就越大。
3.max_fails :容許請求失敗的次數默認爲1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤
4.fail_timeout:max_fails次失敗後,暫停的時間。
5.backup: 其它全部的非backup機器down或者忙的時候,請求backup機器。因此這臺機器壓力會最輕。
nginx支持同時設置多組的負載均衡,用來給不用的server來使用。
client_body_in_file_only 設置爲On 能夠講client post過來的數據記錄到文件中用來作debug
client_body_temp_path 設置記錄文件的目錄 能夠設置最多3層目錄
location 對URL進行匹配.能夠進行重定向或者進行新的代理 負載均衡