标签:lua

0

4516

在nginx中收集请求日志,异步归总到消息队列(nsq)

最近公司的服务器经常因为磁盘被日志文件写爆,磁盘不可用后导致接口请求报错,为此经常要定期删除服务器日志以腾出磁盘空间。有没有更好的办法解决磁盘容量太小,日志保存不能太多、时间不能太长的麻烦呢?我试想过,将接口日志收收集后转储到公司内部服务器,做离线存储方案,这样有三个好处:

1、海量接口日志不用再占用线上服务器的有限磁盘空间,节省费用;

2、在负载均衡多节点的部署架构下,避免日志文件的分散导致的查阅不方便;

3、归总转储离线后,日志可以更长时间的存储,本地集中查阅高效,为进一步分析提供了可能性。 比如:收集日志后,可以统计每日访问的pu、pv,可以分析每个接口请求频次;但业务操作存在bug时,可以借助日志分析问题原因……

但如何转储呢?在网上查阅了一些资料,有人利用kafka消息队列+lua,集中存储nginx请求日志到kafka中。

最近刚好在研究nsq,借此做一个基于nsq+lua的nginx日志转储方案,如下!

一、资源下载:

依赖:

https://github.com/openresty/lua-cjson/archive/2.1.0.6.tar.gz

https://github.com/openresty/lua-resty-core/archive/v0.1.13.tar.gz

lua-nsq:

https://github.com/rainingmaster/lua-resty-nsq

注意将Makefile文件里修改lua相关参数 :

#LUA_INCLUDE_DIR ?= $(PREFIX)/include
#LUA_LIB_DIR ?=     $(PREFIX)/lib/lua/$(LUA_VERSION)

LUA_INCLUDE_DIR ?= $(PREFIX)/include/luajit-2.0
LUA_LIB_DIR ?=     $(PREFIX)/lib/lua/5.1

二、nginx站点server里配置如下:

body_filter_by_lua '
    local resp_body = string.sub(ngx.arg[1], 1, 1000)
    ngx.ctx.buffered = (ngx.ctx.buffered or"") .. resp_body
';
log_by_lua_file /data/service/tengine/conf/lua/log.lua;

三、log.lua脚步如下:

……

乐果   发表于   2018 年 07 月 24 日 标签:lua 继续阅读

0

4537

nginx+lua动态修改upstream实现动态代理

家里有台迷你主机作为家庭服务器,但是在公司或者其他地方想实时访问,以前常有的解决办法是用域名动态dns解析即 “花生壳” 来实现的,但问题是当家里的宽带ip发生变化时,“花生壳”虽然已经更新了dns的解析,但由于下游dns服务缓存的影响,生效会延迟,这样就存在临界时间段无法访问的问题。

最近,学习nginx+lua实现动态代理,即在外网服务器上搭一个代理,当家里ip发生改变时通知外网的代理服务;用户访问外网代理服务,间接访问家庭服务器时,已经知道ip发生变化了,这样即实现了无缝隙间断的访问~

nginx配置案例如下:

lua_shared_dict _ups_zone 64m;

server {
        listen       80;
        server_name  test.cn;
        access_log  off;

        location = /ups_update {
                default_type text/plain;
                content_by_lua '
                        local ups = ngx.req.get_uri_args()["ups"]
                        if ups == nil then
                                ngx.say("usage: /ups_update?ups=x.x.x.x")
                                return
                        end
                        local host = ngx.var.http_host
                        local ups_from = ngx.shared._ups_zone:get(host);
                        --ngx.log(ngx.WARN, host, " update upstream from ", ups_from, " to ", ups)
                        ngx.shared._ups_zone:set(host, ups);
                        ngx.say(host, " update upstream from ", ups_from, " to ", ups)
                ';
        }

        location / {
                set_by_lua $cur_ups '
                        local ups = ngx.shared._ups_zone:get(ngx.var.http_host)
                        if ups ~= nil then
                                --ngx.log(ngx.ERR, "get [", ups, "] from ngx.shared._ups_zone")
                                return ups
                        end
                '
                proxy_next_upstream off;
                proxy_set_header    Host $host;
                proxy_http_version  1.1;
                proxy_set_header    Connection  "";
                proxy_pass $scheme://$cur_ups;
        }
}

乐果   发表于   2017 年 09 月 30 日 标签:nginxlua 继续阅读

0

2425

lua_nginx_module常见使用文档

安装lua_nginx_module 模块

lua_nginx_module 可以一步步的安装,也可以直接用淘宝的OpenResty

Centos和debian的安装就简单了。。

这里说下freebsd的安装:

fetch http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zxvf lua-5.1.4.tar.gz
cd lua-5.1.4
make freebsd
make install
cd ..
fetch https://github.com/chaoslawful/lua-nginx-module/zipball/v0.1.6rc2
fetch https://github.com/simpl/ngx_devel_kit/zipball/v0.2.17rc2
tar zxvf v0.1.6rc2
mv chaoslawful-lua-nginx-module-ccaf132 lua_nginx_module
tar zxvf v0.2.17rc2
mv simpl-ngx_devel_kit-bc97eea ngx_devel_kit
tar zxvf pcre-8.12.tar.gz
tar zxvf nginx-1.0.3.tar.gz
cd nginx-1.0.3
./configure --prefix=/data/soft/nginx --with-pcre=../pcre-8.12 --add-module=../ngx_devel_kit --add-module=../lua_nginx_module
make && make install

安装完成后,我们体验一下lua

第一个lua脚本

ngx.say 是打印的打印输出的意思。。。

location /echo {
    default_type text/plain;
    echo hello lua;
}
location /lua {
    default_type text/plain;
    content_by_lua 'ngx.say("hello world")';
}

用lua脚本做nginx的访问的限制…

……

乐果   发表于   2017 年 09 月 29 日 标签:luanginx 继续阅读

0

4456

nginx+lua+openresty动态二维码生成方案

最近,公司系统与阿里体育对接,在门票业务上我们需要返回二维码图片给对方(可能线下过闸机识别的需要),有几个方案可以选择,公司后端技术栈都集中在php,但用php动态生成效率不高,于是考虑用golang,网上也有一些类库包:github.com/skip2/go-qrcode,但后来发现有人用nginx扩展即实现了,所以我决定也尝试下,顺便熟悉下lua、openresty。

一、安装nginx_lua_module以及echo-nginx-module

Ngx_lua手动编译进Nginx。

首先,我的 Nginx 安装路径为:/usr/local/nginx。

我将尝试编译的两个模块:echo,lua。

所需要的模块如下:

luajit             http://luajit.org 
lua                http://www.lua.org 
ngx_devel_kit      https://github.com/simpl/ngx_devel_kit 
echo-nginx-module  https://github.com/agentzh/echo-nginx-module 
lua-nginx-module   https://github.com/chaoslawful/lua-nginx-module  

前期准备工作

1、安装nginx,版本必须大于1.6,如果已经安装了nginx,则需要重新编译安装下,把conf备份下。

……

乐果   发表于   2017 年 09 月 13 日 标签:nginxlua 继续阅读

热评文章