学习openresty的log_by_lua*和stream-lua-nginx-module模块

1.实例:

log.lua文件

local cjson = require "cjson.safe"
local logger = require "resty.logger.socket"

if not logger.initted() then
    local ok, err = logger.init {
        host = "127.0.0.1", --部署时,需要填写logserver服务器ip
        port = 1234,--部署时,需要填写logserver服务器port
        sock_type = "tcp",--采用udp传输
        flush_limit = 1,--1就是时实传输,
        --drop_limit = 5678, --默认1mb 超过会自动删除
        timeout = 10000,--超时设置
        pool_size = 100 --连接池大小
    }
    if not ok then
        ngx.log(ngx.ERR, "failed to initialize the logger: ", err)
        return
    end
end

client_ip = ngx.var.remote_addr
local args = ngx.req.get_post_args(10)
if not args then
    ngx.say("failed to get post args")
    return 
end
-- ngx.req.read_body()
 
logstr = cjson.encode(args).."\n"

local bytes, err = logger.log(logstr)
if err then
    ngx.log(ngx.ERR, "failed to log message: ", err)
    return
end

日志阶段的配置文件

server{

   
    lua_code_cache on;#lua代码缓存,开发期间为off,上线后改为on
    lua_need_request_body on;
    listen 8088;
    location /v2{
    	
        content_by_lua_block {
            ngx.say(1);
       }

      log_by_lua_file /Users/guanguan/workspace/lua.api.stream/log.lua;

    }

    

     access_log   /usr/local/openresty/nginx/logs/upgrade_access.log  main;
     error_log    /usr/local/openresty/nginx/logs/upgrade_error.log  debug;

}   

2.stream-lua-nginx-module模块的代码

stream.lua文件

local cjson = require "cjson.safe"
local sock = assert(ngx.req.socket(true))
sock:settimeout(1000)  -- one second timout
local data = sock:receive('*l')
ngx.log(ngx.ERR,"服务端接收数据:",data)

 

receive有几种方式,读取固定字节数,读取行(*l),读取到所有(到关闭连接),读取任意数据马上返回(*b,需要patch),此种还未合并到master分支,需要编译:https://github.com/openresty/stream-lua-nginx-module/pull/33

stream配置文件


#user  root;
worker_processes  8;

error_log  logs/error.log;
error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    #use epoll;
    worker_connections  1024;
}

stream {
    # define a TCP server listening on the port 1234:
   
    server {
        listen 1234;

        content_by_lua_file /Users/guanguan/workspace/lua.api.stream/stream.lua;

        error_log    /usr/local/openresty/nginx/logs/stream_error.log  debug;
        } 
   
}


http {
    include       mime.types;
    default_type  application/octet-stream;
    lua_code_cache off;


    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;
    error_log   logs/error.log   error;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;


    #server {
     #   listen       80;
      #  server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

       # location / {
        #    root   html;
         #   index  index.html index.htm;
        #}
        #location /v1{
         #  default_type  application/json;
	  #     content_by_lua_block{
           #    ngx.say("hello!");
           #}
        #}
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
       # error_page   500 502 503 504  /50x.html;
        #location = /50x.html {
         #   root   html;
        #}

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
   # }

    #include /usr/local/openresty/nginx/conf/vhosts/*.conf;
    include /usr/local/openresty/nginx/conf/vstream/*.conf;

}

 

性能如下:

转载于:https://my.oschina.net/u/2263272/blog/809431

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值