Getting Started with Openresty Sep 1, 2019 Felix Zhao 中文 514 words ~3 min read Views: This post was written in 2019. Tools and versions mentioned may be outdated, though the underlying ideas still hold.
Getting Started with Openresty All the following practices were completed on CentOS.
Installation
Edit the configuration file
Configuration file content:
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") } } } }
Add to environment variables
I’m using source ~/.zshrc here, just use your own shell config file.
Let’s use http_load for a quick load test 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
Example 1: Openresty Log Response
Let’s take a look at conf/logresponse.conf
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" ; } } }
Example 2: Openresty with Redis
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; } } }
Other use cases include creating canary release environments and dynamic routing.
Example 3: Openresty with lua-aho-corasick First install the dependency yum install lua-devel, then git clone https://github.com/cloudflare/lua-aho-corasick && make && make install
After installation, configure the files and write your logic in the Lua script.
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; } } }
You can combine this with Redis to store some rules and use the aho-corasick algorithm for matching. Done.
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)
After reading this, do you understand what else Openresty can do?
Troubleshooting
Besides content_by_lua_file, there are also these:
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; }
File permission issues 755 user root root; (not a good choice, you should use your own group instead of root) Path completion issues Check the error log in time and verify the path is correct.
Resources