119人参与 • 2024-11-28 • 软件测试
实验要求
要求不同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的客户访问相同代理时,可以看到不同集群主机的内容
部署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分别输入不同名称的账户,可以看到"开始"连接的是不同的地址
实验要求
不同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>
到此这篇关于nginx配置实现用ip灰度测试(不同用户id)的文章就介绍到这了,更多相关nginx ip灰度测试内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论