An additional Apache configuration file parameters
Bowen's early environment ready reference: blog "nginx simple load balancing"
Load balancing configuration instructions detailed
load balancing module: upstream
functions: load balancing may be defined in the node information backend
Reference configuration (current mode is least_conn)
[root@lb01 conf.d]# cat /etc/nginx/nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
#include /etc/nginx/conf.d/*.conf;
upstream oldboy {
least_conn;
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;
}
}
}
Function module upstream :( HTTP module in the module configuration in general, there is a back upstream of HTTP} brackets)
Effective way to define the load balancer
1) The default resource allocation polling of: evenly distributed
2) resource allocation information (weight) weight information is arranged to scale
upstream oldboy {
server 10.0.0.7:80 weight=10;
server 10.0.0.8:80 weight=1;
server 10.0.0.9:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
}
}
3) allocate more resources to allocate resources based on the number of server connections will be more leisure least_conn
upstream oldboy {
least_conn;
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;
}
}
4) According to the resource allocation request processing capability: fair The node processing efficiency
5) According to hash algorithm to allocate resources information ip_hash test results is always a web of information, the equivalent of a guest chef in charge of a food table, it has always been a chef in charge.
upstream oldboy {
ip_hash;
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;
}
}
Back-end node health check LVS no health checks.
Users access the site www.oldboy.com - normal service load balancing --web01 normal response
1) Set the number of visits to a node failure several opportunities (continuous) max_files default value of 1
upstream oldboy {
server 10.0.0.7:80 max_fails=3;
server 10.0.0.8:80 max_fails=3;
server 10.0.0.9:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
}
}
2) Check the settings again after a long timeout nodes to check with the issue of
default is 10s
upstream oldboy {
server 10.0.0.7:80 max_fails=3 fail_timeout=10s;
server 10.0.0.8:80 max_fails=3 fail_timeout=10s;
server 10.0.0.9:80;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
}
}
3) Set Hot Standby server (couples the spare)
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80 backup;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
}
}
反向代理模块:proxy_pass
1)指定调用集群信息
proxy_pass http://oldboy;
2)识别用户请求host信息
proxy_set_header host $host
; —设置修改负载均衡HTTP请求报文中请求头内容
调用负载均衡HTTP请求报文中的host提取,并修改为$host (调用用户请求报文中host)
修改web节点信息:
1加载bbs网站信息 (web01 web02 web03)
vim /etc/nginx/nginx.conf
include /etc/nginx/conf.d/www.conf;
include /etc/nginx/conf.d/bbs.conf;
2 在站点目录下创建首页文件
web01:
echo bbs01-info >/html/bbs/index.html
web02
echo bbs02-info >/html/bbs/index.html
web03
echo bbs03-info >/html/bbs/index.html
提示
:这里的bbs.conf文件内容一致;都是:
server {
listen 80;
server_name bbs.oldboy.com;
location / {
root /html/bbs/;
index index.html;
}
}
3 访问测试过程(lb01)
注意
:这个是在负载均衡服务器作为用户进行访问的。也就是没有负载均衡,用户直接网站时。一切正常。
[root@lb01 conf.d]# curl -H host:bbs.oldboy.com 10.0.0.7
web01-info
[root@lb01 conf.d]# curl -H host:bbs.oldboy.com 10.0.0.7
bbs01-info
[root@lb01 conf.d]# curl -H host:bbs.oldboy.com 10.0.0.8
bbs02-info
[root@lb01 conf.d]# curl -H host:bbs.oldboy.com 10.0.0.9
bbs03-info
[root@lb01 conf.d]# curl -H host:www.oldboy.com 10.0.0.9
web03-info
[root@lb01 conf.d]# curl -H host:www.oldboy.com 10.0.0.8
web02-info
[root@lb01 conf.d]# curl -H host:www.oldboy.com 10.0.0.7
web01-info
如果是用户经过负载均衡在访问网站时:(在web服务器作为用户,访问负载服务器)
[root@web01 conf.d]# curl -H host:bbs.oldboy.com 10.0.0.31
web02-info
[root@web01 conf.d]# curl -H host:bbs.oldboy.com 10.0.0.31
web01-info
[root@web01 conf.d]# curl -H host:bbs.oldboy.com 10.0.0.31
web02-info
[root@web01 conf.d]# curl -H host:www.oldboy.com 10.0.0.31
web01-info
[root@web01 conf.d]# curl -H host:www.oldboy.com 10.0.0.31
web02-info
[root@web01 conf.d]# curl -H host:www.oldboy.com 10.0.0.31
出现了问题:怎么访问bbs出现www的页面???
原因:因为两个网址都符合四层的匹配条件,然后因为在主配置文件中的文件加载顺序的原因,导致访问不同域名仍然出现同一个服务器内容。最直接的原因是因为post头部携带的信息发生了改变,不再是一开始指定的www.oldboy.com信息(默认修改为了oldboy信息)
nginx.conf中include位置越靠前越优先加载
如果include是*.conf,则根据字母顺序优先级匹配
而负载均衡的识别host头部信息的作用就可以解决这个问题
负载均衡配置:
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80 backup;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
}
}
再次测试:
[root@web01 conf.d]# curl -H host:www.oldboy.com 10.0.0.31
web01-info
[root@web01 conf.d]# curl -H host:www.oldboy.com 10.0.0.31
web02-info
[root@web01 conf.d]# curl -H host:bbs.oldboy.com 10.0.0.31
bbs01-info
[root@web01 conf.d]# curl -H host:bbs.oldboy.com 10.0.0.31
bbs02-info
[root@web01 conf.d]# curl -H host:bbs.oldboy.com 10.0.0.31
bbs01-info
一切正常。利用负载均衡可以加载不同网站页面。
3)识别用户真实IP地址信息
$http_x_forwarded for —日志格式变量用于记录真实用户IP地址
用户访问 --- 负载均衡(记录用户IP地址) --- web服务器(查看用户IP地址)
HTTP请求报文(IP地址信息) --> 识别真实用户地址
proxy_set_header —设置负载均衡请求头的信息
proxy_set_header X-Forwarded-For $remoe_addr;
负载均衡配置:
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80 backup;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://oldboy;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
这个配置好以后在日志中查看真实的IP地址。
注意:
如图所示,访问负载均衡,负载均衡将访问给到了第一台web01服务器,那么在web01的服务器上查看日志才能看到真实IP地址
,不能是在负载均衡的服务器上查看日志。那样是看不到的。
日志信息如下:
10.0.0.31 - - [17/Feb/2020:09:31:09 -0500] "GET / HTTP/1.0" 200 11 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36" "10.0.0.1"
10.0.0.31 - - [17/Feb/2020:09:31:09 -0500] "GET / HTTP/1.0" 200 11 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36" "10.0.0.1"
10.0.0.31 - - [17/Feb/2020:09:35:00 -0500] "GET / HTTP/1.0" 200 11 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36" "10.0.0.1"
4)负载均衡健康检查并不能对页面信息做检测检查,如何处理
实现对网站页面进行健康检查
proxy_next_upstream error | timeout | invalid_header | http_500 | http_502 | http_503 | http_504 | http_403 | http_404 | http_429
解决
:监控网站页面是否正常,负载均衡只返回用户正确页面。
如下的配置中:
如果页面出现403的错误,那么负载就不会将请求发送给他,保证用户看不到出错页面。
负载配置信息:
upstream oldboy {
server 10.0.0.7:80;
server 10.0.0.8:80;
server 10.0.0.9:80 backup;
}
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_403;
}
}
负载均衡企业中实际应用
1)实现网站动静分离过程
动静分离的架构 --URI信息不同
访问URI信息 静态资源 —静态集群
访问URI信息 动态资源 ----动态集群
架构环境规划:
用户请求(URI) 处理请求服务器 站点目录 功能
/upload 10.0.0.8:80 (upload) html/www/upload upload服务器
/static 10.0.0.7:80 (static) html/www/static static静态服务器
/ 10.0.0.9:80 (默认) html/www 默认
第一个历程:在后端节点上创建不同集群节点资源
· 上传集群节点配置: 10.0.0.8
编写nginx.conf / 修改站点目录
www - server 站点目录 /html/www/upload
mkdir /html/www/upload -p
echo upload-page > /html/www/upload/index.html
· 静态集群节点配置: 10.0.0.7
编写nginx.conf / 修改站点目录
www - server 站点目录 /html/www/static/
mkdir /html/www/static
echo static-page >/html/www/static/index.html
第二个历程:修改负载均衡j节点的配置文件
upstream upload {
server 10.0.0.8:80;
}
upstream static {
server 10.0.0.7:80;
}
upstream default {
server 10.0.0.9:80;
}
server {
listen 80;
server_name localhost;
location /upload {
proxy_pass http://upload;
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
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_403;
}
location / {
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_403;
}
}
}
Third course: In fact, there is no third course, but error-prone, and put this to put down here.
web01
The www.conf profile
[root@web01 static]# cat /etc/nginx/conf.d/www.conf
server {
listen 80;
server_name www.oldboy.com;
location / {
root /html/www/;
index index.html;
}
}
web02
The www.conf profile
server {
listen 80;
server_name www.oldboy.com;
location / {
root /html/www/;
index index.html;
}
}
Note 新添加的文件后属主属组是否和/html/www下面
the same.
Note:单点配置IP地址以及动态静态划分和负载主配置文件中指定的是否一样
.
2) to achieve different depending on the display page the UE
架构环境规划
手机端集群 iphone 10.0.0.7 显示页面信息 iphone-page
电脑端集群 PC 10.0.0.8 显示页面信息 PC-page
First course: Configuring load balancing node information
iphone集群中的节点: 10.0.0.7
echo iphone-page > /html/www/index.html
PC集群中的节点: 10.0.0.8
echo PC-page > /html/www/index.html
Second course: load balancing configuration information
upstream PC {
server 10.0.0.8:80;
}
upstream iphone {
server 10.0.0.7:80;
}
server {
listen 80;
server_name localhost;
location / {
if ($http_user_agent ~* "iphone|Android") {
proxy_pass http://iphone;
}
if ($http_user_agent ~* "chrome") {
proxy_pass http://PC;
}
proxy_set_header host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_next_upstream error timeout invalid_header http_403;
}
}