配置Nginx实现用IP测试灰度发,通过不同用户ID测试灰度发布

复制代码
一.配置Nginx实现用IP测试灰度发布
实验要求
要求不同IP的客户访问相同代理时,可以看到不同集群主机的内容 

环境准备
主机名	IP地址	角色
proxy(已存在)	eth1:192.168.99.5/24	代理服务器
web1(已存在)	eth1:192.168.99.100/24	web服务器
web2(已存在)	eth1:192.168.99.200/24	web服务器
创建不同集群,准备多台集群主机
通过$remote_addr变量识别不同客户机

1)为web1搭建nginx虚拟主机
[root@proxy python]# scp /root/lnmp_soft/nginx-1.22.1.tar.gz 192.168.99.100:/root
[root@proxy python]# scp /root/lnmp_soft/nginx-1.22.1.tar.gz 192.168.99.200:/root
[root@web1 ~]# systemctl disable --now httpd    #取消开机自启,停止httpd服务
[root@web1 ~]# yum -y install make gcc openssl-devel pcre-devel #安装基础依赖包
[root@web1 ~]# tar -xf nginx-1.22.1.tar.gz
[root@web1 ~]# cd nginx-1.22.1/
[root@web1 nginx-1.22.1]# ./configure 
[root@web1 nginx-1.22.1]# make && make install
[root@web1 nginx-1.22.1]# vim /usr/local/nginx/conf/nginx.conf  #默认server listen 80的不要动,直接另外写一个server
http {    
...
    server {                      #此为新添加的server
        listen       8001;
        server_name  localhost;
        root html8001;
        index index.html;
}
    server {
        listen       80;
        server_name  localhost;
...
[root@web1 nginx-1.22.1]# /usr/local/nginx/sbin/nginx 
[root@web1 nginx-1.22.1]# mkdir /usr/local/nginx/html8001
[root@web1 nginx-1.22.1]# echo web1-nginx-80 > /usr/local/nginx/html/index.html
[root@web1 nginx-1.22.1]# echo web1-nginx-8001 > /usr/local/nginx/html8001/index.html

使用web1测试
[root@web1 nginx-1.22.1]# curl 192.168.99.100
web1-nginx-80
[root@web1 nginx-1.22.1]# curl 192.168.99.100:8001
web1-nginx-8001

2)为web2搭建nginx
[root@web2 ~]# systemctl disable --now httpd
[root@web2 ~]# yum -y install make gcc openssl-devel pcre-devel #安装基础依赖包
[root@web2 ~]# tar -xf nginx-1.22.1.tar.gz
[root@web2 ~]# cd nginx-1.22.1/
[root@web2 nginx-1.22.1]# ./configure 
[root@web2 nginx-1.22.1]# make && make install
[root@web2 nginx-1.22.1]# /usr/local/nginx/sbin/nginx 
[root@web2 nginx-1.22.1]# echo web2-nginx-80 > /usr/local/nginx/html/index.html

3)使用proxy主机在nginx配置中创建集群
[root@proxy python]# killall uwsgi      #停止uwsgi
[root@proxy python]# cd /usr/local/nginx
[root@proxy nginx]# cp conf/nginx.conf.default conf/nginx.conf  #恢复配置文件
[root@proxy nginx]# vim conf/nginx.conf
http {    
...
    upstream default {              #正常业务集群
        server 192.168.99.100:80;
        server 192.168.99.200:80;
    }
    upstream s8001 {
        server 192.168.99.100:8001;   #新版本业务集群1
    }
    server {
        listen       80;
        server_name  localhost;
        set $group "default";       #自己定义变量$group,值为default,访问default集群
        if ($remote_addr ~ "192.168.99.1"){  #如果客户机ip是包含192.168.99.1就访问新版本业务集群1,$remote_addr为nginx的内置变量,代表访问者的ip
             set $group s8001;  #变量重新赋值
        }
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://$group;       #调用集群
            root   html;
            index  index.html index.htm;
        }
...
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload


使用proxy测试
[root@proxy ~]# curl 192.168.99.5   #显示正常业务集群
web1-nginx-80
[root@proxy ~]# curl 192.168.99.5   #显示正常业务集群
web2-nginx-80

web1访问proxy
[root@web1 ~]# curl 192.168.99.5    #显示新版本业务集群1
web1-nginx-8001

二、通过不同用户ID测试灰度发布

实验要求
不同ID的客户访问相同代理时,可以看到不同集群主机的内容
部署LNPM环境(已操作)
proxy主机部署LNMP服务器相关软件(前面已经安装过,在此不在重复安装)

1)proxy主机修改Nginx配置文件(修改默认首页与动静分离)
[root@proxy ~]# cd /usr/local/nginx
[root@proxy nginx]# cp conf/nginx.conf.default conf/nginx.conf  #恢复配置文件
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
...
 location  ~  \.php$  {
            root           html;
            fastcgi_pass  unix:/run/php-fpm/www.sock;
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }
...        
php-fpm的配置文件之前已经修改好,此时不用修改
启动LNMP服务器相关的服务
1)重新加载Nginx服务
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload
[root@proxy nginx]# ss -antlp | grep :80   

2)启动MySQL服务(已经启动则不需要执行)
[root@proxy nginx]# systemctl start mariadb
[root@proxy nginx]# systemctl status mariadb

3)启动PHP-FPM服务(已经启动则不需要执行)
[root@proxy nginx]# systemctl start  php-fpm
[root@proxy nginx]# systemctl status php-fpm
[root@proxy nginx]# cp /root/lnmp_soft/php_scripts/test.php /usr/local/nginx/html/
[root@proxy nginx]# curl 192.168.99.5/test.php      #测试动态页面
<html>
<body>
This is HTML message
</br>
c is bigger</body>
</html>

4) 配置好lnmp之后,拷贝带登录效果的测试页面
[root@proxy nginx]# cd /root/lnmp_soft/php_scripts/
[root@proxy nginx]# tar -xf php-session-demo.tar.gz    #释放带登录功能的网页
[root@proxy nginx]# cp -r php-session-demo/* /usr/local/nginx/html/   #拷贝页面到nginx中

使用浏览器访问192.168.99.5/index.php  可以看到有登录界面的网页

如果想要访问此网站时直接输入http://192.168.99.5就能访问到这个登录页面,可以更改以下配置实现
[root@proxy nginx]# vim /usr/local/nginx/conf/nginx.conf
server {
   listen 80;
   
   location / {
      root html;
      index index.php index.html index.htm;     #新添加index.php
   }

...
 location  ~  \.php$  {
            root           html;
            fastcgi_pass  unix:/run/php-fpm/www.sock;
            fastcgi_index  index.php;
            include        fastcgi.conf;
        }
...  
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload
直接输入http://192.168.99.5就能访问到登录页面,输入任意用户名和密码登录测试即可

注:这里index.php是登录前页面 ,home.php是登录后才能看的页面
修改home.php页面
1)修改home.php页面
[root@proxy php_scripts]# vim /usr/local/nginx/html/home.php        #修改php页面,将原有Welcome那行修改成以下状态
Welcome :  <?php
if(preg_match("/^abc/",$_SESSION['login_user'])) {      #preg_match匹配正则,如果登录账号是以abc开头,就连接99.100,否则连接99.200
echo "<a href='http://192.168.99.100'>开始</a>";
}
else
{
echo "<a href='http://192.168.99.200'>开始</a>";
}
?>

2)测试
浏览器访问192.168.99.5/index.php分别输入不同名称的账户,可以看到"开始"连接的是不同的地址

配置Nginx实现用IP测试灰度发布

实验要求

要求不同IP的客户访问相同代理时,可以看到不同集群主机的内容

环境准备

主机名 IP地址 角色

proxy(已存在) eth1:192.168.99.5/24 代理服务器

web1(已存在) eth1:192.168.99.100/24 web服务器

web2(已存在) eth1:192.168.99.200/24 web服务器

创建不同集群,准备多台集群主机

通过$remote_addr变量识别不同客户机

1)为web1搭建nginx虚拟主机

root@proxy python# scp /root/lnmp_soft/nginx-1.22.1.tar.gz 192.168.99.100:/root

root@proxy python# scp /root/lnmp_soft/nginx-1.22.1.tar.gz 192.168.99.200:/root

root@web1 \~# systemctl disable --now httpd #取消开机自启,停止httpd服务

root@web1 \~# yum -y install make gcc openssl-devel pcre-devel #安装基础依赖包

root@web1 \~# tar -xf nginx-1.22.1.tar.gz

root@web1 \~# cd nginx-1.22.1/

root@web1 nginx-1.22.1# ./configure

root@web1 nginx-1.22.1# make && make install

root@web1 nginx-1.22.1# vim /usr/local/nginx/conf/nginx.conf #默认server listen 80的不要动,直接另外写一个server

http {

...

server { #此为新添加的server

listen 8001;

server_name localhost;

root html8001;

index index.html;

}

server {

listen 80;

server_name localhost;

...

root@web1 nginx-1.22.1# /usr/local/nginx/sbin/nginx

root@web1 nginx-1.22.1# mkdir /usr/local/nginx/html8001

root@web1 nginx-1.22.1# echo web1-nginx-80 > /usr/local/nginx/html/index.html

root@web1 nginx-1.22.1# echo web1-nginx-8001 > /usr/local/nginx/html8001/index.html

使用web1测试

root@web1 nginx-1.22.1# curl 192.168.99.100

web1-nginx-80

root@web1 nginx-1.22.1# curl 192.168.99.100:8001

web1-nginx-8001

2)为web2搭建nginx

root@web2 \~# systemctl disable --now httpd

root@web2 \~# yum -y install make gcc openssl-devel pcre-devel #安装基础依赖包

root@web2 \~# tar -xf nginx-1.22.1.tar.gz

root@web2 \~# cd nginx-1.22.1/

root@web2 nginx-1.22.1# ./configure

root@web2 nginx-1.22.1# make && make install

root@web2 nginx-1.22.1# /usr/local/nginx/sbin/nginx

root@web2 nginx-1.22.1# echo web2-nginx-80 > /usr/local/nginx/html/index.html

3)使用proxy主机在nginx配置中创建集群

root@proxy python# killall uwsgi #停止uwsgi

root@proxy python# cd /usr/local/nginx

root@proxy nginx# cp conf/nginx.conf.default conf/nginx.conf #恢复配置文件

root@proxy nginx# vim conf/nginx.conf

http {

...

upstream default { #正常业务集群

server 192.168.99.100:80;

server 192.168.99.200:80;

}

upstream s8001 {

server 192.168.99.100:8001; #新版本业务集群1

}

server {

listen 80;

server_name localhost;

set group "default"; #自己定义变量group,值为default,访问default集群

if (remote_addr \~ "192.168.99.1"){ #如果客户机ip是包含192.168.99.1就访问新版本业务集群1,remote_addr为nginx的内置变量,代表访问者的ip

set $group s8001; #变量重新赋值

}

#charset koi8-r;

#access_log logs/host.access.log main;

location / {

proxy_pass http://$group; #调用集群

root html;

index index.html index.htm;

}

...

root@proxy nginx# /usr/local/nginx/sbin/nginx -s reload

使用proxy测试

root@proxy \~# curl 192.168.99.5 #显示正常业务集群

web1-nginx-80

root@proxy \~# curl 192.168.99.5 #显示正常业务集群

web2-nginx-80

web1访问proxy

root@web1 \~# curl 192.168.99.5 #显示新版本业务集群1

web1-nginx-8001

三、通过不同用户ID测试灰度发布

实验要求

不同ID的客户访问相同代理时,可以看到不同集群主机的内容

部署LNPM环境(已操作)

proxy主机部署LNMP服务器相关软件(前面已经安装过,在此不在重复安装)

1)proxy主机修改Nginx配置文件(修改默认首页与动静分离)

root@proxy \~# cd /usr/local/nginx

root@proxy nginx# cp conf/nginx.conf.default conf/nginx.conf #恢复配置文件

root@proxy nginx# vim /usr/local/nginx/conf/nginx.conf

...

location ~ \.php$ {

root html;

fastcgi_pass unix:/run/php-fpm/www.sock;

fastcgi_index index.php;

include fastcgi.conf;

}

...

php-fpm的配置文件之前已经修改好,此时不用修改

启动LNMP服务器相关的服务

1)重新加载Nginx服务

root@proxy nginx# /usr/local/nginx/sbin/nginx -s reload

root@proxy nginx# ss -antlp | grep :80

2)启动MySQL服务(已经启动则不需要执行)

root@proxy nginx# systemctl start mariadb

root@proxy nginx# systemctl status mariadb

3)启动PHP-FPM服务(已经启动则不需要执行)

root@proxy nginx# systemctl start php-fpm

root@proxy nginx# systemctl status php-fpm

root@proxy nginx# cp /root/lnmp_soft/php_scripts/test.php /usr/local/nginx/html/

root@proxy nginx# curl 192.168.99.5/test.php #测试动态页面

<html>

<body>

This is HTML message

</br>

c is bigger</body>

</html>

相关推荐
爱喝水的鱼丶5 分钟前
SAP-ABAP:SAP基础数据校验工具开发系列博客(共5篇)第五篇:性能优化与上线运维:保障高并发场景下的工具稳定运行
运维·学习·性能优化·sap·abap·erp·经验交流
企服AI产品测评局37 分钟前
2026年Agent元年!深度解析实在Agent未来路线图:从自动化工具到全能数字员工的跃迁
运维·人工智能·ai·chatgpt·自动化
Leo.yuan39 分钟前
运维视角下的数据同步工具选型指南:2026年主流方案功能对比
运维
秋漓41 分钟前
Nginx学习与应用
运维·学习·nginx
TDengine (老段)42 分钟前
TDengine 数据修复与迁移 — VGroup 调度、S3 外挂与运维操作
大数据·运维·数据库·物联网·时序数据库·iot·tdengine
m0_737302581 小时前
读懂OpenClaw:新一代开源自主AI智能体的革新与价值
服务器
小白学大数据1 小时前
爬虫优化:Python 剔除无效超时代理实操
服务器·爬虫·python
utf8mb4安全女神1 小时前
shell中的判断语法
linux·运维·服务器
iDao技术魔方1 小时前
WSL 配 GPU 用 Docker 的折腾指南(2026 年版)
运维·docker·容器
2601_950368911 小时前
稀土合金粉末采购指南:3步筛选靠谱镁钆供应商
大数据·运维·人工智能·python