1.實例:php
log.lua文件html
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
日誌階段的配置文件nginx
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模塊的代碼git
stream.lua文件github
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/33json
stream配置文件api
#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; }
性能以下:緩存