nginx七层负载均衡
准备多台虚拟机环境准备:
关闭防火墙和selinux systemctlstopfirewalld&&setenforce 0
设置永久关闭 systemctldisablefirewalld
vim/etc/selinux
两台虚拟机做静态页面,两台做动态页面,一台做nginx反向代理,一台做测试
注:最好做好dns解析
做静态页面:
HTMLA&HTML B
root@html1 ~]# yum-yinstall nginx
创建测试页面index.html,开启服务
PHP A&PHPB
# yum-yinstallhttpdphp
创建测试页面index.php,开启服务
安装Nginx
# yum-yinstall nginx
修改配置文件
定义主机集群
#vim /etc/nginx/nginx.conf
http {
upstream htmlservers {
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream phpservers {
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
}
}
访问页面(使用之前定义的主机集群)
方法一:
server {
location / {
root /usr/share/nginx/html;
index index.html index.htm;
if ($request_uri~*\.html$) {
proxy_pass http://htmlserver;
}
if ($request_uri~*\.php$){
proxy_pass http://phpserver;
}
}
}
方法二:
server {
location ~* \.html$ {
proxy_pass http://htmlservers;
}
location ~* \.php$ {
proxy_pass http://phpservers;
}
}
注:此处做测试的是动静分离
在客户端访问 Nginx 测试
# elinks –dump http:// nginx/index.php
# elinks –dump http:// nginx/index.html
upstream支持的负载均衡算法
轮询(默认)(rr)(roundrobin)(轮流分流量)
可以使用weight指定权重,权重越大,被调度的次数越多(权重用数字表示,可以任意数字)
rr(普通轮询)
wrr(权重轮询)
例子:
upstream httpservers {
server 192.168.1.3:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.4:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.5:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.100:80 backup;
}
ip_hash 根据请求的ip调度,可以解决session的问题,不能使用weight(每有一个ip访问,分配一台服务器)
fail:可以根据请求页面的大小和加载时间长短进行调度,使用第三方的upstream_fair模块
url_hash:按请求的url的hash进行调度,从而使每个url定向到同一服务器,使用第三方的hash模块
upstream支持的状态参数
down: 暂定对该服务器进行调度(相当于注释)
backup: 类似与LVSSorry Server,当所有的非backup的服务器故障才使用,平时不使用
max_fails: 请求失败的次数,默认为1
fail_timeout: 在经历max_fail次失败后,服务器暂停服务的时间
示例:
upstream tianyun.com {
# ip_hash;
server 192.168.10.137 weight=1 max_fails=2 fail_timeout=2;
server 192.168.10.20 weight=2 max_fails=2 fail_timeout=2;
server 192.168.10.251 max_fails=2 fail_timeout=5 down;
server 192.168.10.253 backup;
}
proxy_next_upstream:这个指令属于 http_proxy 模块的,指定后端返回什么样的异常响应时,使用另一个realserver(即将请求传递到下一个服务器)
示例:
location/ {
proxy_passhttp://httpservers;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
Apache LogFormat 可选(日志格式)
LogFormat "%{X-Real-IP}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %b" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
根据站点分区进行调度
http {
upstream news {
server 192.168.1.11:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.12:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.13:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream milis {
server 192.168.1.21:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.22:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.23:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream videos {
server 192.168.1.31:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.32:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.33:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream images {
server 192.168.1.41:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.42:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.43:80 weight=2 max_fails=2 fail_timeout=2;
}
upstream others {
server 192.168.1.51:80 weight=1 max_fails=2 fail_timeout=2;
server 192.168.1.52:80 weight=2 max_fails=2 fail_timeout=2;
server 192.168.1.53:80 weight=2 max_fails=2 fail_timeout=2;
}
server {
location / {
proxy_pass http://others;
}
location /news {
proxy_pass http://news;
}
location /mili {
proxy_pass http://milis;
}
location ~* \.(wmv|mp4|rmvb)$ {
proxy_pass http://videos;
}
location ~* \.(png|gif|jpg)$ {
proxy_pass http://images;
}
}
页:
[1]