Upstream.conf
Upstream grey_1 { keepalive; Server localhost:8020;} Upstream grey_2{ keepalive; Server localhost:8021;}
8020 conf
#vhost confserver{
Index index.php index.html index.do; Root/var/html/webroot/grey_1; Access_log "/data/logs/nginx/access.log"; Error_log "/data/logs/nginx/error.log" warn; Location/{ if ' (!-e $request _filename) { rewrite ^/(. *)/index.php last; } } Location ~ \. (Php|do) ($|/) { Fastcgi_pass 127.0.0.1:9000; Fastcgi_param script_filename $document _root$fastcgi_script_name; Include Fastcgi_params; Set $path _info $request _uri; if ($request _uri ~ ^/(v\d) (\/.*) (\?. *) ($) { set $path _info; break; } Fastcgi_param path_info $path _info; } }
8021 conf
#vhost confserver{ Listen 8021; Index index.php index.html index.do; Root/var/html/webroot/grey_2 Access_log "/data/logs/nginx/access.log"; Error_log "/data/logs/nginx/error.log" warn; Location/{ if ' (!-e $request _filename) { rewrite ^/(. *)/index.php last; } } Location ~ \. (Php|do) ($|/) { Fastcgi_pass 127.0.0.1:9000; Fastcgi_param script_filename $document _root$fastcgi_script_name; Include Fastcgi_params; Set $path _info $request _uri; if ($request _uri ~ ^/(v\d) (\/.*) (\?. *) ($) { set $path _info; break; } Fastcgi_param path_info $path _info; } }
Nginx.conf
#vhost conf server{Listen 8000; Index index.php index.html index.do; Root/var/html/www/webroot/index; Access_log/data/logs/index_access.log; Error_log/data/logs/index_error.log warn; Lua_code_cache off; Location/lua {Content_by_lua ' Ngx.say ("Hello") '; } location/{if (!-e $request _filename) {rewrite ^/(. *)/index.php last; }} location/grey/{default_type ' text/plain '; Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for; Proxy_set_header Host $http _host; Proxy_set_header X-real-ip $remote _addr; Proxy_set_header Connection ""; Proxy_http_version 1.1; Set $backend "Grey_1"; Rewrite_by_lua_file Conf/lua/grey.proxy.lua; Proxy_passhttp:/$backend/? $args; }
Grey.proxy.lua
Local require " Greyrelease.grey " local upstream = Grey.get_upstream_by_userid ()-- Ngx.say (upstream)= Upstream
Grey.lua
Localstrings =require "resty.string"Local _m= {}LocalHash_standard = -LocalHash_max_len =TenLocalDefault_stream ="grey_1"LocalSearch_stream ="grey_2"LocalHash_map = { --grey_0 = {range_start=0,range_end=9,flow= "Grey1"},Grey_0 = {range_start=0, range_end=9, flow="Grey1"}, Grey_1= {range_start=Ten, range_end= +, flow="Grey1"}, Grey_2= {range_start= -, range_end= in, flow="Grey1"}, Grey_3= {range_start= -, range_end= the, flow="Grey1"}, Grey_4= {range_start= +, range_end= the, flow="Grey1"}, Grey_5= {range_start= -, range_end= -, flow="Grey1"}, Grey_6= {range_start= -, range_end= the, flow="Grey1"}, Grey_7= {range_start= -, range_end= -, flow="Grey1"}, Grey_8= {range_start= the, range_end= the, flow="Grey1"}, Grey_9= {range_start= -, range_end= About, flow="Grey1"},}function _m. Get_upstream_by_userid ()ifNgx.var.arg_grey_test = ="grey_1" Then returnSearch_streamElseIfNgx.var.arg_grey_test = ="grey_2" Then returnDefault_streamEnd LocalUser_id=ngx.var.arg_user_idLocalupstream=Default_streamifuser_id = =Nil Then returnUpstreamEnd LocalHex_string =Strings.to_hex (user_id)LocalStr_len =String.len(hex_string)LocalHash_string ="" ifStr_len > Hash_max_len Thenhash_string=string.sub(Hex_string,0, Hash_max_len)Elsehash_string=hex_stringEnd LocalInt_mod =Math.fmod(Hash_string,hash_standard) forKvinch Pairs(HASH_MAP) Do ifv['Range_start'] <= Int_mod andInt_mod < v['Range_end'] ThenUpstream= v['Flow'] Break End End returnUpstreamEndreturn _m
A simple gray-scale publishing system based on Nginx+lua