如何阻止nginx记录Amazon Route 53运行状况检查请求?

时间:2014-01-14 10:40:28

标签: amazon-web-services nginx user-agent amazon-route53

目前,我的AWS运行状况检查正在无情地点击我的服务器:

...
54.228.16.40 - - [14/Jan/2014:10:17:22 +0000] "GET / HTTP/1.1" 301 178 "-" "Amazon Route 53 Health Check Service"
54.248.220.40 - - [14/Jan/2014:10:17:24 +0000] "GET / HTTP/1.1" 301 178 "-" "Amazon Route 53 Health Check Service"
54.232.40.110 - - [14/Jan/2014:10:17:25 +0000] "GET / HTTP/1.1" 301 178 "-" "Amazon Route 53 Health Check Service"
54.241.32.78 - - [14/Jan/2014:10:17:26 +0000] "GET / HTTP/1.1" 301 178 "-" "Amazon Route 53 Health Check Service"
54.245.168.46 - - [14/Jan/2014:10:17:28 +0000] "GET / HTTP/1.1" 301 178 "-" "Amazon Route 53 Health Check Service"
54.251.31.174 - - [14/Jan/2014:10:17:28 +0000] "GET / HTTP/1.1" 301 178 "-" "Amazon Route 53 Health Check Service"
...

我想将NginX配置为不使用"Amazon Route 53 Health Check Service"的用户代理记录任何请求。

我目前的尝试如下:

# default server for forwarding all requests over to main www domain
server {
    listen 80 default_server;

    server_name _;

    return 301 $scheme://www.example.com$request_uri;
}

# server configured to catch aws health check requests
server {
    listen 80;
    server_name 12.345.67.89;

    location / {
        if ( $http_user_agent ~* 'Amazon Route 53 Health Check Service' ) {
            access_log off;
            return 200 'Service OK';      
        }
    }
}

# actual application server
server {
    listen 80;
    server_name www.example.com;

    location / { 
        ...
    }
}

这看起来对我来说很好,事实上当我将CURL设置为健康检查设置为匹配的相同地址时:

curl --user-agent "Amazon Route 53 Health Check Service" http://12.345.67.89:80/

我得到了我期望的东西:

Service OK

我的请求不会在日志中结束。

但是,当这些请求来自实际的AWS运行状况检查时,我的日志会继续被这些请求所淹没。

关于我在哪里做错的任何想法?

由于

2 个答案:

答案 0 :(得分:1)

事实证明我的健康检查设置为example.com而非ip地址:我的不好。

为了记录,我通过将 $host 变量添加到我的日志格式(参见行尾)来发现这一点:

log_format debug_format '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for" host:"$host"';

access_log /var/log/nginx/access.log debug_format;

干杯无论如何

答案 1 :(得分:0)

我们可以映射NGINX设置的用户代理变量,并将值设置为布尔值,以便在定义访问日志路径和格式时使用。检查下面的nginx块以供参考。

map $http_user_agent $log_ua {

~Pingdom 0;
~Amazon-Route53 0;
~SomeOtherUA 0;

default 1;

}

server {

...

access_log /var/log/nginx/access.log main combined if=$log_ua;

}