openresty ngx_lua获取环境变量


openresty ngx_lua获取环境变量

        

             

                               

环境变量

         

ngx.config.subsystem:当前请求的nginx环境

语法格式:subsystem = ngx.config.subsystem
* 如果在http模块下,返回字符串http
* 如果在stream模块下,返回字符串stream

环境:init_by_lua*、init_worker_by_lua*、log_by_lua*、ngx.timer.*、
     set_by_lua*、rewrite_by_lua*、content_by_lua*、access_by_lua*、
     header_filter_by_lua*、body_filter_by_lua*

              

ngx.config.debug:是否是调试模式

语法格式:debug = ngx.config.debug
* 判断请求是否在调试模式下执行
* 如果需要在调试模式下执行某些操作,可以用该语句进行判断

环境:init_by_lua*、init_worker_by_lua*、log_by_lua*、ngx.timer.*、
     set_by_lua*、rewrite_by_lua*、content_by_lua*、access_by_lua*、
     header_filter_by_lua*、body_filter_by_lua*

                                          

ngx.config.nginx_version:获取nginx的版本号

语法格式:version = ngx.config.nginx_version
* 获取nginx的版本号

环境:init_by_lua*、init_worker_by_lua*、log_by_lua*、ngx.timer.*、
     set_by_lua*、rewrite_by_lua*、content_by_lua*、access_by_lua*、
     header_filter_by_lua*、body_filter_by_lua*

            

ngx.config.ngx_lua_version:获取ngx_lua的版本号

语法格式:version = ngx.config.ngx_lua_version
* 获取ngx_lua的版本号

环境:init_by_lua*、log_by_lua*、ngx.timer.*、access_by_lua*、
     set_by_lua*、rewrite_by_lua*、content_by_lua*、
     header_filter_by_lua*、body_filter_by_lua*

               

ngx.config.prefix:获取编译nginx时指定的prefix参数

语法格式:prefix = ngx.config.prefix()
* 获取编译nginx指定的参数--prefix
* 如果nginx启动时使用了-p,则输出-p

环境:init_by_lua*、init_worker_by_lua*、log_by_lua*、ngx.timer.*、
     set_by_lua*、rewrite_by_lua*、content_by_lua*、access_by_lua*、
     header_filter_by_lua*、body_filter_by_lua*

             

ngx.config.nginx_configure:获取编译nginx时,configure设置的参数

语法格式:configure = ngx.config.nginx_configure()
* 获取编译nginx时,configure指定的参数

环境:init_by_lua*、log_by_lua*、ngx.timer.*、access_by_lua*、
     set_by_lua*、rewrite_by_lua*、content_by_lua*、
     header_filter_by_lua*、body_filter_by_lua*

                        

ngx.worker.id:获取worker进程的id

语法格式:id = ngx.worker.id()
* 获取worker进程的id

环境:init_by_lua*、log_by_lua*、ngx.timer.*、access_by_lua*、
     set_by_lua*、rewrite_by_lua*、content_by_lua*、
     header_filter_by_lua*、body_filter_by_lua*

             

ngx.worker.count:获取worker进程的数量

语法格式:count = ngx.worker.count()
* 获取worker进程的数量,nginx配置文件中worker_processes的数量

环境:init_by_lua*、init_worker_by_lua*、log_by_lua*、ngx.timer.*、
     set_by_lua*、rewrite_by_lua*、content_by_lua*、access_by_lua*、
     header_filter_by_lua*、body_filter_by_lua*

                          

ngx.worker.exiting:判断worker进程是否退出

语法格式:exiting = ngx.worker.exiting()
* 判断worker进程是否退出

环境:init_by_lua*、init_worker_by_lua*、log_by_lua*、ngx.timer.*、
     set_by_lua*、rewrite_by_lua*、content_by_lua*、access_by_lua*、
     header_filter_by_lua*、body_filter_by_lua*

         

               

                               

使用示例

         

default.conf

server {
    listen       80;
    server_name localhost;

    location /test {
        content_by_lua_block {
            ngx.say("当前请求的nginx环境 ==> ",ngx.config.subsystem);
            ngx.say("是否是调试模式 ==> ",ngx.config.debug);
            ngx.say("nginx版本号 ==> ",ngx.config.nginx_version);
            ngx.say("ngx_lua版本号 ==> ",ngx.config.ngx_lua_version);

            ngx.say("nginx prefix ==> ",ngx.config.prefix());
            ngx.say("nginx configure ==> ",ngx.config.nginx_configure());

            ngx.say("nginx worker进程id ==> ",ngx.worker.id());
            ngx.say("nginx worker_processes ==> ",ngx.worker.count());
            ngx.say("nginx worker进程是否退出 ==> ",ngx.worker.exiting());
        }
    }

    #error_page  404              /404.html;

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/local/openresty/nginx/html;
    }

}

        

创建容器

docker run -it -d -p 2003:80 \
-v /Users/huli/lua/openresty/conf8/default.conf:/etc/nginx/conf.d/default.conf \
--name open6 lihu12344/openresty

              

使用测试

huli@hudeMacBook-Pro conf8 % curl localhost:2003/test
当前请求的nginx环境 ==> http
是否是调试模式 ==> false
nginx版本号 ==> 1021004
ngx_lua版本号 ==> 10021
nginx prefix ==> /usr/local/openresty/nginx/
nginx configure ==>  --prefix=/usr/local/openresty/nginx --with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl111/include' --add-module=../ngx_devel_kit-0.3.1 --add-module=../echo-nginx-module-0.62 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.33 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.09 --add-module=../srcache-nginx-module-0.32 --add-module=../ngx_lua-0.10.21 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.9 --add-module=../ngx_stream_lua-0.0.11 --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl111/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl111/lib' --with-cc='ccache gcc -fdiagnostics-color=always' --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_v2_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-compat --with-stream --with-http_ssl_module
nginx worker进程id ==> 0
nginx worker_processes ==> 1
nginx worker进程是否退出 ==> false

          

                 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值