主机类型 | 操作系统 | IP地址 | 作用 |
宿主机 | Windows 7 | 远程3台虚拟机,进行配置,同时也作为后面测试使用的客户端 | |
虚拟机1: lb01 | CentOS 6.5 | 10.0.0.7/24 | 负载均衡服务器lb01,将请求分担到Web节点服务器中 |
虚拟机2: web01 | CentOS 6.5 | 10.0.0.9/24 | Web节点服务器web01 |
虚拟机3: web02 | CentOS 6.5 | 10.0.0.10/24 | Web节点服务器web02 |
-
web01作为节点服务器,配置它的虚拟机域名为bbs.xpleaf.org
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[root@web01 conf]
# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application
/octet-stream
;
sendfile on;
keepalive_timeout 65;
log_format main
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
;
server {
listen 80;
server_name bbs.xpleaf.org;
location / {
root html
/bbs
;
index index.html index.htm;
}
access_log logs
/access_bbs
.log main;
}
}
|
-
添加相应的站点目录和内容
1
2
3
4
|
[root@web01 conf]
# cd ../html/bbs/
[root@web01 bbs]
# echo "bbs.xpleaf.org node1 10.0.0.9">index.html
[root@web01 bbs]
# cat index.html
bbs.xpleaf.org node1 10.0.0.9
|
-
配置hosts解析
1
2
3
|
[root@web01 bbs]
# echo "127.0.0.1 bbs.xpleaf.org">>/etc/hosts
[root@web01 bbs]
# tail -1 /etc/hosts
127.0.0.1 bbs.xpleaf.org
|
-
检查配置文件与启动
1
2
3
4
|
[root@web01 bbs]
# /application/nginx/sbin/nginx -t
nginx: the configuration
file
/application/nginx-1
.6.3
//conf/nginx
.conf syntax is ok
nginx: configuration
file
/application/nginx-1
.6.3
//conf/nginx
.conf
test
is successful
[root@web01 bbs]
# /application/nginx/sbin/nginx -s reload
|
-
本机上进行测试
1
2
|
[root@web01 bbs]
# curl bbs.xpleaf.org
bbs.xpleaf.org node1 10.0.0.9
|
-
web02作为节点服务器,配置它的虚拟机域名为bbs.xpleaf.org
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[root@web02 conf]
# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application
/octet-stream
;
sendfile on;
keepalive_timeout 65;
log_format main
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
;
server {
listen 80;
server_name bbs.xpleaf.org;
location / {
root html
/bbs
;
index index.html index.htm;
}
access_log logs
/access_bbs
.log main;
}
}
|
-
添加相应的站点目录和内容
1
2
3
4
|
[root@web01 conf]
# cd ../html/bbs/
[root@web01 bbs]
# echo "bbs.xpleaf.org node2 10.0.0.10">index.html
[root@web01 bbs]
# cat index.html
bbs.xpleaf.org node2 10.0.0.10
|
-
配置hosts解析
1
2
3
|
[root@web01 bbs]
# echo "127.0.0.1 bbs.xpleaf.org">>/etc/hosts
[root@web01 bbs]
# tail -1 /etc/hosts
127.0.0.1 bbs.xpleaf.org
|
-
检查配置文件与启动
1
2
3
4
|
[root@web01 bbs]
# /application/nginx/sbin/nginx -t
nginx: the configuration
file
/application/nginx-1
.6.3
//conf/nginx
.conf syntax is ok
nginx: configuration
file
/application/nginx-1
.6.3
//conf/nginx
.conf
test
is successful
[root@web01 bbs]
# /application/nginx/sbin/nginx -s reload
|
-
本机上进行测试
1
2
|
[root@web02 bbs]
# curl bbs.xpleaf.org
bbs.xpleaf.org node2 10.0.0.10
|
-
配置文件内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[root@lb01 conf]
# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application
/octet-stream
;
sendfile on;
keepalive_timeout 65;
upstream bbs_server_pools {
server 10.0.0.9:80 weight=1;
server 10.0.0.10:80 weight=1;
}
server {
listen 80;
server_name bbs.xpleaf.org;
location / {
proxy_pass http:
//bbs_server_pools
;
}
}
}
|
-
配置hosts解析
1
2
3
|
[root@web01 bbs]
# echo "127.0.0.1 bbs.xpleaf.org">>/etc/hosts
[root@web01 bbs]
# tail -1 /etc/hosts
127.0.0.1 bbs.xpleaf.org
|
-
检查配置文件与启动
1
2
3
4
|
[root@web01 bbs]
# /application/nginx/sbin/nginx -t
nginx: the configuration
file
/application/nginx-1
.6.3
//conf/nginx
.conf syntax is ok
nginx: configuration
file
/application/nginx-1
.6.3
//conf/nginx
.conf
test
is successful
[root@web01 bbs]
# /application/nginx/sbin/nginx -s reload
|
1
2
3
4
5
6
7
8
9
10
11
12
|
[root@lb01 conf]
# curl bbs.xpleaf.org
bbs.xpleaf.org node1 10.0.0.9
[root@lb01 conf]
# curl bbs.xpleaf.org
bbs.xpleaf.org node2 10.0.0.10
[root@lb01 conf]
# curl bbs.xpleaf.org
bbs.xpleaf.org node1 10.0.0.9
[root@lb01 conf]
# curl bbs.xpleaf.org
bbs.xpleaf.org node2 10.0.0.10
[root@lb01 conf]
# curl bbs.xpleaf.org
bbs.xpleaf.org node1 10.0.0.9
[root@lb01 conf]
# curl bbs.xpleaf.org
bbs.xpleaf.org node2 10.0.0.10
|
1
2
3
|
[root@web01 bbs]
# tail -2 /application/nginx/logs/access_bbs.log
10.0.0.7 - - [04
/Mar/2017
:11:33:00 +0800]
"GET / HTTP/1.0"
200 30
"-"
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
"-"
10.0.0.7 - - [04
/Mar/2017
:11:33:29 +0800]
"GET / HTTP/1.0"
304 0
"-"
"Mozilla
/5
.0 (Windows NT 6.1;
|
1
2
3
|
log_format main
'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
[root@lb01 conf]
# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application
/octet-stream
;
sendfile on;
keepalive_timeout 65;
upstream bbs_server_pools {
# 定义节点资源池
server 10.0.0.9:80 weight=1;
server 10.0.0.10:80 weight=1;
}
server {
listen 80;
server_name bbs.xpleaf.org;
location / {
proxy_pass http:
//bbs_server_pools
;
# 把请求转发到节点资源池中指定的主机中
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
|
1
2
3
|
[root@web01 bbs]
# tail -2 /application/nginx/logs/access_bbs.log
10.0.0.7 - - [04
/Mar/2017
:11:49:10 +0800]
"GET / HTTP/1.0"
200 30
"-"
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
"10.0.0.1"
10.0.0.7 - - [04
/Mar/2017
:11:49:11 +0800]
"GET / HTTP/1.0"
200 30
"-"
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
"10.0.0.1"
|
1
|
proxy_set_header Host $host;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
[root@lb01 conf]
# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application
/octet-stream
;
sendfile on;
keepalive_timeout 65;
upstream bbs_server_pools {
server 10.0.0.9:80 weight=1;
server 10.0.0.10:80 weight=1;
}
server {
listen 80;
server_name bbs.xpleaf.org;
location / {
proxy_pass http:
//bbs_server_pools
;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
}
}
}
|
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
|
[root@lb01 conf]
# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application
/octet-stream
;
sendfile on;
keepalive_timeout 65;
upstream bbs_server_pools {
server 10.0.0.9:80 weight=1;
}
upstream bbs_upload_server_pools {
server 10.0.0.10:80 weight=1;
}
server {
listen 80;
server_name bbs.xpleaf.org;
location / {
proxy_pass http:
//bbs_server_pools
;
}
location
/static
{
proxy_pass http:
//bbs_upload_server_pools
;
}
}
}
|
Nginx http功能模块 | 模块说明 |
ngx_http_proxy_module | proxy代理模块,用于把请求后抛给服务器节点或upstream服务器池 |
ngx_http_upstream_module | 负载均衡模块,可以实现网站的负载均衡功能及节点的健康检查 |
-
静态调度算法:负载均衡器根据自身设定的规则进行分配,不需要考虑后端节点服务器的情况
1
2
3
4
5
6
7
8
|
主要有rr、wrr、ip_hash
(1)rr轮询
如果节点服务器不宕机,请求将会平均分发到各节点服务器上;
(2)wrr权重轮询
按照设置的weight权重,将请示按比例分发到各节点服务器上;
(3)ip_hash
按照客户端IP的
hash
结果分配;
可以解决动态网页的session共享问题(会话保持),但无法保证1:1的负载均衡;
|
-
动态调度算法:负载均衡器会根据后端节点的当前状态来决定是否分发请求
1
2
3
4
5
6
7
8
9
10
11
|
主要有fair、least_conn、url_hash、一致性
hash
算法
(1)fair
根据后端节点服务器的响应时间来分配请求,响应时间短的优先分配;
Nginx本身不支持该算法,需要下载相关模块upstream_fair;
(2)least_conn
根据后端节点的连接数来决定分配情况,哪个机器连接数少就分发;
(3)url_hash(第三方调度算法)
与ip_hash类似,根据访问URL来分配请求,让每个URL定向到同一个后端服务器;
后端服务器为缓存服务器时效果显著;
(4)一致性
hash
算法(第三方调度算法)
比较复杂,这里不做介绍。
|