lua require 搜索路径指定lua_package_path方法
如果是一个 *.LUA
的文件, 里面用到了自己写的库, 或者第三方写的库, 但是你不想把它放到 lua 的安装目录里, 则在代码里面可以指定require
搜索的路径。
package.path = '/usr/local/share/lua/5.1/?.lua;/home/resty/?.lua;' --搜索lua模块
package.cpath = '/usr/local/lib/lua/5.1/?.so;' --搜索so模块
如果是要在 nginx.conf
文件中引用第三方的库,则需要在 http 段中添加下面的代码
lua_package_path '/usr/local/share/lua/5.1/?.lua;/home/resty/?.lua;';
lua_package_cpath '/usr/local/lib/lua/5.1/?.so;';
示例代码:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
lua_package_path '/opt/openresty/nginx/html/?.lua;/opt/openresty/nginx/html/lua/?.lua;/opt/openresty/nginx/html/api-lua/?.lua;;';
server {
listen 80;
server_name localhost;
lua_code_cache off;
location / {
root html/vue/dist;
try_files $uri $uri/ /index.html$is_args$query_string;
}
location /api-lua/ {
default_type text/html;
content_by_lua_file html/api-lua/index.lua;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}