负载均衡
nginx服务器 192.168.75.134 yuan.cao
web服务器1 192.168.75.129 brother.cao
web服务器2 192.168.20.156 me.cao
修改配置文件:
vim /etc/nginx/sites-available/yuan.cn
//自己的配置文件
相关服务器参考Nginx多站点虚拟主机配置
upstream yuan.cao
{
server 192.168.75.129:80;
server 192.168.20.156:80;
}
server {
listen 80;
root /usr/share/nginx/yuan;
index index.php index.html index.htm;
server_name yuan.cao;
location / {
try_files $uri $uri/ =404;
proxy_pass http://yuan.cao;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php5.6-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
配置好重启nginx服务器就可以查看效果了:
sudo service nginx restart
扩展:
1.轮询(默认方式)
每个请求按时间顺序逐一分配到后端服务器,如果后端服务器down掉,能自动剔除
2.weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
upstream bakend {
server 192.168.159.10 weight=10;
server 192.168.159.11 weight=10;
}
3.ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
upstream resinserver{
ip_hash;
server 192.168.20.156:8080;
server 192.168.75.129:8080;
}
4.fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream resinserver{
server server1;
server server2;
fair;
}
5、url_hash(第三方)
例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
upstream resinserver{
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
注意:
定义负载均衡设备的Ip及设备状态
upstream resinserver{
ip_hash; server 127.0.0.1:8000 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6801;
server 127.0.0.1:6802 backup;
}
在需要使用负载均衡的server中增加proxy_pass http://resinserver/;每个设备的状态设置为:
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进行匹配.可以进行重定向或者进行新的代理 负载均衡