openresty 配置模块化


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

         

               

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值