负载均衡详述部分
Syntax: upstream name { ... } --- 指定可以分配用户请求web节点信息
Default: —
Context: http
分配后端节点方式方法:
weight --权重 按比例进行权重分配访问资源
upstream oldboy {
server 10.0.0.7:80 weight=3;
server 10.0.0.8:80 weight=1;
server 10.0.0.9:80 weight=2;
}
max_fails=3 --- 向有问题的节点发送请求的次数
fail_timeout=5s --- 指定多久以后给用问题节点再次发送请求
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80 backup;
}
least_conn 连接数少的多分配请求;
upstream oldboy {
least_conn;
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
ip_hash; 缺陷: 负载不均
10.0.0.1 --- 100 0 - 100 web01 wordpress
10.0.0.2 --- 202 负载均衡 101 - 200 web02 wordpress
10.0.0.3 --- 300 201 - 300 web03 wordpress
第一个功能配置: 经过负载均衡访问网站看到不同的页面信息
环境准备:
第一个历程: web01-web03服务器配置三个网站文件
第二个历程: 配置站点目录资源
web01
mkdir /html/{www,bbs,blog} -p
for name in www bbs blog; do echo "web01 $name.oldboy.com" >/html/$name/oldboy.html;done
web02
mkdir /html/{www,bbs,blog} -p
for name in www bbs blog; do echo "web02 $name.oldboy.com" >/html/$name/oldboy.html;done
web03
mkdir /html/{www,bbs,blog} -p
for name in www bbs blog; do echo "web03 $name.oldboy.com" >/html/$name/oldboy.html;done
proxy_set_header -- 修改请求头信息
proxy_set_header Host $host;
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
proxy_set_header Host $host;
}
}
第二个功能配置: 让web节点日志中显示真实用户IP地址
proxy_set_header -- 在请求头中添加信息
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
第三个功能配置: 根据页面显示信息进行健康检查
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_404;
}
}
说明: 给用户显示的页面一定是正常的页面信息
企业实际应用过程
准备环境:
动态集群: web01 uri /dynamic --- 显示页面 dynamic.oldboy.com
静态集群: web02 uri /static --- 显示页面 static.oldboy.com
上传集群: wen03 uri /upload --- 显示页面 upload.oldboy.com
web01:
echo dynamic.oldboy.com >/html/www/index.html
修改:
mkdir /html/www/dynamic
mv /html/www/index.html /html/www/dynamic
web02:
echo static.oldboy.com >/html/www/index.html
修改:
mkdir /html/www/static/
mv /html/www/index.html /html/www/static
web03:
echo upload.oldboy.com >/html/www/index.html
修改:
mkdir /html/www/upload
mv /html/www/index.html /html/www/upload
负载均衡服务器配置:
upstream dynamic {
server 10.0.0.7:80;
}
upstream static {
server 10.0.0.8:80;
}
upstream upload {
server 10.0.0.9:80;
}
server {
listen 80;
server_name localhost;
location /dynamic/ {
proxy_pass http://dynamic;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_404;
}
location /static/ {
proxy_pass http://static;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_404;
}
location /upload {
proxy_pass http://upload;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_404;
}
}
准备环境:
iphone iphone web01 iphone.oldboy.com
chrome chrome web02 chrome.oldboy.com
default default web03 default.oldboy.com
web01
echo iphone.oldboy.com > /html/bbs/index.html
web02
echo chrome.oldboy.com > /html/bbs/index.html
web03
echo default.oldboy.com > /html/bbs/index.html
配置负载均衡服务器:
location / {
if ($http_user_agent ~* Android) {
proxy_pass http://iphone;
}
if ($http_user_agent ~* chrome) {
proxy_pass http://chrome;
}
proxy_pass http://default;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_404;
}