nginx反向代理及负载均衡

|-------|-----------------|-------------|
| node1 | 192.168.136.55 | Nginx主负载均衡器 |
| node3 | 192.168.136.57 | Web01服务器 |
| node4 | 192.168.136.58 | Web02服务器 |
| node5 | 192.168.135.131 | 客户端(测试) |

nginx反向代理

  1. 安装nginx

三台机器都安装nginx

yum install nginx -y
  1. 配置用于测试的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 一样配置

  1. 配置用于测试的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 会话保持

相关推荐
叶北辰CHINA10 分钟前
nginx反向代理,负载均衡,HTTP配置简述(说人话)
linux·运维·nginx·http·云原生·https·负载均衡
不惑_27 分钟前
在 Ubuntu 安装 Python3.7(没有弯路)
linux·运维·ubuntu
theo.wu2 小时前
使用Buildpacks构建Docker镜像
运维·docker·容器
Lansonli2 小时前
云原生(四十八) | Nginx软件安装部署
nginx·云原生·ecs服务器
枫叶丹44 小时前
【在Linux世界中追寻伟大的One Piece】进程信号
linux·运维·服务器
刻词梨木4 小时前
ubuntu中挂载点内存不足,分配不合理后使用软链接的注意事项
linux·运维·ubuntu
灯火不休ᝰ5 小时前
[win7] win7系统的下载及在虚拟机中详细安装过程(附有下载文件)
linux·运维·服务器
加油,旭杏8 小时前
【中间件学习】fastCG介绍和使用
学习·nginx·fastcgi
HHoao9 小时前
Ubuntu启动后第一次需要很久才能启动GTK应用问题
linux·运维·ubuntu
小灰兔的小白兔9 小时前
【Ubuntu】Ubuntu常用命令
linux·运维·ubuntu