openresty 配置模块化
模块搜索路径
lua_package_path:lua模块搜索路径
语法格式:lua_package_path <lua-style-path-str>
环境:http
Sets the Lua module search path used by scripts specified
by set_by_lua, content_by_lua and others.
* 设置set_by_lua、content_by_lua等指令中的模块搜索路径
The path string is in standard Lua path form, and ;; can
be used to stand for the original search paths
* 设置的path是标准的lua格式
* ;; ==> 表示原始搜索路径
# 示例
lua_package_path "/usr/local/openresty/modules/?.lua;;" #绝对路径
lua_package_path "modules/?.lua;;" #相对路径
lua_package_path "${prefix}/modules/?.lua;;" #${prefix}表示编译时的--prefix参数
lua_package_cpath:c模块搜索路径
语法格式:lua_package_cpath <lua-style-cpath-str>
环境:http
Sets the Lua C-module search path used by scripts
specified by set_by_lua, content_by_lua and others.
* 设置set_by_lua、content_by_lua等指令中使用的c模块搜索路径
The cpath string is in standard Lua cpath form, and
;; can be used to stand for the original cpath
* cpath string是标准的lua cpath格式
* ;; ==> 表示原始搜索路径
配置模块
redis_config.lua
local config = {}
config.host = "172.18.0.81";
config.port = 6379
return config
nginx.conf
#user nobody;
#worker_processes 1;
pcre_jit on;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#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;
proxy_temp_path /var/run/openresty/nginx-proxy;
fastcgi_temp_path /var/run/openresty/nginx-fastcgi;
uwsgi_temp_path /var/run/openresty/nginx-uwsgi;
scgi_temp_path /var/run/openresty/nginx-scgi;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
# 添加自定义的模块搜索路径
lua_package_path "/usr/local/openresty/config/?.lua;;";
}
default.conf
server {
listen 80;
server_name localhost;
location / {
root /usr/local/openresty/nginx/html;
index index.html index.htm;
}
local /test {
content_by_lua_block {
local redis = require "resty.redis";
local config = require "redis.config";
local red = redis:new();
red:set_timeouts(1000, 1000, 1000);
local ok, err = red:connect(config.host, config.port);
if not ok then
ngx.say("red failed to connect: ", err)
return
end
ngx.say("red:set('1','gtlx')");
local res, err = red:set('1','gtlx');
if not res then
ngx.say("failed to set: ", err)
return
end
ngx.say("\nred:get('1')");
res, err = red:get('1');
if not res then
ngx.say("failed to get: ", err)
return
end
ngx.say("get读取结果 ==> ", res);
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/openresty/nginx/html;
}
}
创建容器
#redis
docker run -it -d --net fixed --ip 172.18.0.81 -p 6301:6379 --name redis redis
#openresty
docker run -it -d --net fixed --ip 172.18.0.82 -p 6004:80 \
-v /Users/huli/lua/openresty/config/redis_config.lua:/usr/local/openresty/config/redis_config.lua \
-v /Users/huli/lua/openresty/config/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf \
-v /Users/huli/lua/openresty/config/default2.conf:/etc/nginx/conf.d/default.conf \
--name open-config lihu12344/openresty
使用测试
huli@bogon config % curl localhost:6004/test
red:set('1','gtlx')
red:get('1')
get读取结果 ==> gtlx