Nginx配置實(shí)現(xiàn)用IP灰度測(cè)試(不同用戶ID)
一.配置Nginx實(shí)現(xiàn)用IP測(cè)試灰度發(fā)布
實(shí)驗(yàn)要求
要求不同IP的客戶訪問相同代理時(shí),可以看到不同集群主機(jī)的內(nèi)容
環(huán)境準(zhǔn)備
主機(jī)名 IP地址 角色
proxy(已存在) eth1:192.168.99.5/24 代理服務(wù)器
web1(已存在) eth1:192.168.99.100/24 web服務(wù)器
web2(已存在) eth1:192.168.99.200/24 web服務(wù)器
創(chuàng)建不同集群,準(zhǔn)備多臺(tái)集群主機(jī)
通過$remote_addr變量識(shí)別不同客戶機(jī)
1)為web1搭建nginx虛擬主機(jī)
[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 #取消開機(jī)自啟,停止httpd服務(wù)
[root@web1 ~]# yum -y install make gcc openssl-devel pcre-devel #安裝基礎(chǔ)依賴包
[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 #默認(rèn)server listen 80的不要?jiǎng)?,直接另外寫一個(gè)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測(cè)試
[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 #安裝基礎(chǔ)依賴包 [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主機(jī)在nginx配置中創(chuàng)建集群
[root@proxy python]# killall uwsgi #停止uwsgi
[root@proxy python]# cd /usr/local/nginx
[root@proxy nginx]# cp conf/nginx.conf.default conf/nginx.conf #恢復(fù)配置文件
[root@proxy nginx]# vim conf/nginx.conf
http {
...
upstream default { #正常業(yè)務(wù)集群
server 192.168.99.100:80;
server 192.168.99.200:80;
}
upstream s8001 {
server 192.168.99.100:8001; #新版本業(yè)務(wù)集群1
}
server {
listen 80;
server_name localhost;
set $group "default"; #自己定義變量$group,值為default,訪問default集群
if ($remote_addr ~ "192.168.99.1"){ #如果客戶機(jī)ip是包含192.168.99.1就訪問新版本業(yè)務(wù)集群1,$remote_addr為nginx的內(nèi)置變量,代表訪問者的ip
set $group s8001; #變量重新賦值
}
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://$group; #調(diào)用集群
root html;
index index.html index.htm;
}
...
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload使用proxy測(cè)試
[root@proxy ~]# curl 192.168.99.5 #顯示正常業(yè)務(wù)集群 web1-nginx-80 [root@proxy ~]# curl 192.168.99.5 #顯示正常業(yè)務(wù)集群 web2-nginx-80
web1訪問proxy
[root@web1 ~]# curl 192.168.99.5 #顯示新版本業(yè)務(wù)集群1 web1-nginx-8001
二、通過不同用戶ID測(cè)試灰度發(fā)布
實(shí)驗(yàn)要求
不同ID的客戶訪問相同代理時(shí),可以看到不同集群主機(jī)的內(nèi)容
部署LNPM環(huán)境(已操作)
proxy主機(jī)部署LNMP服務(wù)器相關(guān)軟件(前面已經(jīng)安裝過,在此不在重復(fù)安裝)
1)proxy主機(jī)修改Nginx配置文件(修改默認(rèn)首頁與動(dòng)靜分離)
[root@proxy ~]# cd /usr/local/nginx
[root@proxy nginx]# cp conf/nginx.conf.default conf/nginx.conf #恢復(fù)配置文件
[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的配置文件之前已經(jīng)修改好,此時(shí)不用修改
啟動(dòng)LNMP服務(wù)器相關(guān)的服務(wù)
1)重新加載Nginx服務(wù)
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload [root@proxy nginx]# ss -antlp | grep :80
2)啟動(dòng)MySQL服務(wù)(已經(jīng)啟動(dòng)則不需要執(zhí)行)
[root@proxy nginx]# systemctl start mariadb [root@proxy nginx]# systemctl status mariadb
3)啟動(dòng)PHP-FPM服務(wù)(已經(jīng)啟動(dòng)則不需要執(zhí)行)
[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 #測(cè)試動(dòng)態(tài)頁面 <html> <body> This is HTML message </br> c is bigger</body> </html>
4) 配置好lnmp之后,拷貝帶登錄效果的測(cè)試頁面
[root@proxy nginx]# cd /root/lnmp_soft/php_scripts/ [root@proxy nginx]# tar -xf php-session-demo.tar.gz #釋放帶登錄功能的網(wǎng)頁 [root@proxy nginx]# cp -r php-session-demo/* /usr/local/nginx/html/ #拷貝頁面到nginx中
使用瀏覽器訪問192.168.99.5/index.php 可以看到有登錄界面的網(wǎng)頁
如果想要訪問此網(wǎng)站時(shí)直接輸入http://192.168.99.5就能訪問到這個(gè)登錄頁面,可以更改以下配置實(shí)現(xiàn)
[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就能訪問到登錄頁面,輸入任意用戶名和密碼登錄測(cè)試即可
注:這里index.php是登錄前頁面 ,home.php是登錄后才能看的頁面
修改home.php頁面
1)修改home.php頁面
[root@proxy php_scripts]# vim /usr/local/nginx/html/home.php #修改php頁面,將原有Welcome那行修改成以下狀態(tài)
Welcome : <?php
if(preg_match("/^abc/",$_SESSION['login_user'])) { #preg_match匹配正則,如果登錄賬號(hào)是以abc開頭,就連接99.100,否則連接99.200
echo "<a ;
}
else
{
echo "<a ;
}
?>2)測(cè)試
瀏覽器訪問192.168.99.5/index.php分別輸入不同名稱的賬戶,可以看到"開始"連接的是不同的地址
三、通過不同用戶ID測(cè)試灰度發(fā)布
實(shí)驗(yàn)要求
不同ID的客戶訪問相同代理時(shí),可以看到不同集群主機(jī)的內(nèi)容
部署LNPM環(huán)境(已操作)
proxy主機(jī)部署LNMP服務(wù)器相關(guān)軟件(前面已經(jīng)安裝過,在此不在重復(fù)安裝)
1)proxy主機(jī)修改Nginx配置文件(修改默認(rèn)首頁與動(dòng)靜分離)
[root@proxy ~]# cd /usr/local/nginx
[root@proxy nginx]# cp conf/nginx.conf.default conf/nginx.conf #恢復(fù)配置文件
[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的配置文件之前已經(jīng)修改好,此時(shí)不用修改
啟動(dòng)LNMP服務(wù)器相關(guān)的服務(wù)
1)重新加載Nginx服務(wù)
[root@proxy nginx]# /usr/local/nginx/sbin/nginx -s reload [root@proxy nginx]# ss -antlp | grep :80
2)啟動(dòng)MySQL服務(wù)(已經(jīng)啟動(dòng)則不需要執(zhí)行)
[root@proxy nginx]# systemctl start mariadb [root@proxy nginx]# systemctl status mariadb
3)啟動(dòng)PHP-FPM服務(wù)(已經(jīng)啟動(dòng)則不需要執(zhí)行)
[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 #測(cè)試動(dòng)態(tài)頁面 <html> <body> This is HTML message </br> c is bigger</body> </html>
到此這篇關(guān)于Nginx配置實(shí)現(xiàn)用IP灰度測(cè)試(不同用戶ID)的文章就介紹到這了,更多相關(guān)Nginx IP灰度測(cè)試內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解使用Nginx和uWSGI配置Python的web項(xiàng)目的方法
這篇文章主要介紹了使用Nginx和uWSGI配置Python的web項(xiàng)目的方法,與其他CGI連接方式相比uwsgi的連接性能也較為出眾,需要的朋友可以參考下2015-12-12
Nginx轉(zhuǎn)發(fā)丟失cookie表現(xiàn)形式及解決方案
本文主要介紹了Nginx轉(zhuǎn)發(fā)丟失cookie表現(xiàn)形式及解決方案,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01
一文詳解Nginx的強(qiáng)緩存和協(xié)商緩存
這篇文章主要為大家詳細(xì)介紹了Nginx中強(qiáng)緩存和協(xié)商緩存的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-03-03
nginx+lua(openresty)實(shí)現(xiàn)黑/白名單權(quán)限控制的示例
本文介紹了如何使用Openresty進(jìn)行權(quán)限控制和灰度發(fā)布,具體通過定時(shí)器定期更新黑名單數(shù)據(jù),進(jìn)行用戶過濾和權(quán)限管控,具有一定的參考價(jià)值,感興趣的可以了解一下2024-09-09
nginx中的location路徑配置(實(shí)路徑和虛路徑)
這篇文章主要介紹了nginx中的location路徑配置(實(shí)路徑和虛路徑),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06

