Nginx log function introduction

foreword

  • The log is very important for the program, and can be used for troubleshooting, recording the running status of the program, and so on. A good log can greatly improve the programmer's troubleshooting efficiency. Nginx mainly has two parts : visitor log and error log .

Set up visitor logs

  • The visitor log mainly records some request and response information when the client accesses the Nginx server. The official document explains the visitor log: access_log
  • To set the visitor log, you need to set the following information in the nginx configuration file /usr/local/nginx/conf/nginx.conf, which can be set under the http node or under the server node. By default, the log has been set under the http node.
  •   # 设置日志具体格式
      log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                        '$status $body_bytes_sent "$http_referer" '
                        '"$http_user_agent" "$http_x_forwarded_for"';
    
      # 设置日志存储位置
      access_log  logs/access.log  main;
    
  • parameter explanation
    • $remote_addr : IP address of the client accessing the website
    • $remote_user : client user name
    • $time_local : access time and time zone
    • $request : user's http request start line information
    • $status : http response status code
    • $body_bytes_sent : The number of bytes in the response body sent by the server to the client
    • $http_referer : record which link the request came from
    • $http_user_agent : record client access information
    • $http_x_forwarded_for : When there is a proxy server on the front end, set the web node to record the configuration of the client address. The premise for this parameter to take effect is that the proxy server also needs to perform related x_forwarded_for settings.
  • The log is recorded in log/access.log, once a request is sent, an access log will be generated. I sent a GET request with postman and Google Chrome respectively, which can be compared with the fields set above. If any field does not get data, a short horizontal line will be displayed.
  • postman request
  •   192.168.206.1 - - [27/May/2023:19:59:15 -0700] "GET / HTTP/1.1" 200 634 "-" "PostmanRuntime/7.32.2" "-"
    
  • Google Chrome request
  •   192.168.206.1 - - [27/May/2023:20:06:02 -0700] "GET / HTTP/1.1" 200 634 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" "-"
    

built-in variable

Turn off visitor log

  • If you want to close the log, change the log storage location behind access_log to off, and reload the configuration to close it.
  •   access_log  off;
    

set error log

  • The error log is set globally by default, and can also be written to the http node or server node.
  •   error_log  logs/error.log error;
    
  • Parameter 1 indicates the log storage directory, and parameter 2 indicates the log level. The log levels are as follows, the default is error
    • debug info notice warn error crit alert

Guess you like

Origin blog.csdn.net/new9232/article/details/130910096