您的位置:首页 > 理论基础 > 计算机网络

nginx之ngx_http_map_module模块使用

2016-06-19 18:04 441 查看

一,官方使用文档

       官方使用文档路径:http://nginx.org/en/docs/http/ngx_http_map_module.html

#使用格式$http_user_agent请求头参数值,agent变量名
map $http_user_agent $agent{
default       0;
"~Opera Mini" 1;
"Chrom.*" 2;
"\Firefox" 3;
}
#相关符号作用介绍
~:表示区分大小写匹配
~*:表示不区分大小写匹配
*:通配符匹配
\:表示精确匹配字符串
如果都没有匹配到,则返回定义的default value;

 

二,使用案例介绍

     需求背景:架构流程(client web -> haproxy->nginx->tomcat),需要nginx在打印日志时,过滤掉haproxy的链路检测访问日志,防止记录一些无用的日志,导致磁盘空间的浪费。

     配置如下:

#nginx.conf配置如下
http {
#使用log_format命令,定义日志输出的格式,变量名为 logFormat
log_format  logFormat  '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

#使用map命令,定义满足过滤条件的变量loggable
map $request $loggable {
default 1;
"~GET /.+/monitor\.html HTTP/.+" 0;
}

server {
listen       80;
server_name  localhost;
#综合使用上述定义的变量
access_log  logs/nginx.access.log logFormat if=$loggable;
error_log   logs/nginx.error.log logFormat;
}
}

 

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: