🥷
🥷
文章目录
  1. Openresty 入门
    1. 安装
    2. 示例一: Openresty Log Response
    3. 示例二: Openresty with redis
    4. 示例三: Openresty with lua-aho-corasick
  2. 遇到的问题排查
  3. Resources

Openresty入门

Openresty 入门

以下实践均在Centos下完成

安装

image

1.编辑配置文件

image

配置文件内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
worker_processes  1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 4996;
location / {
default_type text/html;
content_by_lua_block {
ngx.say("I am here")
}
}
}
}

2.加入环境变量

image

我这里是source ~/.zshrc ,依照自己的shell的配置文件即可

image

3.让我们使用http_load来压测一下看看

1
2
3
4
5
6
wget -c http://soft.kwx.gd/tools/http_load-12mar2006.tar.gz
tar -xz http_load-12mar2006.tar.gz
cd http_load-12mar2006
make
echo http://127.0.0.1:4996 > url
./http_load -p 20 -s 5 url

image

示例一: Openresty Log Response

image

让我们看一下conf/logresponse.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
log_format log_req_resp '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" $request_time req_body:"$request_body" resp_body:"$resp_body"';

server {
listen 4996;
access_log logs/access.log log_req_resp;

lua_need_request_body on;

set $resp_body "";
body_filter_by_lua '
local resp_body = string.sub(ngx.arg[1], 1, 1000)
ngx.ctx.buffered = (ngx.ctx.buffered or "") .. resp_body
if ngx.arg[2] then
ngx.var.resp_body = ngx.ctx.buffered
end
';

location / {
echo "I am here!! Log ME Now";
}
}
}

示例二: Openresty with redis

image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24


user root root;
worker_processes 2;

error_log logs/error.log;
events {
worker_connections 1024;
}

http {
server {
listen 4996;
location /by_lua_file {
default_type text/html;
content_by_lua_file conf/lua/iamhere.lua;
}

location /with_redis {
content_by_lua_file conf/lua/redis.lua;
}

}
}

其他应用还可以,创建灰度发布环境,以及动态路由。

示例三: Openresty with lua-aho-corasick

首先安装依赖yum install lua-devel, 然后git clone https://github.com/cloudflare/lua-aho-corasick && make && make install

image

安装之后,进行文件配置,同时在lua脚本之中编写逻辑即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

user root root;
worker_processes 2;

error_log logs/error.log;
events {
worker_connections 1024;
}

http {
server {
listen 4996;

location /with_aho {
content_by_lua_file conf/lua/aho_corasick.lua;
}

}
}

当然你可以配合redis存储一些规则,并用aho-corasick算法进行匹配。 Done

1
2
3
4
5
6
7
8
local ac = require "ahocorasick"

local dict = {"string1", "string", "etc"}
local acinst = ac.create(dict)
local r = ac.match(acinst, "etc")

ngx.say("match result: ", r)

image

告诉我看到这,你明白了Openresty还可以做什么吗?

遇到的问题排查

image

除了content_by_lua_file 还有以下这些:

1
2
3
4
5
6
location /lua {
rewrite_by_lua_file /path/to/rewrite.lua;
access_by_lua_file /path/to/access.lua;
content_by_lua_file /path/to/content.lua;
}

  1. 文件权限问题
  • 755
  • user root root; (不是一个好的选择,应该采用自己的组,而不是采用root的组)
  1. 路径补全问题
  • 及时查看error的log,然后判断路径是不是正确。

image

Resources