Nginx调试日志[emerg]: invalid log level “debug_http” in /path/conf/nginx.conf:XX

配置日志的时候重启nginx报错:[emerg]: invalid log level “debug_http” in /path/conf/nginx.conf:XX,看到这篇文章找到了答案

转载地址:http://100continue.iteye.com/blog/1821893


用户在使用Nginx的过程中,可能会遇到所请求的资源不正确,Nginx Core Dump,段错误等异常情况,这时需要有相应的机制来进行调试及问题定位,特别是面对大量的日志信息,合理的调试处理机制对用户来说是一件非常重要的事情。以下将着重为大家介绍调试日志。

 

【调试日志】

一,开启调试日志:

要开启调试日志,首先需要在配置Nginx时打开调试功能,然后编译:

./configure --with-debug ...

然后在配置文件中设置error_log的级别为:

error_log /path/to/log debug;

Nginx的Windows二进制版本总是将调试日志开启的,因此只需要设置debug的日志级别即可。

 

二,日志级别分析:

在此,我们通过分析Nginx源码了解下Nginx将日志分为几个等级及不同日志等级之间的相互关系:

 

Ngx_log.h代码   收藏代码
  1. #define NGX_LOG_STDERR 0  
  2. #define NGX_LOG_EMERG 1  
  3. #define NGX_LOG_ALERT 2  
  4. #define NGX_LOG_CRIT 3  
  5. #define NGX_LOG_ERR 4  
  6. #define NGX_LOG_WARN 5  
  7. #define NGX_LOG_NOTICE 6  
  8. #define NGX_LOG_INFO 7  
  9. #define NGX_LOG_DEBUG 8  
  10.   
  11. #define NGX_LOG_DEBUG_CORE 0x010  
  12. #define NGX_LOG_DEBUG_ALLOC 0x020  
  13. #define NGX_LOG_DEBUG_MUTEX 0x040  
  14. #define NGX_LOG_DEBUG_EVENT 0x080  
  15. #define NGX_LOG_DEBUG_HTTP 0x100  
  16. #define NGX_LOG_DEBUG_MAIL 0x200  
  17. #define NGX_LOG_DEBUG_MYSQL 0x400  
  18.   
  19. #define NGX_LOG_DEBUG_FIRST NGX_LOG_DEBUG_CORE  
  20. #define NGX_LOG_DEBUG_LAST NGX_LOG_DEBUG_MYSQL  
  21. #define NGX_LOG_DEBUG_CONNECTION 0x80000000  
  22. #define NGX_LOG_DEBUG_ALL 0x7ffffff0  

 

其中默认有效的第一级别日志是"stderr","emerg","alert","crit","error","warn","notice","info","debug"。

而Ngx_log.h内列出的其他debug第二级别日志:"debug_core","debug_alloc","debug_mutex","debug_event","debug_http","debug_mail","debug_mysql"等则需要在配置Nginx时启动调试日志功能才能使用,并且用户可以通过修改Ngx_log.h及Ngx_log.c源码来更新debug第二级别。

 

我们再通过Ngx_log.c的部分代码分析下可以如何使用这些日志级别:

 

Ngx_log.c代码   收藏代码
  1. char *  
  2. ngx_log_set_levels(ngx_conf_t *cf, ngx_log_t *log)  
  3. {  
  4.     ...  
  5.   
  6.         for (n = 1; n <= NGX_LOG_DEBUG; n++) {  
  7.             if (ngx_strcmp(value[i].data, err_levels[n].data) == 0) {  
  8.   
  9.                 if (log->log_level != 0) {  
  10.                     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,  
  11.                                        "duplicate log level \"%V\"",  
  12.                                        &value[i]);  
  13.                     return NGX_CONF_ERROR;  
  14.                 }  
  15.   
  16.                 log->log_level = n;  
  17.                 found = 1;  
  18.                 break;  
  19.             }  
  20.         }  
  21.   
  22.         for (n = 0, d = NGX_LOG_DEBUG_FIRST; d <= NGX_LOG_DEBUG_LAST; d <<= 1) {  
  23.             if (ngx_strcmp(value[i].data, debug_levels[n++]) == 0) {  
  24.                 if (log->log_level & ~NGX_LOG_DEBUG_ALL) {  
  25.                     ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,  
  26.                                        "invalid log level \"%V\"",  
  27.                                        &value[i]);  
  28.                     return NGX_CONF_ERROR;  
  29.                 }  
  30.   
  31.                 log->log_level |= d;  
  32.                 found = 1;  
  33.                 break;  
  34.             }  
  35.         }  
  36.  ...  
  37.  if (log->log_level == NGX_LOG_DEBUG) {  
  38.         log->log_level = NGX_LOG_DEBUG_ALL;  
  39.     }  
  40.  ...  
  41. }  

按照以上代码逻辑,我们可以得出以下结论:

1. 第一级别日志之间是互斥的,如果配置文件内加入如下配置项:

Java代码   收藏代码
  1. error_log path/logs/error.log warn;  
  2. error_log path/logs/error.log info;  

 那么启动Nginx将报错如下:

[emerg]: duplicate log level "info" in /path/conf/nginx.conf:XX

但是需要注意的是,在配置文件不同block中是允许重新定义错误日志的。但是当用户在重新定义错误日志时,如果没有指定相应的日志级别,那么调试日志将会被屏蔽。下面的例子里,在server层中重新定义的日志就屏蔽了这个虚拟主机的调试日志:

 

C代码   收藏代码
  1. error_log  /path/to/log  debug;  
  2.   
  3. http {  
  4.     server {  
  5.         error_log  /path/to/log;  
  6.         ...  

 为了避免这个问题,可以注释这行重新定义日志的配置,或者也给日志指定debug级别:

 

C代码   收藏代码
  1. error_log  /path/to/log  debug;  
  2.   
  3. http {  
  4.     server {  
  5.         error_log  /path/to/log  debug;  
  6.         ...  

 

 2. 第二级别日志是多选的,用户可以根据项目需要配置多个第二级别日志:

C代码   收藏代码
  1. error_log  logs/error.log debug_mysql;  
  2. error_log  logs/error.log debug_core;  

 

3. 在第一级别日志与第二级别日志组合配置时,仅有在第一级别日志为"debug"时才可以有第二级别的配置,其他第一级别日志的情况下指定第二级别日志将无法启动Nginx,如:

C代码   收藏代码
  1. error_log  logs/error.log error;  
  2. error_log  logs/error.log debug_core;  

启动Nginx将获得如下错误信息:

[emerg]: invalid log level “debug_http” in /path/conf/nginx.conf:XX

 当用户开启debug级别日志时,会输出所有debug_开头的调试信息,因此可以通过上面组合debug_core|debug_http的形式来获取用户所需要的调试信息。

 

三,日志格式设置:

用户在使用Nginx提供web服务的时候,可能会有很多场景需要记录日志,如打点日志,访问日志,数据统计日志,性能分析日志等。为了更加方便的对日志进行分析,我们可以通过设置日志格式的方式来要求Nginx按照用户要求进行日志的展现。

控制nginx日志输出的指令如下:

C代码   收藏代码
  1. log_format  customLog "$remote_addr^A$remote_user^A$time_local^A$request_method^A$uri^A$args^A$server_protocol"  
  2.         "^A$status^A$body_bytes_sent^A$http_referer"  
  3.         "^A$http_user_agent";  
  4. access_log /path/logs/access.log customLog;  

上面例子中通过使用特殊字符(^A)来作为日志字段的分隔符,用户后续可以使用sort和grep之类的工具对特定url做分析,如统计各url请求量倒排取前50个:

C代码   收藏代码
  1. awk -F^A '{print $5}' /path/logs/access.log | sort | uniq -c | sort -nr | head -50  

类似上面的日志定制化设置,可以让用户在调试日志的过程中随心所欲,如鱼得水。

详细的log_format指令和access_log指令,用户可以访问Nginx官网的HttpLog模块

 

四,调试日志的几个注意点:

1. 勘误:在Nginx Wiki里面error log相关部分的介绍中提到

http://wiki.nginx.org/NginxHttpMainModule#error_log 
Default values for the error level:
in the main section - error
in the HTTP section - crit
in the server section - crit

但是,我们从源码上看:

C代码   收藏代码
  1. static char *  
  2. ngx_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)  
  3. {  
  4.     ...  
  5.   
  6.     if (cf->args->nelts == 2) {  
  7.         cf->cycle->new_log.log_level = NGX_LOG_ERR;  
  8.         return NGX_CONF_OK;  
  9.     }  
  10.  ...  
  11. }  

当error_log 的日志级别选项为配置时,默认日志级别为error,无上面提及的三个section的区别。故特在此勘误。

 

2. 配置error_log off并不能关闭日志记录——日志信息会被写入到文件名为off的文件当中。如果要关闭日志记录,用户可以做如下配置:

C代码   收藏代码
  1. error_log /dev/null crit;  

 

3. 如果nginx进程没有权限将日志信息写入指定的log地址,那么nginx会在启动是报错:

[alert]: could not open error log file: open() "/path/log/nginx/error.log" failed (13: Permission denied)

 

4. 通过debug_connection配置项,用户可以针对某些地址开启调试日志:

 

C代码   收藏代码
  1. error_log  /path/to/log;  
  2.    
  3. events {  
  4.     debug_connection   10.232.10.1;  
  5.     debug_connection   10.232.10.0/24;  
  6. }  

 

 

 

 

参考文献:

1. 调试日志 http://nginx.org/cn/docs/debugging_log.html

2. nginx error log  http://wiki.nginx.org/NginxHttpMainModule#error_log

3. log format http://wiki.nginx.org/HttpLogModule

4. 使用nginx记日志 http://blog.linezing.com/2011/11/%E4%BD%BF%E7%94%A8nginx%E8%AE%B0%E6%97%A5%E5%BF%97

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个错误的原因是在nginx配置文件中的proxy_pass指令中,参数数量不正确。错误提示是"invalid number of arguments in "proxy_pass" directive"。在你提供的引用内容中,有几个解决方案是针对类似的错误而提供的。 首先,根据引用,错误可能是由于在proxy_pass属性中的地址配置中使用了错误的符号。正确的符号应该是冒号(:),而不是分号(;)。确认一下你的配置文件中是否使用了正确的符号。 另外,根据引用,错误可能是由于缺少了分号。在你的配置文件中,你需要确保在proxy_pass指令的末尾添加一个分号,以确保参数的正确性。 综上所述,你需要检查你的配置文件中的proxy_pass指令,确保参数数量正确,并使用正确的符号。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [invalid number of arguments in “proxy_pass“ directive in /usr/local/nginx/conf/nginx.conf:46](https://blog.csdn.net/m0_46405589/article/details/114535632)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Nginx反向代理proxy_cache_path directive is not allowed错误解决方法](https://download.csdn.net/download/weixin_38674763/14096007)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [nginx: [emerg] invalid number of arguments in “proxy_pass“ directive in /usr/local/nginx/conf](https://blog.csdn.net/u010227042/article/details/118735758)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值