½ËÕ×ðÁú¿Ê±Ò»Ö±¸ß¶ÈÖØÊÓÊÖÒÕÁ¢Ò죬¶¦Á¦´ó¾ÙÍƶ¯½¹µãÊÖÒÕÑз¢£¬Îª¸ü¶à¿Í»§Ìṩ»ùÓÚ×ÔÉí½¹µã¾ºÕùÁ¦µÄÆóÒµ¼¶Êý¾ÝÇå¾²²úÆ·¡£
±¾ÎÄÖ÷Òª¹Ø×¢»Ò¶ÈÐû²¼ÖеÄWebÇ°¶Ë»Ò¶ÈµÄ·ÖÁ÷Õ½ÂÔ¡£
ÏÖÔÚÖ÷Á÷µÄ¼Ü¹¹ÊÇÇ°ºó¶ËÊèÉ¢¼Ü¹¹£¬Ç°¶ËÖ÷Òª½ÓÄÉNginx¾ÙÐÐÐû²¼¡£¹ØÓÚÇ°¶ËµÄÁ÷Á¿Çл»ºÍ·ÖÁ÷Ò²ºÜ³ÉÊ죬±¾ÎÄÏÈÈݼ¸ÖÖ³£¼ûµÄ·ÖÁ÷¹æÔòÕ½ÂÔ£¬²¢ÓÃNginxµÄÓï·¨À´ÑÝʾÉèÖÃʵÏÖ¡£
»Ò¶ÈÐû²¼µÄ»®·Ö
ÈôÊÇƾ֤Ðû²¼¶ËÀ´·ÖµÄ»°£¬»Ò¶ÈÐû²¼¿ÉÒÔ·ÖΪWebÇ°¶Ë»Ò¶È¡¢¿Í»§¶Ë»Ò¶ÈºÍЧÀͶ˻Ҷȼ¸ÖÖ¡£
ÎÞÂÛÊÇÄÄÖֻҶȼƻ®£¬Ò»Ñùƽ³£¶¼ÐèÒªÖª×ãÒÔÏÂ2¸öÒªÇó£º
Ò»¸öÒªÇóÊÇÁ÷Á¿·ÖÁ÷Öð²½Çл»µÄÉèÖ㬸ø²úÆ·Ö°Ô±¡¢ÔËÓª»òÔËάµÈÊÂÇéÖ°Ô±ÉèÖÃÕâ¸öÕ½ÂÔ¡£
ÁíÒ»¸öÒªÇóÊÇͳһ¸öÓû§Ê¼ÖÕ»á¼ûµÄÊÇͳһ¸ö°æ±¾µÄ´úÂë¶ÔÓ¦µÄϵͳ¡£
»ùÓÚURL·ÖÁ÷
»ùÓÚURL·ÖÁ÷µÄÔÀíÊÇÈÃNginxƾ֤ÇëÇóURLµÄ²î±ð£¬½ø¶ø½«ÇëÇóת·¢µ½²î±ðµÄЧÀÍÆ÷ÉÏ¡£
ÀýÈ磺
url http://192.168.2.200/login/0001/
url http://192.168.2.200/login/0002/
ÐèÇóÊǽ«µÇ¼ÇëÇóºó׺Ϊ0001µÄÓû§ÇëÇóת·¢µ½»Ò¶ÈЧÀÍÆ÷ÉÏ£¬½«µÇ¼ÇëÇóºó׺Ϊ0002µÄÓû§ÇëÇóת·¢µ½Éú²úЧÀÍÆ÷ÉÏ¡£
ÑÝʾ´úÂëÈçÏ£º
map $zone $up_stream {
^~0001 frontends_0001;
^~0002 frontends_0002;
default frontends_0001;
}
»ùÓÚCOOKIE·ÖÁ÷
»ùÓÚCookie·ÖÁ÷µÄÔÀíΪ£ºÔÚÓû§Ê״εǼʱÅÌÎʸÃÓû§ÊÇ·ñÊǻҶÈÓû§£¬²¢ÎªÆäÉèÖñêʶCookie£¬ºóÐø½ÓÄÉCookie±êʶÀ´¾ÙÐзÖÁ÷¡£
±¾ÎĽÓÄÉgray×Ö¶Î×÷Ϊ±êʶ£¬ÈôÊÇcookieÖÐgrayµÄֵΪtrueÔò·ÖÁ÷µ½»Ò¶ÈÇéÐΣ¬ÆäËûÇéÐÎÔò·ÖÁ÷µ½Éú²úÇéÐΡ£
ʵÏÖ´úÂëÈçÏ£º
upstream normal {
server 192.168.2.200:9000;
}
upstream tag {
server 192.168.2.200:9001;
}
server {
listen 8080;
server_name host_nginx;
set $group normal;
if ($http_cookie ~* "gray=true"){
set $group gray;
}
}
»ùÓÚHeader·ÖÁ÷
»ùÓÚHeader·ÖÁ÷µÄÔÀíÀàËÆcookie£¬»ñÈ¡»Ò¶È±êʶʹÓõÄÊÇ$http_headerд·¨¡£
Header²»¿ÉÏñCookieÒ»ÑùÔÚ¿Í»§¶Ë±£´æ£¬ÒÔÊÇÒ»Ñùƽ³£ÊÇÔÚ¶à²ãÍøÂçÖÐʹÓ㬼´Èë¿Ú²ã¸øHeader¸³Öµ£¬·ÖÁ÷²ãÔÙÈ¥·ÖÁ÷¡£
ʵÏÖ´úÂëÈçÏ£º
map $http_tag $group {
~*true$ gray;
default normal;
»ùÓÚIP·ÖÁ÷
»ùÓÚIP·ÖÁ÷µÄÔÀíÊÇУÑé¿Í»§¶ËµÄIPÊÇ·ñÔÚ×ðÁú¿Ê±»Ò¶ÈIPÁбíÖС£ÎÒÃÇ¿ÉÒÔ½ÓÄÉMySQL±í´æ´¢ÐèÒª·Ö·¢µ½»Ò¶ÈÇéÐεÄIPµØµã£¬ÈôÊÇÇëÇóIPÔÚ±íÖÐÔò·ÖÁ÷µ½»Ò¶ÈÇéÐΡ£
ÔÚNginxÄÚÀïʹÓÃluaÐèÒªNginxÌØÊâÔöÌílua-nginx-moduleÄ£¿é£¬ÔÚNginxÖÐÅþÁ¬mysqlµÄÒªÁì²Î¿¼lua-resty-mysqlÄ£¿é¡£
location @dark {
content_by_lua 'ngx.say("gray")';
}
location @normal {
content_by_lua 'ngx.say("normal")';
}
location /test {
access_by_lua '
local mysql = require "resty.mysql"
local db, err = mysql:new()
if not db then
xxx...
end
local ok, err, errcode, sqlstate = db:connect {
xxx...
}
if not ok then
ngx.log(ngx.ERR, "failed to connect: ", err, ": ", errcode, " ", sqlstate)
ngx.exec("@normal")
end
local req_ip = ngx.var.http_x_real_ip or ngx.var.http_x_forwarded_for or ngx.var.remote_addr or "0.0.0.0"
local name = ngx.unescape_uri(req_ip)
local quoted_name = ngx.quote_sql_str(name)
local res, err, errcode, sqlstate = db:query(var_sql)
if not res then
xxx...
end
if tonumber(res[1]["cnt"]) > 0 then
ngx.exec("@gray")
end
ngx.exec("@normal")
';
}
»ùÓÚNginx IngressʵÏÖ
KubernetesÏ¿ÉÒÔʹÓÃNginx IngressʵÏÖ»Ò¶ÈÐû²¼ÊÊÓó¡¾°Ö÷Ҫȡ¾öÓÚÓªÒµÁ÷Á¿ÇзֵÄÕ½ÂÔ£¬ÏÖÔÚNginx IngressÖ§³Ö»ùÓÚHeader¡¢CookieºÍЧÀÍȨÖØÈýÖÖÁ÷Á¿ÇзֵÄÕ½ÂÔ£¬»ùÓÚÕâЩսÂÔ¿ÉÒÔʵÏÖÒÔÏÂ2ÖÖ³¡¾°£º
»ùÓÚHeader»òCookieÇзֲ¿·ÖÓû§Á÷Á¿µ½Ð°汾£º
»ùÓÚЧÀÍȨÖØÇзÖÒ»¶¨±ÈÀýµÄÁ÷Á¿µ½Ð°汾£º
С½á
×ÛÉÏËùÊö£¬Ç°¶Ë»Ò¶ÈÐû²¼·ÖÁ÷µÄ·½·¨ÓÐÐí¶à£¬ÎÒÃÇÐèҪƾ֤ӪҵµÄÇéÐΡ¢ÊÖÒÕ´¢±¸ÇéÐΡ¢ÓªÒµÏµÍ³µÄÓû§ÈºµÈ¶à¸ö·½ÃæÀ´×ÛºÏ˼Á¿£¬È·¶¨Ò»¸ö×îÊʺϵÄʵÏÖ·½·¨¡£
¹Ø×¢Êý¾Ý °ü¹ÜδÀ´
ɨÂë£ü¹Ø×¢ÎÒÃÇ
΢Ðźţü½ËÕ×ðÁú¿Ê±
꿅ᣟhttp://www.bwda.net