# Nginx lua
相关的安装包和安装文档可在https://download.csdn.net/download/cnzyyh/10424281 下载!
在Nginx的日志里面获取请求数据,如果Nginx里使用了proxy_pass
可以根据Nginx自带的$request_body
获取.其他方式要借用nginx-lua
的功能.
软件信息:
- CentOS 7
- Nginx 1.10.3
如系统已存在下面依赖,请选择升级,而不是强制安装.
使用yum
安装依赖
yum -y install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel
不建议使用以下命令强制安装
不能使用外网的情况,请预先下载相关依赖,使用rpm
安装(测试环境)
rpm -ivh rpms/*.rpm --force
[root@dev2 luajit]# wget -c http://luajit.org/download/LuaJIT-2.0.4.tar.gz
[root@dev2 luajit]# tar -xvf LuaJIT-2.0.4.tar.gz
[root@dev2 luajit] cd LuaJIT-2.0.4
[root@dev2 LuaJIT-2.0.4]# make install PREFIX=/usr/local/luajit
安装成功的相关提示
==== Successfully built LuaJIT 2.0.4 ==+
......
==== Successfully installed LuaJIT 2.0.4 to /usr/local/luajit ====
添加系统环境变量
[root@dev2 LuaJIT-2.0.4]# vim /etc/profile
# export for luajit
export LUAJIT_LIB=/usr/local/luajit/lib
export LUAJIT_INC=/usr/local/luajit/include/luajit-2.0
[root@dev2 LuaJIT-2.0.4]# source /etc/profile
[root@dev2 ngx-devel-kit]# wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz
[root@dev2 ngx-devel-kit]# tar -xzvf v0.3.0.tar.gz
[root@dev2 ngx-devel-kit]# wget https://github.com/openresty/lua-nginx-module/archive/v0.10.8.tar.gz
[root@dev2 ngx-devel-kit]# tar -xzvf v0.10.8.tar.gz
[root@dev2 nginx]# wget http://nginx.org/download/nginx-1.10.3.tar.gz
[root@dev2 nginx]# tar -xvf nginx-1.10.3.tar.gz
[root@dev2 nginx]# cd nginx-1.10.3/
修改Nginx
源代码,解决log
日志中文乱码问题 修改文件:nginx-1.10.3/src/http/modules/ngx_http_log_module.c
static uintptr_t
ngx_http_log_escape(u_char *dst, u_char *src, size_t size)
{
ngx_uint_t n;
/* 修改这里 */
/*static u_char hex[] = "0123456789ABCDEF";*/
static uint32_t escape[] = {
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
/* ?>=< ;:98 7654 3210 /.-, +*)( '&%$ #"! */
0x00000004, /* 0000 0000 0000 0000 0000 0000 0000 0100 */
/* _^]\ [ZYX WVUT SRQP ONML KJIH GFED CBA@ */
0x10000000, /* 0001 0000 0000 0000 0000 0000 0000 0000 */
/* ~}| {zyx wvut srqp onml kjih gfed cba` */
0x80000000, /* 1000 0000 0000 0000 0000 0000 0000 0000 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
0xffffffff, /* 1111 1111 1111 1111 1111 1111 1111 1111 */
};
if (dst == NULL) {
/* find the number of the characters to be escaped */
n = 0;
while (size) {
if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
n++;
}
src++;
size--;
}
return (uintptr_t) n;
}
/* 修改这里 */
while (size) {
/* if (escape[*src >> 5] & (1 << (*src & 0x1f))) {
*dst++ = '\\';
*dst++ = 'x';
*dst++ = hex[*src >> 4];
*dst++ = hex[*src & 0xf];
src++;
} else {
*dst++ = *src++;
}*/
*dst++=*src++;
size--;
}
return (uintptr_t) dst;
}
安装
[root@dev2 nginx-1.10.3]# ./configure --prefix=/usr/local/nginx --add-module=/home/dev/nginx/ngx-devel-kit/ngx_devel_kit-0.3.0 --add-module=/home/dev/nginx/lua-nginx-module/lua-nginx-module-0.10.8
[root@dev2 nginx-1.10.3]# make -j2
[root@dev2 nginx-1.10.3]# make install
- --prefix 指定nginx安装路径
- --add-module 添加模块,以解压实际路径为准
编辑conf/nginx.conf
#user nobody;
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$request_body'; --> 主要
sendfile on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
lua_need_request_body on; --> 主要
server {
listen 80;
server_name 10.10.2.101;
charset utf-8; --> 设置编码
location / {
root html;
index index.html index.htm;
}
location /hello { -->主要
default_type 'text/plain';
content_by_lua '
ngx.say("success")
ngx.req.read_body()
';
access_log logs/access.log main;
}
}
使用nginx -t
检查配置文件是否正确
出现异常:sbin/nginx: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
解决方法
[root@dev2 nginx]# ln -s /usr/local/luajit/lib/libluajit-5.1.so.2 /lib64/libluajit-5.1.so.2
启动Nginx
[root@dev2 nginx]#sbin/nginx
浏览器访问
停止Nginx: nginx -s stop
检查Nginx配置: nginx -t
重新加载Nginx: nginx -s reload