Nginx日志配置(Ngx_http_log_module)

本文介绍了Nginx的 ngx_http_log_module 模块,用于定义日志格式并高效保存日志。讨论了如何指定日志格式,包括常见的Nginx内置变量,如客户端地址、响应码等。同时,讲解了日志文件缓存的配置,如`open_log_file_cache`指令的参数,如`max`、`inactive`、`valid`和`min_uses`,以提高日志处理性能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Ngx_http_log_module:定义日志格式,并且以指定的格式保存

Syntaxaccess_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition];
access_log off;
Defaultaccess_log logs/access.log combined;
Contexthttp, server, location, if in location, limit_except
Syntaxlog_format name [escape=default|json] string …;
Defaultlog_format combined “…”;
Contexthttp
  • 指定日志格式:
#示例配置  
log_format compression '$remote_addr - $remote_user [$time_local] '
                       '"$request" $status $bytes_sent '
                       '"$http_referer" "$http_user_agent" "$gzip_ratio"';

access_log /spool/logs/nginx-access.log compression buffer=32k;
#access_log:访问日志文件路径;
#buffer=32;缓冲在内存中,32K的内存空间,日志先缓冲至内存中,根据时间节点,空间节点在存储至硬盘中(效率更高);

变量定义:多数为Nginx内建变量
$remote_addr:客户端地址;
$remote_user:客户端用户;
[$time_local]:收到用户请求时服务器本地时间;
$reques:请求的url;
$status:响应码;
$bytes_sent:发送客户端的响应报文字节数;
$http_referer:从什么地方跳转到当前页面资源;
$http_user_agent:客户端浏览器的类型;
$gzip_ratio:页面资源压缩比;
……

  • 缓存所打开的日志文件的元数据:
Syntaxopen_log_file_cache max=N [inactive=time] [min_uses=N] [valid=time];
open_log_file_cache off;
Defaultopen_log_file_cache off;
Contexthttp, server, location

相关选项:
max=N:设置缓存中描述符的最大数量;
inactive=time:设置缓存描述符关闭的时间(定义非活动时长,如果在此期间使用次数小于最小使用次数,定义非活动项目)默认10s;
min_uses=N:在由inactive参数定义的时间内文件使用的最小次数;
简单来讲:在inactive=time时间内,缓存项最少访问min_uses=N次,为活动项,否则为非活动;
valid=time:设置文件检查(每隔多久检查一次缓存项是否有效);
off:禁用缓存;

  • 演示环境:
Server192.168.47.140
[root@GaoServer ~]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@GaoServer ~]# uname -r
3.10.0-327.el7.x86_64
[root@GaoServer ~]# nginx -V
nginx version: nginx/1.10.2
......  
  • 相关配置/参数:(打开日志缓存加速日志性能)
#在http配置段,定义日志参数;注意,如果在server配置段中也同样定义日志参数,以server配置段中参数生效(最小定义);  
[root@GaoServer ~]# vim /etc/nginx/nginx.conf  
http {  
......  
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '    #main定义格式  
                      '$status $body_bytes_sent "$http_referer" '  
                      '"$http_user_agent" "$http_x_forwarded_for"';  

    access_log  /var/log/nginx/access.log  main;  
...... 

#定义不同虚拟主机使用不同的访问日志:  
[root@GaoServer ~]# vim /etc/nginx/conf.d/server.conf  
server {  
        listen 80;  
        server_name www.server1.com;  
        access_log /var/log/nginx/nginx_log/server1_access.log main;  
        location / {  
                root /data/nginx/server1;  
        }  
        error_page 404 =200     /404.html;  
        location = /404.html {  
                root /etc/nginx/error_pages/;  
        }  
}  

server {  
        listen 8080;  
        server_name www.server2.com;  
        access_log /var/log/nginx/nginx_log/server2_access.log main;  
        location / {  
                root /data/nginx/server2;  
        }  
}  
#创建日志存放文件上级目录:  
[root@GaoServer]# mkdir /var/log/nginx/nginx_log -pv  
mkdir: 已创建目录 "/var/log/nginx/nginx_log"  
[root@GaoServer ~]# nginx -t  
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok  
nginx: configuration file /etc/nginx/nginx.conf test is successful  
[root@GaoServer ~]# nginx -s reload  

#访问测试:  
[root@GaoServer ~]# curl 127.0.0.1  
server1  
[root@GaoServer ~]# curl 127.0.0.1:8080  
server2  
......  
[root@GaoServer ~]# cd /var/log/nginx/nginx_log/  
[root@GaoServer nginx_log]# ls  
server1_access.log  server2_access.log  
[root@GaoServer nginx_log]# tail server2_access.log   
127.0.0.1 - - [02/Nov/2017:07:17:08 +0800] "GET / HTTP/1.1" 200 8 "-" "curl/7.29.0" "-"  
......  
  • 定义日志缓存:

Usage example:(官方示例)
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;

[root@GaoServer ~]# vim /etc/nginx/nginx.conf
......  
    access_log  /var/log/nginx/access.log  main;
    open_log_file_cache max=100 inactive=20s valid=1m min_uses=2;
......
[root@GaoServer ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@GaoServer ~]# nginx -s reload
<think>嗯,用户想在Nginx中添加ngx_http_lua_module模块,我需要详细说明编译和配置的步骤。首先,用户可能已经安装了Nginx,但需要重新编译添加新模块。记得要提醒他们备份配置,防止出错。 第一步应该是安装依赖项,比如LuaJIT,因为ngx_http_lua_module依赖它。需要指导用户下载LuaJIT并进行安装。这里可能会有问题,比如环境变量设置是否正确,或者权限问题,所以得提到export命令,并建议将路径添加到.bashrc或类似文件中。 接下来是获取Nginx源码,用户可能不知道他们的Nginx版本,所以需要让他们用nginx -v命令查看,并下载对应的源码包。然后解压到指定目录。 然后是关键的配置步骤,使用./configure命令,添加模块参数。这里需要确保用户正确指定了LuaJIT的库和头文件路径,可能需要使用--with-ld-opt和--with-cc-opt选项。用户之前提到过在安装时遇到Lua库的问题,所以这里要特别强调路径的正确性,避免再次出错。 编译和安装部分,make和make install的步骤要清楚,提醒用户不要直接make install,而是先make,防止覆盖现有配置。如果用户之前已经安装了Nginx,可能需要停止服务后再进行安装。 配置Nginx部分,需要给出一个简单的例子,比如用content_by_lua_block指令来测试Lua是否正常工作。同时,要提醒用户检查nginx.conf是否加载了模块,并重新加载配置。 验证部分,指导用户如何测试配置是否正确,包括检查Nginx错误日志和发送请求查看响应。这部分很重要,因为用户需要确认模块是否成功加载。 可能的问题和注意事项中,要提到动态模块的可能性,但根据引用中的信息,用户可能是在静态编译,所以重点放在静态添加。还要提到依赖项如PCRE、OpenSSL等,确保用户安装了所有必要软件。 最后,引用相关的参考资料,特别是用户之前遇到的错误,确保他们正确设置环境变量和路径。同时,提醒日志的重要性,引用用户提供的日志配置例子,帮助他们调试。 整个过程需要步骤清晰,避免技术术语过多,用简单的中文说明,确保用户能一步步跟随操作。同时,预测用户可能遇到的问题,提前给出解决方案,比如环境变量设置和路径检查。</think>### 逐步解答:如何在Nginx中添加ngx_http_lua_module模块 #### 步骤1:安装依赖项 需先安装LuaJIT(Lua的高效实现版本),这是ngx_http_lua_module的核心依赖: ```bash wget https://github.com/LuaJIT/LuaJIT/archive/refs/tags/v2.1-20230410.tar.gz tar -zxvf LuaJIT-2.1-20230410.tar.gz cd LuaJIT-2.1-20230410 make && sudo make install ``` 设置环境变量(需添加到`.bashrc`或`.zshrc`): ```bash export LUAJIT_LIB=/usr/local/lib export LUAJIT_INC=/usr/local/include/luajit-2.1 ``` #### 步骤2:获取Nginx源码 若已安装Nginx,需通过`nginx -v`获取版本号,下载对应源码包: ```bash wget http://nginx.org/download/nginx-1.25.3.tar.gz tar -zxvf nginx-1.25.3.tar.gz cd nginx-1.25.3 ``` #### 步骤3:配置编译参数 通过`./configure`添加模块路径(假设ngx_http_lua_module源码在`/path/to/lua-nginx-module`): ```bash ./configure \ --add-module=/path/to/lua-nginx-module \ --with-ld-opt="-Wl,-rpath,/usr/local/lib" \ --with-cc-opt="-I/usr/local/include/luajit-2.1" ``` *若遇到`error: ngx_http_lua_module requires the Lua library`,检查环境变量`LUAJIT_LIB`和`LUAJIT_INC`是否设置正确[^2]* #### 步骤4:编译与安装 ```bash make sudo make install # 若已安装旧版,需先停止Nginx服务 ``` #### 步骤5:配置Nginx 在`nginx.conf`中添加Lua模块测试配置: ```nginx location /lua_test { content_by_lua_block { ngx.say("Hello from Lua!") } } ``` #### 步骤6:验证模块 1. 检查Nginx错误日志: ```bash tail -f /var/log/nginx/error.log ``` 2. 发送请求验证: ```bash curl http://localhost/lua_test ``` 若返回`Hello from Lua!`则表示成功 #### 注意事项 1. 动态模块加载(需Nginx 1.9.11+): ```bash ./configure --add-dynamic-module=/path/to/lua-nginx-module ``` 2. 确保安装`pcre-dev`、`openssl-dev`等依赖项 3. 若使用自定义日志格式,可参考`ngx_http_log_module`的变量定义[^3][^4]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值