Nginx 负载均衡: 由理论到实践
Nginx负载均衡的应用很广,很多场景下都在使用这种架构。
环境如下:
1
2
3
|
192.168.1.100
(
master
node
)
server1
192.168.1.109
(
slave
)
server2
192.168.1.106
(
slave
)
server3
|
安装nginx
在上面三台服务器上,分别安装nginx
1
|
yum
install
nginx
|
配置nginx
在上面三台服务器上,分别配置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
|
user
nginx
;
worker
_processes
1
;
error_log
/
var
/
log
/
nginx_error
.
log
crit
;
worker_rlimit
_nofile
8192
;
events
{
worker
_connections
1024
;
# you might need to increase this setting for busy servers
use
epoll
;
# Linux kernels 2.6.x change to epoll
}
http
{
server_names_hash_max
_size
2048
;
server_names_hash_bucket
_size
512
;
server_tokens
off
;
include
mime
.
types
;
default_type
application
/
octet
-
stream
;
sendfile
on
;
tcp_nopush
on
;
tcp_nodelay
on
;
keepalive
_timeout
10
;
# Gzip on
gzip
on
;
gzip_min
_length
1100
;
gzip
_buffers
4
32k
;
gzip_types
text
/
plain
application
/
x
-
javascript
text
/
xml
text
/
css
;
ignore_invalid_headers
on
;
client_max_body
_size
8m
;
client_header
_timeout
3m
;
client_body
_timeout
3m
;
send
_timeout
3m
;
connection_pool
_size
256
;
client_header_buffer
_size
4k
;
large_client_header
_buffers
4
64k
;
request_pool
_size
4k
;
output
_buffers
4
32k
;
postpone
_output
1460
;
# Cache most accessed static files
open_file_cache
max
=
10000
inactive
=
10m
;
open_file_cache
_valid
2m
;
open_file_cache_min
_uses
1
;
open_file_cache_errors
on
;
# Include each virtual host
include
"/etc/nginx/conf.d/*.conf"
;
}
|
配置虚拟主机
在server2和server3上
1
|
nano
-
w
/
etc
/
nginx
/
conf
.
d
/
mysite
.
com
.
conf
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
server
{
access_log
off
;
error_log
/
var
/
log
/
yoursite
.
com
-
error
.
log
;
listen
80
;
server_name
yoursite
.
com
www
.
yoursite
.
com
;
location
~
*
.
(
gif
|
jpg
|
jpeg
|
png
|
ico
|
wmv
|
3gp
|
avi
|
mpg
|
mpeg
|
mp4
|
flv
|
mp3
|
mid
|
js
|
css
|
wml
|
swf
)
$
{
root
/
var
/
www
/
yoursite
.
com
;
expires
max
;
add_header
Pragma
public
;
add_header
Cache
-
Control
"public, must-revalidate, proxy-revalidate"
;
}
location
/
{
root
/
var
/
www
/
yoursite
.
com
;
index
index
.
php
index
.
html
index
.
htm
;
}
}
|
在server1上
1
|
nano
-
w
/
etc
/
nginx
/
conf
.
d
/
balancer
.
com
.
conf
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
upstream
balancer
{
server
192.168.1.100
:
80
;
server
192.168.1.106
:
80
;
}
server
{
listen
192.168.1.100
:
80
;
server_name
yoursite
.
com
;
error_log
/
var
/
log
/
yoursite
.
com
-
error
.
log
;
location
/
{
proxy_pass
http
:
//balancer;
}
}
|
重启nginx
1
|
service
nginx
restart
|
DNS记录
1
2
|
yoursite
.
com
IN
A
192.168.1.100
www
IN
A
192.168.1.100
|
如果仅仅是为了测试,直接绑定hosts文件。
The master server
之所以叫它master,是由于作为主的负载均衡器使用的。它也可以被用来从slave上请求服务。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
upstream
balancer
{
server
192.168.1.100
:
80
;
server
192.168.1.106
:
80
;
}
server
{
listen
192.168.1.100
:
80
;
server_name
yoursite
.
com
;
error_log
/
var
/
log
/
yoursite
.
com
-
error
.
log
;
location
/
{
proxy_pass
http
:
//balancer;
}
}
|
另一种场景,负载均衡器同时做为请求
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
|
server
{
access_log
off
;
error_log
/
var
/
log
/
yoursite
.
com
-
error
.
log
;
listen
127.0.01
:
80
;
server_name
yoursite
.
com
www
.
yoursite
.
com
;
location
~
*
.
(
gif
|
jpg
|
jpeg
|
png
|
ico
|
wmv
|
3gp
|
avi
|
mpg
|
mpeg
|
mp4
|
flv
|
mp3
|
mid
|
js
|
css
|
wml
|
swf
)
$
{
root
/
var
/
www
/
yoursite
.
com
;
expires
max
;
add_header
Pragma
public
;
add_header
Cache
-
Control
"public, must-revalidate, proxy-revalidate"
;
}
location
/
{
root
/
var
/
www
/
yoursite
.
com
;
index
index
.
php
index
.
html
index
.
htm
;
}
}
upstream
balancer
{
server
192.168.1.100
:
80
;
server
192.168.1.106
:
80
;
server
127.0.0.1
:
80
;
}
server
{
listen
192.168.1.100
:
80
;
server_name
yoursite
.
com
;
error_log
/
var
/
log
/
yoursite
.
com
-
error
.
log
;
location
/
{
proxy_pass
http
:
//balancer;
}
}
|
参考文档:
- http://nginx.org/en/docs/http/load_balancing.html
- http://wiki.nginx.org/HttpUpstreamModule
- http://wiki.nginx.org/LoadBalanceExample
收 藏
成长的对话版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!