Openresty 实现灰度发布
2.1. 安装 Openresty
CentOS
wget https://openresty.org/package/centos/openresty.repo -O /etc/yum.repos.d/openresty.repo
RockyLinux
wget https://openresty.org/package/rocky/openresty.repo -O /etc/yum.repos.d/openresty.repo 或者 yum-config-manager --add-repo https://openresty.org/package/rocky/openresty.repo
安装
dnf install -y openresty
2.1.1. 源码安装
dnf install pcre-devel openssl-devel gcc zlib-devel
cd /usr/local/src wget https://openresty.org/download/openresty-1.21.4.1.tar.gz tar zxvf openresty-1.21.4.1.tar.gz cd openresty-1.21.4.1/ ./configure --prefix=/srv/openresty-1.21.4.1 gmake gmake install ln -s /srv/openresty-1.21.4.1 /srv/openresty
2.2. Openresty 开发
2.2.1. Hello world!!!
[root@netkiller openresty]# vim /srv/openresty/nginx/conf/nginx.conf server { listen 8080; location / { default_type text/html; content_by_lua_block { ngx.say("<p>Hello world!!!</p>") } } }
重载配置文件
[root@netkiller openresty]# bin/openresty -s reload
测试效果
[root@netkiller openresty]# curl http://www.netkiller.cn <p>Hello world!!!</p>
2.2.2. 日期和时间
当前时间
[root@netkiller lua]# cat time.lua print(os.time()) [root@netkiller lua]# lua time.lua 1670496310
日期
[root@netkiller lua]# cat date.lua local d1 = os.date("%Y-%m-%d %H:%M:%S") print(d1) [root@netkiller lua]# lua date.lua 2022-12-08 19:07:45
[root@netkiller lua]# cat date.lua local d1 = os.date("%Y-%m-%d %H:%M:%S") print(d1) local d2 = os.date("%Y-%m-%d %H:%M:%S", os.time()) print(d2) [root@netkiller lua]# lua date.lua 2022-12-08 19:12:11 2022-12-08 19:12:11
2.2.3. 数据结构
2.2.3.1. list 列表
[root@netkiller lua]# cat list.lua local fruits = { "apple", "orange", "pear", "banana" } for _, fruit in pairs(fruits) do if fruit == "pear" then print("We Found it!") else print("Oh no, keep traversing!") end end [root@netkiller lua]# lua list.lua Oh no, keep traversing! Oh no, keep traversing! We Found it! Oh no, keep traversing!
2.2.4. echo 输出
location /a { echo "A"; } location /b { echo "B"; }
2.2.5. 参数处理
2.2.5.1. 获取 GET/POST 参数
location /param {