|-------|-----------------|-------------|
| node1 | 192.168.136.55 | Nginx主负载均衡器 |
| node3 | 192.168.136.57 | Web01服务器 |
| node4 | 192.168.136.58 | Web02服务器 |
| node5 | 192.168.135.131 | 客户端(测试) |
nginx反向代理
- 安装nginx
三台机器都安装nginx
yum install nginx -y
- 配置用于测试的Web服务(以下操作在两台web服务器)。 配置虚拟主机
bash
[root@node3 conf.d]# mkdir -p /usr/share/nginx/html/{www,bbs}/logs
[root@node3 ~]# cd /etc/nginx/conf.d/
[root@node3 conf.d]# vim vhost.conf
server {
listen 80;
server_name bbs.yunjisuan.com;
location / {
root /usr/share/nginx/html/bbs;
index index.html index.htm;
}
access_log /usr/share/nginx/html/bbs/logs/access_bbs.log main;
}
server {
listen 80;
server_name www.yunjisuan.com;
location / {
root /usr/share/nginx/html/www;
index index.html index.htm;
}
access_log /usr/share/nginx/html/www/logs/access_www.log main;
}
# nginx -t 测语法
# systemctl start nginx
node4 一样配置
- 配置用于测试的Web服务 在两台web服务器上操作。 准备测试页面
bash
echo "`hostname -I `www" > /usr/share/nginx/html/www/index.html
echo "`hostname -I `bbs" > /usr/share/nginx/html/bbs/index.html
客户端测试:
bash
# vi + /etc/hosts
192.168.136.57 bbs.yunjisuan.com www.yunjisuan.com
192.168.136.58 bbs.yunjisuan.com www.yunjisuan.com
[root@node5 ~]# curl www.yunjisuan.com
192.168.136.57 www
[root@node5 ~]# curl bbs.yunjisuan.com
192.168.136.57 bbs
#或者
[root@node5 ~]# curl -H host:bbs.yunjisuan.com 192.168.136.57
192.168.136.57 bbs
[root@node5 ~]# curl -H host:www.yunjisuan.com 192.168.136.57
192.168.136.57 www
nginx负载均衡
实现 Nginx 负载均衡的组件说明
|--------------------------|------------------------------------------|
| Nginx http 功能模块 | 模块说明 |
| ngx_http_proxy_module | proxy 代理模块,用于把请求后拋给服务器节点或 upstream 服 务器池 |
| ngx_http_upstream_module | 负载均衡模块, 可以实现网站的负载均衡功能及节点的健康检査 |
配置简单的负载均衡 以下操作在node1
实现:经过反向代理后的节点服务器记录用户IP
反向代理多虚拟主机节点服务器
bash
# cd /etc/nginx/conf.d/
# vim vhost.conf
upstream wwwPools {
server 192.168.136.57;
server 192.168.136.58;
}
server {
listen 80;
server_name www.yunjisuan.com;
location / {
proxy_pass http://wwwPools;
proxy_set_header Host $host; # 依据host字段信息,识别代理的是哪个虚拟主机
proxy_set_header X-Forwarded-For $remote_addr; # 显示客户端的IP地址
}
}
server {
listen 80;
server_name bbs.yunjisuan.com;
location / {
proxy_pass http://wwwPools;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
客户端: (改成反向代理的地址)
bash
# vim /etc/hosts
192.168.136.55 bbs.yunjisuan.com www.yunjisuan.com
bash
[root@node5 ~]# for ((i=1;i<=4;i++)); do curl http://bbs.yunjisuan.com;done
192.168.136.58 bbs
192.168.136.57 bbs
192.168.136.58 bbs
192.168.136.57 bbs
如果web服务器是apache
bash
# vim vhost.conf
<VirtualHost *:80>
ServerName bbs.yunjisuan.com
DocumentRoot "/var/www/html/bbs/"
CustomLog "/var/www/html/bbs/logs/access_log" combined
</VirtualHost>
<VirtualHost *:80>
ServerName www.yunjisuan.com
DocumentRoot "/var/www/html/www/"
CustomLog "/var/www/html/www/logs/access_log" combined
</VirtualHost>
bash
# cp /usr/share/nginx/html/www/index.html /var/www/html/www
# cp /usr/share/nginx/html/bbs/index.html /var/www/html/bbs
修改配置文件 vim /etc/httpd/conf/httpd.conf
bash
LogFormat "%h %{X-FORWARDED-FOR}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
如果是tomcat
改成
bash
pattern="%{X-FORWARDED-FOR}i %l %u %t %r %s %b %D %q %{User-Agent}i" resolveHosts="false"
根据URL中的目录地址实现代理转发
vim /etc/nginx/conf.d/vhost.conf
bash
upstream static_pools {
server 192.168.136.57;
}
upstream upload_pools {
server 192.168.136.58;
}
upstream default_pools {
server 192.168.136.59;
}
server {
listen 80;
server_name www.yunjisuan.com;
location /static/ {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://static_pools;
}
location /upload/ {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://upload_pools;
}
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://default_pools;
}
}
在node3上创建页面
bash
[root@node3 static]# echo "static web page test." > /usr/share/nginx/html/static/index.html
在node4上
bash
[root@openEuler-node4 ~]# mkdir /var/www/html/upload
[root@openEuler-node4 ~]# echo "upload test page" > /var/www/html/upload/index.html
在192.168.136.59上
bash
# echo "default test page" > /usr/share/nginx/html/index.html
bash
[root@node5 conf.d]# curl http://www.yunjisuan.com/upload/
upload test page
[root@node5 conf.d]# curl http://www.yunjisuan.com/static/
static web page test.
[root@node5 conf.d]# curl http://www.yunjisuan.com
default test page
根据客户端的设备(user_agent)转发
bash
[root@node1 ~]# vim /etc/nginx/conf.d/vhost.conf
upstream static_pools {
server 192.168.136.57;
}
upstream upload_pools {
server 192.168.136.58;
}
upstream default_pools {
server 192.168.136.59;
}
server {
listen 80;
server_name www.yunjisuan.com;
location / {
if ($http_user_agent ~* "MSIE")
{ proxy_pass http://static_pools;
}
if ($http_user_agent ~* "Chrome")
{
proxy_pass http://upload_pools;
}
proxy_pass http://default_pools;
}
}
bash
[root@node3 html]# echo "static page" > /usr/share/nginx/html/index.html
[root@node4 html]# echo "upload test page " > /usr/share/nginx/html/index.html
bash
[root@node5 conf.d]# curl -A chrome -H host:www.yunjisuan.com 192.168.136.55
upload test page
[root@node5 conf.d]# curl -A MSIE -H host:www.yunjisuan.com 192.168.136.55
static page
[root@node5 conf.d]# curl -A XXX -H host:www.yunjisuan.com 192.168.136.55
default test page
upstream其他使用
修改为IP HASH算法
始终只由一台服务器提高
添加server备份
当前面都down了 启动backup
注意:backup不能和ip_hash同时开启
想完全不启用其中的一台server
1、backup所有RS都不能提高服务时才起作用
2、backup 不能和ip_hash算法一起使用
3、down 通常用于RS维护时,不参与调度
4、ip_hash 会话保持