詳細検索

Prevent nginx from outputting access logs for health checks

Avatar
by miyashitak
2 min read

Prevent nginx from outputting access logs for health checks
Translated from 日本語 • View original

Good morning. This is the Miyashita of infrastructure.
This time it will be a log-related setting for nginx.


Table of Contents

  1. introduction
  2. About ngx_http_geo_module
  3. Configuring nginx
  4. reference site

Introduction

I wanted to implement a setting in nginx that does not output logs from a specific NW using the definition of setenv, which is often implemented in apache, but it did not have the same function, so I looked it up. It can be used when it is convenient not to output to logs, such as checking the health of a load balancer or connecting from a monitoring server. It is not necessary for analysis, and it also reduces unnecessary DISK I/O.

What is ngx_http_geo_module

Official documentation

You can set the IP address as a variable as described in the documentation.
It is commonly used to use this module when NW in a particular country refuses to connect. In such cases, it is possible to manage it with a separate file that has been registered in large quantities by include.

Configuring nginx

nginx.conf.
[shell]# vi nginx.conf http { include mime.types; # not access_log IP's geo $no_log { default 0; 172.0.1.0/24 1; } server { (omitted) location / { root /var/www/html; index index.cgi index.php index.html index.htm; if ($no_log) { access_log off; } } }[/shell]

The basic syntax would be geo [$address] $variable { ... }. "default" is "0" for everything that is not included in the condition, and the IP specified is "1". For the location of the hierarchy you want to restrict, if ($variable) { access_log off; } If you write "1", the log corresponding to "1" will not be printed.

Finally, restart nginx.

[shell]# /etc/init.d/nginx restart[/shell]
Now access_log no longer shows connection logs from 172.0.1.0/24.
It seems that it can be done with serenv like Apache, but this was easier with enginx.

Reference Site

ngx_http_geo_module module nginx's geo module is too convenient

Related Articles