nginx负载均衡简单配置
准备三台虚拟机来做这个实验:
172.16.100.6 web服务器
172.16.100.7 web服务器
172.16.100.106 负载均衡服务器
首先三台电脑装好nginx软件:
向web服务器中放入测试文件:
1
2
3
4
5
6
7
8
|
< html >
< head >
< title >Welcome to nginx!</ title >
</ head >
< body bgcolor = "white" text = "black" >
< center >< h1 >Welcome to nginx! 172.16.100.6</ h1 ></ center >
</ body >
</ html >
|
配置负载均衡服务器:
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
|
vi /usr/local/nginx/conf/nginx .conf
user nginx nginx;worker_processes 1; events { worker_connections 1024;
} http { include mime.types;
default_type application /octet-stream ;
sendfile on;
keepalive_timeout 65;
upstream webservs {
server 172.16.100.6 weight=1 max_fails=2 fail_timout=2;
server 172.16.100.7 weight=1 max_fails=2 fail_timout=2;
server 127.0.0.1:8083 backup;
}
server {
listen 80;
server_name localhost;
index index.html index.htm index.php;
location / {
proxy_pass webservs;
proxy_set_header X-Real-IP $remote_addr;
}
error_page 500 502 503 504 /50x .html;
location = /50x .html {
root html;
}
}
server {
listen 8083;
server_name localhost;
index index.html index.htm;
root /web/errorpages ;
} |
mkdir -p web/errorpages
vi web/errorpages/index.html
sorry!.......
当两台后端服务器挂掉,就会使用本机的sever访问的页面是sorry
拓展:
定义upstream中server指令语法如下:
server address [parameters]
关键字server必选。
address也必选,可以是主机名、域名、ip或unix socket,也可以指定端口号。
parameters是可选参数,可以是如下参数:server address [ parameters ]
weight = NUMBER 权重
max_fails = NUMBER 最多错误几次
fail_timeout = TIME 最多错误迟疑多长时间,不确定默认10s
down 表示当前server已停用
backup 表示当前server是备用服务器,只有其它非backup后端服务器都挂掉了或者很忙才会分配到请求。
max_fails和fail_timeout一般会关联使用,如果某台server在fail_timeout时间内出现了max_fails次连接失败,那么Nginx会认为其已经挂掉了,从而在fail_timeout时间内不再去请求它,fail_timeout默认是10s,max_fails默认是1,即默认情况是只要发生错误就认为服务器挂掉了,如果将max_fails设置为0,则表示取消这项检查。
本文转自ling118 51CTO博客,原文链接:http://blog.51cto.com/meiling/1896426,如需转载请自行联系原作者