由于使用了nginx+lua模块,这种方式适合很多场景,非常强大,但是问题是你可能需要学习很多lua的语法。
方式三.使用http头信息判断+权重(灰度值)
http请求传输过程中,会自动带上User-Agent,Host,Referer,Cookie等信息。我们只需要判断ip地址段,用户代理,Cookie中的信息等。我们这里以Cookie为例。
当然,这里需要解决两个问题:
①首次访问静态页面可能不会产生cookie
②我们需要通过代码动态设置路由
③通过weight控制灰度值
我们可以通过一个例子来解决上述中的②与③的问题
upstream tts_V6 {
server 192.168.3.81:5280 max_fails=1 fail_timeout=60;
}
upstream tts_V7 {
server 192.168.3.81:5380 max_fails=1 fail_timeout=60;
}
upstream default { #通过upstream default + weight节点控制权重
server 192.168.3.81:5280 max_fails=1 fail_timeout=60 weight=5;
server 192.168.3.81:5380 max_fails=1 fail_timeout=60 weight=1;
}
server {
listen 80;
server_name test.taotaosou.com;
access_log logs/test.taotaosou.com.log main buffer=32k;
#match cookie
set $group "default";
if ($http_cookie ~* "tts_version_id=tts1"){ #动态控制路由
set $group tts_V6;
}
if ($http_cookie ~* "tts_version_id=tts2"){
set $group tts_V7;
}
location / {
proxy_pass http://$group;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
index index.html index.htm;
}
}
对于问题①,我们可以在index页面通过script来访问动态页面:
如
<script src="https://test.taotaosou.com/cookieinfo.php" /><script>
此外,我们还要在cookieinfo.php中判断和生成cookie
<?php
if(!session_id())
{
session_start();
}
if(!isset($_COOKIE["tts_version_id"]))
{
$cookieValue = $_SERVER['SERVER_PORT']==5280?"tts1":"tts2";
setcookie("tts_version_id", $cookieValue, time()+3600, "/");
}
?>
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!








