更多优质内容
请关注公众号

Nginx HTTP模块篇 日志记录之log模块和过滤模块 (十六)-张柏沛IT博客

正文内容

Nginx HTTP模块篇 日志记录之log模块和过滤模块 (十六)

栏目:其他内容 系列:Nginx入门系列 发布时间:2020-02-17 21:47 浏览量:2809

log 阶段

log模块

log_format name 格式      # 定义日志格式,默认值在nginx.conf中有。可以定义多个格式用不同name区分
access_log path name [if=condition]        # 这个name是log_format定义的格式的名字;如果使用了if,则为真才会记录日志

 

 

http过滤模块

该模块是发生在content模块之后,log模块之前。如image_fiter模块,gzip模块就是过滤模块的一部分。

重要的过滤模块:
header_filter模块:用于构造响应头
write_filter模块:用于调用操作系统的write()或者send()方法,将响应发送给客户端。

其他过滤模块
sub模块:将响应(体)中就是源码中指定的字符串替换为新的字符串;默认未编译进nginx,通过--with-http_sub_filter_module启用

sub_filter string replacement;      # 将指定字符替换为其他字符,忽略大小写。
sub_filter_last_modified on|off;    # 是否显示替换时间(在响应头的Last-Modified项),默认off
sub_filter_once on|ff;    # 是否只替换一次,默认on
sub_filter_type mime-type ...;      # 指定特定Content-Type类型的响应才替换,默认是text/html


addition模块:在响应前或者响应后增加内容,而增加内容的方式是通过新增子请求的响应完成。
默认未编译到nginx,--with-http_addition_module

指令:
add_before_body uri;
add_after_body uri;
addition_types text/html;

例子:
/var/www/html
    |-index.html    # "index"
    |-a.txt         # "a"

/var/www/addition   
    |-before.txt    # "before"
    |-after.txt     # "after"

server {
    listen 8080;
    server_name addition.zbpblog.com;
    root /var/www/html;
    
    location / {
        add_before_body /before;
        add_after_body /after;
        addition_types *;   # 表示所有Content-Type类型的响应都可以增加内容
    }
    
    location /before {
        proxy_pass http://127.0.0.1:8090/before.txt;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Original-URI $request_uri;
    }
    
    location /after {
        proxy_pass http://127.0.0.1:8090/after.txt;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Original-URI $request_uri;
    }
}

server {
    listen 8090;
    root /var/www/addition;     # 该目录内含before.txt和after.txt
    
}

访问 http://addition.zbpblog.com:8080/a.txt
结果为:

before
a
after

 




更多内容请关注微信公众号
zbpblog微信公众号

如果您需要转载,可以点击下方按钮可以进行复制粘贴;本站博客文章为原创,请转载时注明以下信息

张柏沛IT技术博客 > Nginx HTTP模块篇 日志记录之log模块和过滤模块 (十六)

热门推荐
推荐新闻