Implementation of Nginx filtering access logs of static resource files

Implementation of Nginx filtering access logs of static resource files

Messy log

Nginx in daily use is mostly used as both a static resource server and a reverse proxy server. In particular, considering cross-domain issues, the same listening port is used for static resources and backend interfaces. If no filtering is performed, a large number of requests for static resources such as js, css, jpg, etc. will be seen in access_log, which will affect the viewing of backend interface call logs.

I didn't pay much attention to this thing at first, but when I was browsing an article about Nginx optimization, I found a method to use map to define a parameter of whether to write logs. Combined with the recent use of map for dynamic cross-domain configuration, I simply learned and recorded another usage scenario of map.

Use map to filter logs for accessing static resource files

http {
  log_format main '$remote_addr [$time_local] $request $status '
           'uct="$upstream_connect_time" rt="$request_time"';

  map $uri $not_static {
    default 1;
    ~^(.*\.(gif|jpg|jpeg|png|bmp|swf|js|css|woff|ttf)$) 0;
  }

  server {
    listen 23456;
    server_name localhost;
    access_log logs/test.log main if=$not_static;
  }
}

Explanation:

  • Customize a log_format, marked as main
  • Match the URI in the request. If the resource ends with gif, jpg, css, js, etc., $not_static is 0, otherwise it is 1
  • For requests to access port 23456, access_log specifies the use of a custom log format identified as main, and logs are recorded only when $not_static is 1. For more information about the if parameter, refer to the official documentation.
  • One thing to note is that when using the if parameter in access_log, you must explicitly specify a log_format, otherwise an error will be reported: nginx: [emerg] unknown log format "if=$not_static"

Another way to write dynamic and static separated logs

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|woff|ttf)$ {
  #access_log off; #Do not output the log of access to static resources access_log logs/static_resources.log;
}

This is the end of this article about the implementation of Nginx access log filtering of static resource files. For more relevant Nginx access log content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Use nginx-http-concat module to merge static resource files in nginx
  • How to deploy nginx with Docker and modify the configuration file
  • Detailed explanation of how to find the location of the nginx configuration file
  • Solution to the unknown directive that cannot be opened due to Nginx configuration file problems
  • How to view nginx configuration file path and resource file path

<<:  Vue uses the video tag to implement video playback

>>:  MySQL uses the truncate command to quickly clear all tables in a database

Recommend

Detailed explanation of mysql partition function and example analysis

First, what is database partitioning? I wrote an ...

js canvas to realize the Gobang game

This article shares the specific code of the canv...

How to install Elasticsearch7.6 cluster in docker and set password

Table of contents Some basic configuration About ...

Windows 10 1903 error 0xc0000135 solution [recommended]

Windows 10 1903 is the latest version of the Wind...

Summary of 4 solutions for returning values ​​on WeChat Mini Program pages

Table of contents Usage scenarios Solution 1. Use...

How to collect Nginx logs using Filebeat

Nginx logs can be used to analyze user address lo...

HTML line spacing setting methods and problems

To set the line spacing of <p></p>, us...

How to manually encapsulate paging components in Vue3.0

This article shares the specific code of the vue3...

HTML basic summary recommendation (title)

HTML: Title Heading is defined by tags such as &l...

Two ways to exit bash in docker container under Linux

If you want to exit bash, there are two options: ...

uniapp Sample code for implementing global sharing of WeChat mini-programs

Table of contents Create a global shared content ...