CentOS7安装Nginx (包含日志记录功能)

# Nginx lua

相关的安装包和安装文档可在https://download.csdn.net/download/cnzyyh/10424281 下载!

需求描述


在Nginx的日志里面获取请求数据,如果Nginx里使用了proxy_pass可以根据Nginx自带的$request_body获取.其他方式要借用nginx-lua的功能.

软件信息:

  • CentOS 7
  • Nginx 1.10.3

安装


1.安装依赖

如系统已存在下面依赖,请选择升级,而不是强制安装.

使用yum安装依赖

yum -y install gcc gcc-c++ zlib zlib-devel openssl openssl-devel pcre pcre-devel

不建议使用以下命令强制安装

不能使用外网的情况,请预先下载相关依赖,使用rpm安装(测试环境)

rpm -ivh rpms/*.rpm --force

2. 安装luajit

[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

3. 模块ngx_devel_kit

[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

4. 模块lua-nginx-module

[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

5. 安装Nginx

[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

浏览器访问

visit

Nginx常用命令


停止Nginx: nginx -s stop 

检查Nginx配置: nginx -t

重新加载Nginx: nginx -s reload
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值