openresty配置的一些问题总结!
环境
-
win10下的linux子系统ubuntu(wsl)
-
openresty版本:ngx_openresty-1.7.7.2.tar.gz
wsl安装
请移步 {% post_link wsl笔记 %}
openresty安装
或者参考 {% post_link openresty安装笔记 %}
openssl版本问题
需要指定旧版本,推荐在编译 Nginx 时指定 OpenSSL 源码目录,而不是使用系统自带的版本,这样更可控。
Lua模块安装
-
安装 LuaRocks
LuaRocks: Lua 的模块安装和部署工具
apt-get install luarocks
-
安装 luasocket
lua 远程调试引入的
require("mobdebug").start()
需要 socket注:Lua 的 Remote Debug 远程调试 现在还搞不定,此处占个坑。
luarocks install luasocket
openresty使用
nginx常用命令
-
启动:
指定配置文件
nginx -p `pwd`/ -c conf/nginx.conf
-
停止:
nginx指定配置文件的,停止时也需指定参数
nginx -p `pwd`/ -c conf/nginx.conf -s quit
nginx.conf 配置
-
正则匹配路径
模式 含义 location = /uri = 表示精确匹配,只有完全匹配上才能生效 location ^~ /uri ^~ 开头对URL路径进行前缀匹配,并且在正则之前。 location ~ pattern 开头表示区分大小写的正则匹配 location ~* pattern 开头表示不区分大小写的正则匹配 location /uri 不带任何修饰符,也表示前缀匹配,但是在正则匹配之后 location / 通用匹配,任何未匹配到其它location的请求都会匹配到,相当于switch中的default location ~ ^/api/([-_a-zA-Z0-9]+) { # 准入阶段完成参数验证 access_by_lua_file nginx_test_server/access_check.lua; #内容生成阶段 content_by_lua_file nginx_test_server/$1.lua; }
-
开发调试时取消缓存
# 这里设置为 off,是为了避免每次修改之后都要重新 reload 的麻烦。 # 在生产环境上务必确保 lua_code_cache 设置成 on。 lua_code_cache off;
-
http 模块 报错 bad argument #2 to 'set_keepalive' (number expected, got nil)
参考:bad argument #2 to 'set_keepalive' (number expected, got nil)的解决办法
在关联数组中多传一个参数keepalive=false 即
{ method = “GET”, path = requestBody, keepalive=false }
lua 的一些坑
-
openresty/lua-resty-redis 的 批量查询 返回值问题
A non-nil Redis "bulk reply" results in a Lua string as the return value. A nil bulk reply results in a
ngx.null
return value.A nil multi-bulk reply returns in a
ngx.null
value.如果使用批量查询如mget,查不到数据会返回 ngx.null
OpenResty缓存
需要先在 nginx.conf 里面的修改 ,这个 cache 是 Nginx 所有 worker 之间共享的
lua_shared_dict my_cache 128m;