openresty 安装
# cd /usr/local/src/
// 下载 openresty
# wget https://openresty.org/download/openresty-1.13.6.2.tar.gz
# tar xf openresty-1.13.6.2.tar.gz && cd openresty-1.13.6.2 && ./configure --prefix=/usr/local/openresty --with-luajit --with-pcre --with-http_iconv_module --with-http_realip_module --with-http_sub_module --with-http_stub_status_module --with-stream --with-stream_ssl_module && make build install
// 修改 nginx.conf 默认配置
# vim /usr/local/openresty/nginx/conf/nginx.conf
user nobody;
worker_processes 1;
error_log logs/error.log;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
client_max_body_size 20m;
gzip on;
include /usr/local/openresty/nginx/conf/conf.d/*.conf;
}
# vim /usr/lib/systemd/system/openresty.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target
[Service]
Type=forking
ExecStartPre=/usr/local/openresty/nginx/sbin/nginx -t
ExecStart=/usr/local/openresty/nginx/sbin/nginx
ExecReload=/usr/local/openresty/nginx/sbin/nginx -s reload
ExecStop=/usr/local/openresty/nginx/sbin/nginx -s stop
PrivateTmp=true
[Install]
WantedBy=multi-user.target
# mkdir -p /usr/local/openresty/nginx/conf/conf.d
# systemctl daemon-reload
# systemctl restart nginx
openresty lua-resty-cookie 实现灰度功能
# cd /usr/local/openresty/lualib/resty/
// 下载 lua-resty-cookie
# git clone https://github.com/cloudflare/lua-resty-cookie.git
// 把 cookie.lua 放在 /usr/local/openresty/lualib/resty/ 目录
# cp lua-resty-cookie/lib/resty/cookie.lua .
# ls /usr/local/openresty/lualib/resty/cookie.lua
/usr/local/openresty/lualib/resty/cookie.lua
# vim /usr/local/openresty/nginx/conf/conf.d/lua-gray.conf
server {
listen 8097;
server_name _;
access_log logs/lua.log main;
index index.html index.htm;
root /usr/local/openresty/nginx/html/;
location /normal {
content_by_lua_block {
local resty_cookie = require "resty.cookie"
local cookie, err = resty_cookie:new()
if not cookie then
ngx.log(ngx.ERR, err)
return
end
local fields, err = cookie:get_all()
if not fields then
ngx.exec("@normal")
end
for k, v in pairs(fields) do
if k == "grayVersion" and v == "2.4" then
-- ngx.say(k, " => ", v)
ngx.exec("@gray")
else
-- ngx.say(k, " => ", v)
ngx.exec("@normal")
end
end
}
}
location @normal {
root /data/static;
}
location @gray {
root /data/static/gray;
}
}
# systemctl reload nginx
// 网站资源目录结构
# cat /data/static/normal/index.html
normal
# cat /data/static/gray/normal/index.html
gray
// 测试灰度效果
# curl 127.0.0.1:8097/normal/
normal
# curl --cookie 'grayVersion=1.0' 127.0.0.1:8097/normal/
normal
# curl --cookie 'grayVersion=2.4' 127.0.0.1:8097/normal/
gray
网友评论