学习openresty的log_by_lua*和stream-lua-nginx-module模块
2019独角兽企业重金招聘Python工程师标准>>>
1.实例:
log.lua文件
local cjson = require "cjson.safe"
local logger = require "resty.logger.socket"if not logger.initted() thenlocal ok, err = logger.init {host = "127.0.0.1", --部署时,需要填写logserver服务器ipport = 1234,--部署时,需要填写logserver服务器portsock_type = "tcp",--采用udp传输flush_limit = 1,--1就是时实传输,--drop_limit = 5678, --默认1mb 超过会自动删除timeout = 10000,--超时设置pool_size = 100 --连接池大小}if not ok thenngx.log(ngx.ERR, "failed to initialize the logger: ", err)returnend
endclient_ip = ngx.var.remote_addr
local args = ngx.req.get_post_args(10)
if not args thenngx.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 thenngx.log(ngx.ERR, "failed to log message: ", err)return
end
日志阶段的配置文件
server{lua_code_cache on;#lua代码缓存,开发期间为off,上线后改为onlua_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
学习openresty的log_by_lua*和stream-lua-nginx-module模块相关推荐
- nginx简介--理解nginx配置/模块/openresty
1. nginx功能和定位 Nginx最核心的功能是Web服务器和反向代理服务器. Web服务器完成对HTTP请求协议的解析和以HTTP协议格式响应请求.缓存.日志处理这些基本Web服务器功能: 反向 ...
- openresty开发系列24--openresty中lua的引入及使用
openresty开发系列24--openresty中lua的引入及使用 openresty 引入 lua 一)openresty中nginx引入lua方式 1)xxx_by_lua ---> ...
- LNMP架构——OpenResty实现缓存前移(到达Nginx前端层面)
前言 我们都知道Nginx有很多的特性和好处,但是在Nginx上开发成了一个难题,Nginx模块需要用C开发,而且必须符合一系列复杂的规则,最重要的用C开发模块必须要熟悉Nginx的源代码,使得开发者 ...
- Nginx学习之五:Nginx第三方模块
为什么80%的码农都做不了架构师?>>> 第三方模块 这些模块不被官方所支持,亦未必能跨越Nginx的版本兼容.不过其中有些模块证明是对多数用户是有用的.只在于个人的风险问题. ...
- openresty开发系列38--通过Lua+Redis 实现动态封禁IP
openresty开发系列38--通过Lua+Redis 实现动态封禁IP 一)需求背景为了封禁某些爬虫或者恶意用户对服务器的请求,我们需要建立一个动态的 IP 黑名单.对于黑名单之内的 IP ,拒绝 ...
- 学习OpenResty编程
1.Windows版本的下载位置 https://github.com/LomoX-Offical/nginx-openresty-windows Linux下OpenResty的下载和安装 http ...
- 【Ubuntu18.04】实现redis+lua+nginx动态黑名单封禁
必要包整合链接 nginx+lua+redis.rar-其它文档类资源-CSDN下载 nginxLuaRedis文件.rar-其它文档类资源-CSDN下载 安装必要环境 apt-get install ...
- 开始学习OpenResty
OpenResty下载地址 https://openresty.org/download/ngx_openresty-1.9.3.2.tar.gz Nginx增加模块需要重新编译. OpenResty ...
- CentOS 7 学习(一) 配置LAMP和Nginx
CentOS 7 学习(一) 配置LAMP和Nginx CentOS是RedHat Linux企业版的代码编译版本,属于比较通用的服务器Linux版本,据说Ubuntu Server更通用,呵呵,不过 ...
最新文章
- 一场“正宗”的开发者大会,为什么说微软更像是“AII in AI”了?
- 一些关于反汇编与逆向方面的博文分享
- 用 C 语言开发一门编程语言 — 条件分支
- mysql ddl dcl_MySQL常用DDL、DML、DCL语言整理(附样例)
- 转pdf wps_华为手机自带图片转PDF的功能?今天才知道,当了5年花粉白当了
- bat 变量 文件内容第一行_VBA基础入门(38)FSO生成bat文件后执行的实例
- 5G高频段频谱规划启动 大国博弈加剧
- gitee提交突然报错remote: Incorrect username or password ( access token )
- Django搭建登录注册功能
- 微信计步器怎么不计步_送我一顶圣诞帽@星尘StarDust,制作一个圣诞创意微信头像...
- 项目管理的流程及生命周期
- 红旗6.0 sp2 永中office2009不能启动的原因
- servlet是干什么的?
- 计算机技术在足球的应用,图象处理技术在足球机器人中的应用研究
- 【neutron】mitaka版本openstack网络之open vSwitch
- 《论英语能力从小开始培养的重要性,不一定是非要去国外》
- Dart语言编程基础
- 柔柔弱弱若若若若若若若若
- ios 13 全局修改 present 卡片式
- 实例10 等差数列求和
热门文章
- 【每日一题】剑指 Offer 10- I. 斐波那契数列
- android os被删除怎么办,手机系统应用误删了怎么办 如何修复手机异常【详细介绍】...
- Spring Boot 2.3 中开启Spring Security
- Handler消息传递机制
- 刚入行的小菜鸡,怎样做好功能测试?
- linux删除配置信息,Linux LVM逻辑卷配置过程详解(创建、扩展、缩减、删除、卸载、快照创建)...
- Note:一些优化建议
- creo 3.0计算机配置,Creo 3.0 Parametric 配置选项文件使用说明
- java jmx jboss_jboss中JMX的连接与Mbean的获取
- input file文件上传_微服务间的文件上传与下载-Feign