ngnix:初步使用log日志系统打印ngx_conf_parse解析的配置项

缘由

这次主要是由昨天的那篇博客啊,书上说ngx_conf_s的成员 name是什么名字,但是我在用ddd调试的时候,却没有发现里面存的有什么值。今天就打算用nginx自带的日志系统来验证一下。实际证明我是对的,对于我这份配置文件来说name确实什么值都没有。


配置文件

先看一下我的配置文件:
master_process off;  
daemon off;  
error_log  logs/error.log  debug;  


events {  
    worker_connections  1024;  
}  
  
http {  
    include       mime.types;  
    default_type  application/octet-stream;  
    keepalive_timeout  65;  
    server {  
        listen 8080;  
    location / {  
        root html;  
        add_prefix on;  
    }  
    }  
} 

添加log代码

我主要在:src/core/ngx_conf_file.c的for循环内添加了以下打印代码。从上一篇博客的图来分析就知道这个循环是逐一用来解析配置文件的。这一点从我后来的打印信息了也验证了。其中,我之所要打印cf->args->elts这是从以往的分析中我就发现了这个cf->args动态数组装的就是配置文件中的一条完整指令(指令名+其值)。那么elts作为其动态数组的第一个元素,那么打印就是一条指令的指令名(listen)。
char *
ngx_conf_parse(ngx_conf_t *cf, ngx_str_t *filename)
{
	...
    for ( ;; ) {
        rc = ngx_conf_read_token(cf);
        //----------------我的添加
		ngx_log_error(NGX_LOG_ERR, cf->log, 0, "ngx_conf_parse  execute:args[1]:%V",cf->args->elts);
		if(cf->name!=NULL){
			ngx_log_error(NGX_LOG_ERR, cf->log, 0, "name:%V ",cf->name);
		}
	...//-----------------我的添加结束
}

编译

我原来认为这一步我会真的比较艰难,但是应该是发现了简单的办法,但是却不特别明白其原理。当我在源代码里面添加了上述语句之后,如果像平常执行
./configure  ...//后面还有很多,根据自己的实际情况添加
make
sudo ./objs/nginx

这样肯定是没有问题的,但是大家肯定也知道,再第一次make的时候时间还是有点慢。

重点是,当我再一次修改源代码后,直接
make
sudo ./objs/nginx
这样,新一次修改的代码也会生效。而且make的速度非常快。我认为这样就非常好了。我想起原理大概就是说有一种机制能够设别是否源代码有没有改动,如果没有改动就直接使用上一次生成的中间文件。


执行的结果

首先会在控制台打印信息,这应该是我将错误级别甚至为error的原因。后来我将级别调到notice后,就没有输出到控制台了。当然在log文件中也有:
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:master_process
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:daemon
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:error_log
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:events
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:worker_connections
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:worker_connections
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:http
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:include
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:types
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:text/html
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:text/css
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:text/xml
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:image/gif
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:image/jpeg
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-javascript
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/atom+xml
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/rss+xml
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:text/mathml
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:text/plain
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:text/vnd.sun.j2me.app-descriptor
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:text/vnd.wap.wml
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:text/x-component
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:image/png
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:image/tiff
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:image/vnd.wap.wbmp
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:image/x-icon
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:image/x-jng
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:image/x-ms-bmp
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:image/svg+xml
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:image/webp
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/java-archive
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/mac-binhex40
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/msword
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/pdf
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/postscript
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/rtf
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/vnd.ms-excel
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/vnd.ms-powerpoint
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/vnd.wap.wmlc
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/vnd.google-earth.kml+xml
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/vnd.google-earth.kmz
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-7z-compressed
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-cocoa
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-java-archive-diff
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-java-jnlp-file
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-makeself
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-perl
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-pilot
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-rar-compressed
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-redhat-package-manager
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-sea
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-shockwave-flash
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-stuffit
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-tcl
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-x509-ca-cert
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/x-xpinstall
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/xhtml+xml
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/zip
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/octet-stream
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/octet-stream
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/octet-stream
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/octet-stream
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/octet-stream
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:application/octet-stream
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:audio/midi
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:audio/mpeg
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:audio/ogg
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:audio/x-m4a
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:audio/x-realaudio
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:video/3gpp
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:video/mp4
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:video/mpeg
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:video/quicktime
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:video/webm
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:video/x-flv
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:video/x-m4v
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:video/x-mng
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:video/x-ms-asf
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:video/x-ms-wmv
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:video/x-msvideo
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:video/x-msvideo
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:video/x-msvideo
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:default_type
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:keepalive_timeout
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:server
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:listen
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:location
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:root
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:add_prefix
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:add_prefix
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:add_prefix
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:add_prefix
2014/04/28 17:22:18 [notice] 10985#0: ngx_conf_parse  execute:args[1]:add_prefix

可以看到,输出的log的信息是和配置文件的指令名想对应的。当然里面还有很多内容,这是与include指令有关的。
此外就是add_prefix执行了6次,worker_connections执行了2次。这次暂时没没搞懂,以后哦再次说。注意这个add_prefix是我们自己的做实验添加的变量,不是nginx自带的。我们自己添加了模块
执行configure如下:
./configure --add-module=/home/asd/Downloads/chapter6 --without-http_rewrite_module --without-http_gzip_module --with-cc-opt='-g -O0' --with-debug
忘了的话看博客

include指令

包含其他的文件,我们可以看到
    include       mime.types;
实际上就是将mime.tpyes的文件直接插到这里了,而且这里。可以简单看一下mime.types的内容。
types {
    text/html                             html htm shtml;
    text/css                              css;
    text/xml                              xml;
    image/gif                             gif;
	.... 
    text/plain                            txt;
}

types指令主要完成 MIME type到文件扩展名的映射而已。

下面摘了一下百度百科对MIME的介绍:

MIME(Multipurpose Internet Mail Extensions)多用途互联网邮件扩展类型就是设定某种扩展名的文件用一种应用程序来打开的方式类型,当该扩展名文件被访问的时候,浏览器会自动使用指定应用程序来打开。多用于指定一些客户端自定义的文件名,以及一些媒体文件打开方式。

从我实际的使用中来看,上次我再发一个通过nginx发送一个txt的文件的时候呢,里面的http的respones就会将content-type的类型设置为text/plain,这和上面mime.types的设定是一致的。我想通过这样一种方式浏览器也就是知道text/plain是一种txt文件,那么方便指定应用程序打开它。可以想像如果是mp4视频格式的话,这个指令应该特别有用吧。


总结

今天学会了log调试,感觉非常好。当然前几次用ddd(gdb)调试也非常不错,然而这两者应该说各有各的用处,所以应该根据实际情况来选择。就想这次我像看看name到底有什么值,但是for循环要执行那么多次,难道我用ddd一直点c么?而且try了一次,watch在这样的情况下也不是那么方便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值