1.安装LuaJIT-2.0.4

链接:

http://luajit.org/download.html

tar -xf LuaJIT-2.0.4.tar.gz

cd LuaJIT-2.0.4

make PREFIX=/usr/local/luajit

make install PREFIX=/usr/local/luajit

2.修改环境变量

vim /etc/profile

export LUAJIT_LIB=/usr/local/luajit/lib

export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0

source /etc/profile

3.下载nginx lua模块

ngx_devel_kit 链接:https://github.com/simpl/ngx_devel_kit/tags

lua-nginx-module 链接:https://github.com/openresty/lua-nginx-module/tags

4.编译nginx 增加支持模块

cd /tmp/soft/

tar -xf ngx_devel_kit-0.3.0.tar.gz

tar -xf lua-nginx-module-0.10.5.tar.gz

4.1.编译nginx

tar -xf nginx-1.9.14.tar.gz

cd nginx-1.9.14

./configure \

--prefix=/app/local/nginx \

--pid-path=/app/local/nginx \

--user=nginx \

--group=nginx \

--with-threads \

--with-file-aio \

--with-http_ssl_module \

--with-http_v2_module \

--with-http_addition_module \

--with-http_sub_module \

--with-http_dav_module \

--with-http_flv_module \

--with-http_mp4_module \

--with-http_perl_module \

--with-mail \

--with-http_gzip_static_module \

--with-http_auth_request_module \

--with-http_random_index_module \

--with-http_secure_link_module \

--with-http_degradation_module \

--with-http_slice_module \

--with-http_stub_status_module \

--with-http_perl_module \

--with-zlib=/tmp/soft/zlib-1.2.8 \

--with-stream \

--with-stream_ssl_module \

--with-pcre=/tmp/soft/pcre-8.37 \

--with-openssl=/tmp/soft/openssl-1.0.2 \

--with-libatomic \

--add-module=/tmp/soft/ngx_log_if-master \

--add-module=/tmp/soft/ngx_devel_kit-0.3.0 \

--add-module=/tmp/soft/lua-nginx-module-0.10.5

make

5.备份原有的程序及增加链接

mv /app/local/nginx/sbin/nginx /app/local/nginx/sbin/nginx.0729

cp ./objs/nginx /app/local/nginx/sbin/

ln -s /usr/local/lib/libpcre.so.1 /lib64/libpcre.so.1

ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2

6.查看模块

# /app/local/nginx/sbin/nginx -V

nginx version: nginx/1.9.14

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC)

built with OpenSSL 1.0.2 22 Jan 2015

TLS SNI support enabled

configure arguments: --prefix=/app/local/nginx --pid-path=/app/local/nginx --user=nginx --group=nginx --with-threads --with-file-aio --with-http_ssl_module --with-http_v2_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_perl_module --with-mail --with-http_gzip_static_module --with-http_auth_request_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_slice_module --with-http_stub_status_module --with-http_perl_module --with-zlib=/tmp/soft/zlib-1.2.8 --with-stream --with-stream_ssl_module --with-pcre=/tmp/soft/pcre-8.37 --with-openssl=/tmp/soft/openssl-1.0.2 --with-libatomic --add-module=/tmp/soft/ngx_log_if-master --add-module=/tmp/soft/ngx_devel_kit-0.3.0 --add-module=/tmp/soft/lua-nginx-module-0.10.5

7.nginx 修改为json格式

修改很简单,如下:

log_format ng_json '{'

'"http_cdn_src_ip":"$http_cdn_src_ip",'

'"time_local": "$time_local",'

'"request":"$request",'

'"status":"$status",'

'"body_bytes_sent":"$body_bytes_sent",'

'"request_body":"$request_body",'

'"content_length":"$content_length",'

'"http_referer":"$http_referer",'

'"http_user_agent":"$http_user_agent",'

'"http_x_forwarded_for":"$http_x_forwarded_for",'

'"remote_addr":"$remote_addr",'

'"upstream_response_time":"$upstream_response_time",'

'"request_time":"$request_time",'

'"http_x_trace_code":"$http_x_trace_code"}

}';

查看生成的json:

{

"http_cdn_src_ip": "-",

"time_local": "29/Jul/2016:03:01:02 +0800",

"request": "GET /q.gif?platform=pc&category=player&action=bufferEmpty&t=1469732461170&loc=/star/3459038 HTTP/1.1",

"status": "200",

"body_bytes_sent": "43",

"request_body": "-",

"content_length": "-",

"http_referer": "http://www.ckl.com/ckl/3459038",

"http_user_agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 BIDUBrowser/7.5 Safari/537.36",

"http_x_forwarded_for": "-",

"remote_addr": "117.174.247.112",

"upstream_response_time": "-",

"request_time": "0.000",

"http_x_trace_code": "-"

}

发现,request 字段依然不是json格式,如何修改,nginx测试无法实现(自己无法实现)所以修改了,增加lua脚本,不通过nginx记录日志

而是直接使用lua来记录,lua脚本如下:

#cd /app/local/nginx/lua/

#vim parse.lua

local args = {}

args = ngx.req.get_uri_args()

local v_prev = ""

local sp = "\""

for key,val in pairs(args) do

if key == nil or val == nil then

else

v_prev = v_prev .. sp ..  key .. sp .. ":" .. sp .. val .. sp .. ","

end

end

function isnil(value)

if value == nil then

value = "-"

end

return value

end

local logContent = ""

if v_prev ~= nil then

local http_cdn_src_ip = isnil(ngx.var.http_cdn_src_ip)

local time_local = isnil(ngx.var.time_local)

local status = isnil(ngx.var.status)

local body_bytes_sent = isnil(ngx.var.body_bytes_sent)

local request_body = isnil(ngx.var.request_body)

local content_length = isnil(ngx.var.content_length)

local http_referer = isnil(ngx.var.http_referer)

local http_user_agent = isnil(ngx.var.http_user_agent)

local http_x_forwarded_for = isnil(ngx.var.http_x_forwarded_for)

local remote_addr = isnil(ngx.var.remote_addr)

local upstream_response_time = isnil(ngx.var.upstream_response_time)

local request_time = isnil(ngx.var.request_time)

local http_x_trace_code = isnil(ngx.var.http_x_trace_code)

logContent = "{\"http_cdn_src_ip\":" .. sp .. http_cdn_src_ip .. sp .. ",\"time_local\":" .. sp .. time_local .. sp  .. ",\"request\":" ..  "{" .. string.sub(v_prev,1,#v_prev-1) ..  "}" .. ",\"status\":" .. sp .. status .. sp .. ",\"body_bytes_sent\":" .. sp .. body_bytes_sent .. sp .. ",\"request_body\":" .. sp .. request_body .. sp .. ",\"content_length\":" .. sp .. content_length .. sp .. ",\"http_referer\":" .. sp .. http_referer .. sp .. ",\"http_user_agent\":" .. sp .. http_user_agent .. sp .. ",\"http_x_forwarded_for\":" .. sp .. http_x_forwarded_for .. sp .. ",\"remote_addr\":" .. sp .. remote_addr .. sp .. ",\"upstream_response_time\":" .. sp .. upstream_response_time .. sp .. ",\"request_time\":" .. sp .. request_time .. sp .. ",\"http_x_trace_code\":" .. sp .. http_x_trace_code .. sp .. "}"

local file = io.open("/app/data/log/nginx/ckl_access.log","a")

local hc = "\n"

file:write(logContent)

file:write(hc)

file:close()

end

ngx.status = 200

ngx.exit(0)

修改nginx配置:

增加如下:

location ~ /ckl.gif {

set $logContent "";

default_type text/plain;

content_by_lua_file "/app/local/nginx/lua/parse.lua";

}

重启nginx 生效,这个必须重启

/etc/init.d/nginx restart

查看新的json

{

"http_cdn_src_ip": "-",

"time_local": "01/Aug/2016:02:01:02 +0800",

"request": {

"t": "1469988090580",

"category": "player",

"action": "bufferEmpty",

"platform": "pc",

"loc": "/v/2955653"

},

"status": "000",

"body_bytes_sent": "0",

"request_body": "-",

"content_length": "-",

"http_referer": "http://www.ckl.com/v/2955653",

"http_user_agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36",

"http_x_forwarded_for": "-",

"remote_addr": "221.226.105.101",

"upstream_response_time": "-",

"request_time": "0.000",

"http_x_trace_code": "-"

}

nginx lua以html显示,nginx 支持lua及lua脚本格式化日志相关推荐

  1. 启动代码格式:nginx安装目录地址 -c nginx配置文件地址

    启动 启动代码格式:nginx安装目录地址 -c nginx配置文件地址 例如: [root@LinuxServer sbin]# /usr/local/nginx/sbin/nginx -c /us ...

  2. linux裸机安装nginx,linux环境下安装nginx步骤 - 进击的乌龟 - 博客园

    开始前,请确认gcc g++开发类库是否装好,默认已经安装. ububtu平台编译环境可以使用以下指令 apt- get install build- essential apt - get inst ...

  3. 用lua扩展你的Nginx(写的非常好)

    一. 概述 Nginx是一个高性能,支持高并发的,轻量级的web服务器.目前,Apache依然web服务器中的老大,但是在全球前1000大的web服务器中,Nginx的份额为22.4%.Nginx采用 ...

  4. 用lua扩展你的Nginx(整理)

    首先得声明.这不是我的原创,是在网上搜索到的一篇文章,原著是谁也搞不清楚了.按风格应该是属于章亦春的文章. 整理花了不少时间,所以就暂写成原创吧. 一. 概述 Nginx是一个高性能.支持高并发的,轻 ...

  5. nginx文件服务器美化autoindex显示

    网络上很多使用nginx来作为文件服务器的,前段时间公司也有这个需求,就研究了一下使用nginx来做文件服务器,参见前面两篇文章: 容器中使用ngnix搭建支持上传下载的文件服务器 容器中使用ngin ...

  6. 解决nginx+php二级页面显示空白的问题

    迁移官网商城的发现主页的二级界面显示空白页,此https商城网站 后修改nginx配置,显示正常 添加 location ~ [^/]\.php(/|$) { #fastcgi_pass remote ...

  7. nginx 修改html header,nginx 安全笔记 (修改nginx的header信息和错误显示版本号)

    nginx 安全笔记 (修改nginx的header信息和错误显示版本号) 发布时间:2020-08-07 16:18:18 来源:51CTO 阅读:1189 作者:674591788 随笔记载,欢迎 ...

  8. Nginx SSL模块配置提供HTTPS支持(Ngx_http_ssl_module)

    Ngx_http_ssl_module:此模块为Nginx提供HTTPS支持: 官方文档:http://nginx.org/en/docs/http/ngx_http_ssl_module.html ...

  9. Nginx执行php,显示“No input file specified. ”的处理方法

    使用apt-get install nginx和php-cgi 配置好nginx和php,如果配置的请看网上教程,很多. 在/var/www/nginx-default中放上一份phpinfo.php ...

最新文章

  1. 用计算机怎么算个人所得税,个税计算器2021
  2. 您为了什么而学?【一入红尘深似海 勿负天下有心人】
  3. 怎样格式化电脑_电脑硬盘故障恢复软件:坏硬盘数据怎么恢复?
  4. python偶数个数_python基础
  5. lightoj1027(期望dp)
  6. Swing basic
  7. ssh服务及安全配置
  8. oracle update范例,oracle 12c单范例数据库打12.1.0.2.4补丁记录
  9. webpack4开始使用
  10. 李宏毅机器学习day2
  11. Docker-07:Docker网络管理
  12. python对数正态分布函数_python生成具有上下限的截断对数正态分布
  13. python|面向对象(一)
  14. Word2003快速操作技巧及常用快捷键使用
  15. VB不能加载MSCOMCTL.OCX所需文件
  16. upsert----非标准DML语句
  17. python制作个人名片_python制作名片
  18. 学军OJ题解——1179 约会
  19. RGB 常用颜色对照表
  20. oracle函数(in/out)

热门文章

  1. .Net Core应用搭建的分布式邮件系统设计
  2. 从工程转向管理,访谈Github公司的Phil Haack
  3. [转]如何进行单元测试
  4. C#趣味程序---爱因斯坦的台阶问题
  5. Android之error: ‘const struct JNINativeInterface‘ has no member named ‘callVoidMethod‘
  6. linux之查看文件大小和磁盘大小总结
  7. Android之Base64
  8. 一篇文学会商用可编辑问卷表单制作【iVX 十二】
  9. [python opencv 计算机视觉零基础到实战] 十三 直方图颜色提鲜
  10. 主成分分析步骤_多元分析(1)--主成分分析