`
haoningabc
  • 浏览: 1465708 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

nginx调试日志的几种方法

阅读更多
最简单的方式就是

fprintf(stderr, "haoning hahahah:%s\r\n","ningning");


或者
 ngx_log_stderr(0,"haoning: ngx_http_hello_world_handler\"%s\"","haohao" );

但是如果打印
ngx_log_stderr(0,"haoning haohao subrequest in memory: %d", (int) r->subrequest_in_memory);
ngx_log_stderr(0,"haoning haohao  r->method : %d",(int) r->method);
ngx_log_stderr(0,"haoning haohao r->http_version: %d",(int) r->http_version) ;
ngx_log_stderr(0,"haoning haohao r->request_line.data: %s",r->request_line.data) ;
ngx_log_stderr(0,"haoning haohao r->uri.data): %s",r->uri.data);
ngx_log_stderr(0,"haoning haohao r->args.data: %s",r->args.data);
ngx_log_stderr(0,"haoning haohao r->unparsed_uri.data: %s",r->unparsed_uri.data);
ngx_log_stderr(0,"haoning haohao r->method_name.data: %s",r->method_name.data)  ;
ngx_log_stderr(0,"haoning haohao r->http_protocol.data: %s",r->http_protocol.data);
ngx_log_stderr(0,"haoning haohao r->exten.data: %s",r->exten.data);

会报错

参考章亦春的echo
http://wiki.nginx.org/HttpEchoModule

源码中有个ddebug.h里面定义的打印log都输出到stderr
所以在这个模块的源码中把开头的
#ifndef DDEBUG
#define DDEBUG 0
#endif

改成
#ifndef DDEBUG
#define DDEBUG 1
#endif

当启动nginx的时候就输出log了 类似

e.c line 463.
echo *** ngx_http_echo_helper: filter used = 1 at /root/hellogit/hello/graphviz/nginx/mymodule/echo-nginx-module-0.48/src/ngx_http_echo_module.c line 324.
echo *** ngx_http_echo_helper: found raw arg hello at /root/hellogit/hello/graphviz/nginx/mymodule/echo-nginx-module-0.48/src/ngx_http_echo_module.c line 358.
echo *** ngx_http_echo_echo_before_body: processing echo_before_body directive... at /root/hellogit/hello/graphviz/nginx/mymodule/echo-nginx-module-0.48/src/ngx_http_echo_module.c line 463.
echo *** ngx_http_echo_helper: found raw arg world at /root/hellogit/hello/graphviz/nginx/mymodule/echo-nginx-module-0.48/src/ngx_http_echo_module.c line 358.
echo *** ngx_http_echo_helper: filter used = 1 at /root/hellogit/hello/graphviz/nginx/mymodule/echo-nginx-module-0.48/src/ngx_http_echo_module.c line 324.
echo *** ngx_http_echo_helper: found raw arg hiya at /root/hellogit/hello/graphviz/nginx/mymodule/echo-nginx-module-0.48/src/ngx_http_echo_module.c line 358.
echo *** ngx_http_echo_helper: found raw arg igor at /root/hellogit/hello/graphviz/nginx/mymodule/echo-nginx-module-0.48/src/ngx_http_echo_module.c line 358.

可以重定向
./nginx >a.log 2>&1

★★★★★★第二种方法★★★★★★
还有一种是通用的 打印log的方法,跟nginx没关系
任何都适用,定义haolog.c   haolog.h 在ngx_http_echo_module.c中引用haolog.h
/root/hellogit/hello/graphviz/nginx/mymodule/echo
[root@VM_253_237_tlinux echo]# ls
config  haolog.c  haolog.h  nginx.conf  ngx_http_echo_module.c  shuoming.txt
[root@VM_253_237_tlinux echo]# 

config中标记新增的源码.c 和依赖的.h
ngx_addon_name=ngx_http_echo_module
HTTP_MODULES="$HTTP_MODULES ngx_http_echo_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_echo_module.c $ngx_addon_dir/haolog.c"
NGX_ADDON_DEPS="$NGX_ADDON_DEPS $ngx_addon_dir/haolog.h"
CORE_LIBS="$CORE_LIBS -lpcre"

log代码如下
[root@VM_253_237_tlinux echo]# cat  haolog.h

#include <stdio.h>
#include <time.h>
#define DEBUG_LOG( str ) log_append_to_file("/root/hellogit/hello/graphviz/nginx/mymodule/mylog/test.log", str,__FILE__,__LINE__ );
void log_append_to_file(char* filename,char* str,char* sourceFile,int fileLine);
[root@VM_253_237_tlinux echo]

haolog.c:
[root@VM_253_237_tlinux echo]# cat haolog.c 
#include <stdio.h>
#include <time.h>
#define DEBUG_LOG( str ) log_append_to_file("/root/hellogit/hello/graphviz/nginx/mymodule/mylog/test.log", str,__FILE__,__LINE__ );  
void log_append_to_file(char* filename,char* str,char* sourceFile,int fileLine)
{
        time_t t;
        time(&t);
        struct tm* tp= localtime(&t);
        char now_str[100];
        strftime(now_str, 100, "%Y-%m-%d %H:%M:%S", tp);
        FILE *fo;
        fo = fopen(filename, "a");
    if (fo == 0) {
        return;
    }
        fprintf(fo, "%s %s(:%d):%s\r\n",now_str,sourceFile,fileLine, str);
        fclose(fo);
}

//int main(int argc, char **argv)
//{
///*********************define******************/
//
//        DEBUG_LOG("haoning----logmain.c");
//        printf("Hello World!\n");
//        return 0;
//} 
[root@VM_253_237_tlinux echo]# 


完整的nginx模块如下
[root@VM_253_237_tlinux echo]# cat ngx_http_echo_module.c 
/*
* Copyright (C) fabricehao
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <time.h>
#include "haolog.h"

/* Module config */
typedef struct {
    ngx_str_t  ed;
} ngx_http_echo_loc_conf_t;
static char *ngx_http_echo(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
static void *ngx_http_echo_create_loc_conf(ngx_conf_t *cf);
static char *ngx_http_echo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);
/* Directives */
static ngx_command_t  ngx_http_echo_commands[] = {
        { 
                ngx_string("echo"),
        NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
        ngx_http_echo,
        NGX_HTTP_LOC_CONF_OFFSET,
        offsetof(ngx_http_echo_loc_conf_t, ed),
        NULL 
        },
    ngx_null_command
};
/* Http context of the module */
static ngx_http_module_t  ngx_http_echo_module_ctx = {
    NULL,                                  /* preconfiguration */
    NULL,                                  /* postconfiguration */
    NULL,                                  /* create main configuration */
    NULL,                                  /* init main configuration */
    NULL,                                  /* create server configuration */
    NULL,                                  /* merge server configuration */
    ngx_http_echo_create_loc_conf,         /* create location configration */
    ngx_http_echo_merge_loc_conf           /* merge location configration */
};
/* Module */
ngx_module_t  ngx_http_echo_module = {
    NGX_MODULE_V1,
    &ngx_http_echo_module_ctx,             /* module context */
    ngx_http_echo_commands,                /* module directives */
    NGX_HTTP_MODULE,                       /* module type */
    NULL,                                  /* init master */
    NULL,                                  /* init module */
    NULL,                                  /* init process */
    NULL,                                  /* init thread */
    NULL,                                  /* exit thread */
    NULL,                                  /* exit process */
    NULL,                                  /* exit master */
    NGX_MODULE_V1_PADDING
};
/* Handler function */
static ngx_int_t
ngx_http_echo_handler(ngx_http_request_t *r)
{
        DEBUG_LOG("haoning.........ngx_http_echo_handler");
    ngx_int_t rc;
    ngx_buf_t *b;
    ngx_chain_t out;
    ngx_http_echo_loc_conf_t *elcf;
    elcf = ngx_http_get_module_loc_conf(r, ngx_http_echo_module);
    if(!(r->method & (NGX_HTTP_HEAD|NGX_HTTP_GET|NGX_HTTP_POST)))
    {
        return NGX_HTTP_NOT_ALLOWED;
    }
    r->headers_out.content_type.len = sizeof("text/html") - 1;
    r->headers_out.content_type.data = (u_char *) "text/html";
    r->headers_out.status = NGX_HTTP_OK;
    r->headers_out.content_length_n = elcf->ed.len;
    if(r->method == NGX_HTTP_HEAD)
    {
                DEBUG_LOG("haoning......ngx_http_echo_handlerr---r->method == NGX_HTTP_HEAD");
        rc = ngx_http_send_header(r);
        if(rc != NGX_OK)
        {
            return rc;
        }
    }
    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
    if(b == NULL)
    {
        ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer.");
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
    }
    out.buf = b;
    out.next = NULL;
    b->pos = elcf->ed.data;
    b->last = elcf->ed.data + (elcf->ed.len);
    b->memory = 1;
    b->last_buf = 1;
    rc = ngx_http_send_header(r);
    if(rc != NGX_OK)
    {
        return rc;
    }
        DEBUG_LOG("haoning......ngx_http_output_filter");
    return ngx_http_output_filter(r, &out);
}
static char *
ngx_http_echo(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
        DEBUG_LOG("haoning --ngx_http_echo->>>>> init");
    ngx_http_core_loc_conf_t  *clcf;
    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
    clcf->handler = ngx_http_echo_handler;
    ngx_conf_set_str_slot(cf,cmd,conf);
    return NGX_CONF_OK;
}
static void *
ngx_http_echo_create_loc_conf(ngx_conf_t *cf)
{
        DEBUG_LOG("haoning --ngx_http_echo_create_loc_conf");
    ngx_http_echo_loc_conf_t  *conf;
    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_echo_loc_conf_t));
    if (conf == NULL) {
        return NGX_CONF_ERROR;
    }
    conf->ed.len = 0;
    conf->ed.data = NULL;
    return conf;
}
static char *
ngx_http_echo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
{
        DEBUG_LOG("haoning --ngx_http_echo_merge_loc_conf");
    ngx_http_echo_loc_conf_t *prev = parent;
    ngx_http_echo_loc_conf_t *conf = child;
    ngx_conf_merge_str_value(conf->ed, prev->ed, "");
    return NGX_CONF_OK;
}
[root@VM_253_237_tlinux echo]# 

调用
DEBUG_LOG("haoning --ngx_http_echo_merge_loc_conf");
就打印到自定义的
/root/hellogit/hello/graphviz/nginx/mymodule/mylog/test.log


疑问,handler里的怎么打印不出来????
分享到:
评论

相关推荐

    nginx中用JSON格式记录日志的配置示例

    根据上述文件提供的信息,配置Nginx以使用JSON格式记录日志的步骤可以详细分为以下几个知识点: 1. Nginx日志格式配置基础 首先,要了解Nginx的日志记录是通过其配置文件来定义的,通常位于nginx.conf中的http、...

    Nginx相关.rar

    在Linux系统上,通常有以下几种方法来安装Nginx: 1. **使用包管理器**:对于基于RPM的系统(如凝思6),可以使用`yum`或`dnf`(如果已升级到更新版本)来安装。命令如下: ``` sudo yum install nginx ``` 2. ...

    squid和nginx配置正向代理访问API接口.rar

    在实际应用中,你可能还需要考虑其他因素,如缓存策略(对于提高性能)、SSL/TLS 加密(确保数据传输安全)以及日志记录(便于监控和调试)。同时,根据业务需求,你可能需要结合 Squid 和 Nginx 使用,比如 Squid ...

    nginx lua处理图片

    如果出现问题,可以查看Nginx的日志文件或者启用更详细的日志模式来进行调试。 通过这种方式,Nginx Lua不仅可以作为静态文件服务器,还可以作为一个轻量级的图片处理服务,为Web应用提供灵活、高效的图片处理能力...

    php apache/nginx

    关于Apache和PHP的集成,有几种方法,如使用PHP作为Apache模块(mod_php),FastCGI,或者通过PHP-FPM(FastCGI Process Manager)。每种方法都有其优缺点,例如mod_php将PHP解析器直接嵌入Apache,响应速度快但可能...

    nginx rtmp直播

    - **日志**:配置Nginx的日志记录,以便分析和调试直播服务中的问题。 7. **案例应用** - **教育直播**:在线教育平台可以通过Nginx RTMP实现教师授课的实时直播,学生可以随时随地观看。 - **电竞赛事**:游戏...

    nginx中文文档

    Nginx是一种高性能的HTTP和反向代理服务器,同时也支持IMAP/POP3/SMTP代理服务。其在Linux、BSD、Solaris、Mac OS X、...此外,文档还提供了常见问题的解答,以及如何调试Nginx的详细信息,大大降低了使用Nginx的门槛。

    nginx配置文件.zip

    - 日志记录:合理设置日志文件,便于监控和调试。 通过深入理解这些知识点,你将能够有效地利用Nginx在Docker环境中搭建高效、稳定的网络服务。阅读并实践提供的配置文件,结合博客文章,将有助于你更好地掌握这些...

    keepalived +nginx 终于搞定了

    在IT行业中,网络服务的高可用性和负载均衡是至关重要的,而`keepalived`与`nginx`的结合就是一种常见的实现方式。本篇将详细阐述`keepalived`和`nginx`如何协同工作,以及如何成功配置它们,确保服务的稳定运行。 ...

    解析Nginx中的日志模块及日志基本的初始化和过滤配置

    配置Nginx的日志模块,可以通过以下几种方式: 1. **日志级别配置**:通过`error_log`指令设置日志级别,如`error_log /path/to/error.log info;`将错误日志级别设置为INFO。 2. **日志文件位置**:使用`error_log...

    ngnix.zip----arm版本

    8. **监控与日志**:理解如何查看Nginx的日志文件,以便于调试问题和分析服务器性能。还可以集成其他监控工具,如Prometheus或ELK栈,以获取更详细的性能数据。 9. **反向代理与负载均衡**:学习如何配置Nginx作为...

    nginx 原理简介

    3. **调试工具**:包括使用gdb进行调试、日志记录等方法。 #### 结语 Nginx凭借其强大的功能和灵活性,已成为现代Web开发不可或缺的一部分。无论是作为Web服务器、反向代理还是负载均衡器,Nginx都能胜任。深入...

    PHP代码调试工具

    9. Web服务器日志:Apache和Nginx等Web服务器也有自己的日志系统,记录了请求处理的详细信息,对于排查服务器相关的问题非常有帮助。 10. IDE集成:现代IDE如PhpStorm、Visual Studio Code等,都有强大的PHP调试...

    nginx进阶vip - 第二课笔记1

    Location指令的匹配规则有以下几种: 1. `=`:精确匹配,如果找到,则停止其他匹配。 2. `^~`:如果URI以指定的字符串开始,停止其他匹配。 3. `~` 和 `~*`:区分大小写和不区分大小写的正则匹配。 4. `/`:作为...

    理解 Nginx 源码

    配置文件中的配置项主要分为几种类型,包括基本配置项、用于调试和定位的配置项、优化性能的配置项以及事件类配置项。基本配置项包括error_log、pid、worker_processes等,用于定义错误日志的路径、存放pid文件的...

    python-nginx-example-app-源码.rar

    8. **日志管理**:Python 应用和 Nginx 都会生成日志,这对于调试和监控系统状态至关重要。了解日志配置和日志文件的位置有助于排查问题。 9. **部署流程**:理解如何将这个应用部署到服务器上,涉及到的步骤可能...

    日志清除器 日志清除器

    在IT行业中,日志清除器是一种非常实用的工具,它主要负责管理和清理系统或应用程序产生的大量日志文件。日志文件记录了系统运行时的各种事件、错误信息、警告以及调试数据,对于排查问题和监控系统状态至关重要。...

    nginx 反向代理之 proxy_pass的实现

    格式很简单: proxy_pass URL; ... 示例如下: ...对于proxy_pass的配置有几种情况需要注意: 假设server_name为www.xxx.com 当请求http://www.xxx.com/aming/a.html的时候,以上示例分别访问的结果是 示例1

    Nginx虚拟主机

    3. **日志记录**:`access_log`和`error_log`指令用于记录访问和错误日志,有助于分析和调试。 总结,Nginx虚拟主机是高效管理多网站的关键技术,通过合理配置,可以实现资源优化、性能提升以及安全控制。了解和...

Global site tag (gtag.js) - Google Analytics