NGINX访问日志和错误日志

Logs are very useful to monitor activities of any application apart from providing you with valuable information while you troubleshoot it.

日志对于监视任何应用程序的活动非常有用,除了在您排除故障时为您提供有价值的信息外。

Like any other application, NGINX also records events like visitors to your site, issues it encountered and more to log files. This information enables you to take preemptive measures in case you notice some serious discrepancies in the log events.

与其他任何应用程序一样,NGINX还会记录事件,例如您网站的访客,遇到的问题以及更多日志文件。 该信息使您能够采取先发制人的措施,以防万一您注意到日志事件中的某些严重差异。

This article will guide you in details about how to configure NGINX logging so that you have a better insight into its activities.

本文将指导您有关如何配置NGINX日志记录的详细信息,以便您更好地了解其活动。

先决条件 (Prerequisite)

  • You have already installed NGINX by following our tutorial from here.

    您已经按照此处的教程安装了NGINX。

登录NGINX (Logs in NGINX)

By default, NGINX writes its events in two types of logs – the error log and the access log. In most of the popular Linux distro like Ubuntu, CentOS or Debian, both the access and error log can be found in /var/log/nginx, assuming you have already enabled the access and error logs in the core NGINX configuration file.

默认情况下,NGINX将其事件写入两种类型的日志中: 错误日志访问日志 。 在大多数流行的Linux发行版(如Ubuntu,CentOS或Debian)中,访问和错误日​​志都可以在/var/log/nginx ,前提是您已经在核心NGINX配置文件中启用了访问和错误日​​志。

Let us find out more about NGINX access log, error log and how to enable them if you have not done it earlier.

让我们找到有关NGINX访问日志,错误日志以及如果您之前没有做过的话如何启用它们的更多信息。

什么是NGINX访问日志? (What is NGINX access log?)

The NGINX logs the activities of all the visitors to your site in the access logs. Here you can find which files are accessed, how NGINX responded to a request, what browser a client is using, IP address of clients and more. It is possible to use the information from the access log to analyze the traffic to find sites usages over time. Further, by monitoring the access logs properly, one can find out if a user is sending some unusual request for finding flaws in the deployed web application.

NGINX在访问日志中记录到您站点的所有访问者的活动。 在这里,您可以找到要访问的文件,NGINX如何响应请求,客户端使用的浏览器,客户端的IP地址等等。 可以使用访问日志中的信息来分析流量,以查找一段时间内的站点使用情况。 此外,通过适当地监视访问日志,可以发现用户是否正在发送一些异常请求来查找已部署的Web应用程序中的缺陷。

什么是NGINX错误日志? (What is NGINX error log?)

On the other hand, if NGINX faces any glitches then it will record the event to the error log. This may happen if there is some error in the configuration file. Therefore if NGINX is unable to start or abruptly stopped running then you should check the error logs to find more details. You may also find few warnings in the error log but it does not indicate that a problem has occurred but the event may pose a serious issue in the near future.

另一方面,如果NGINX遇到任何故障,则它将事件记录到错误日志中。 如果配置文件中存在某些错误,则可能会发生这种情况。 因此,如果NGINX无法启动或突然停止运行,则应检查错误日志以查找更多详细信息。 您可能还会在错误日志中找到很少的警告,但它并不表示已发生问题,但该事件可能会在不久的将来造成严重的问题。

如何启用NGINX访问日志? (How to enable NGINX access log?)

In general, the access log can be enabled with access_log directive either in http or in server section. The first argument log_file is mandatory whereas the second argument log_format is optional. If you don’t specify any format then logs will be written in default combined format.

通常,可以在http或服务器部分中使用access_log指令启用访问日志。 第一个参数log_file是必需的,而第二个参数log_format是可选的。 如果您未指定任何格式,则日志将以默认的组合格式写入。

access_log log_file log_format;

The access log is enabled by default in the http context of core NGINX configuration file. That means access log of all the virtual host will be recorded in the same file.

默认情况下,访问日志是在核心NGINX配置文件的http上下文中启用的。 这意味着所有虚拟主机的访问日志将记录在同一文件中。

http {
      ...
      ...
      access_log  /var/log/nginx/access.log;
      ...
      ...
}

It is always better to segregate the access logs of all the virtual hosts by recording them in a separate file. To do that, you need to override the access_log directive that is defined in the http section with another access_log directive in the server context.

最好通过将所有虚拟主机的访问日志记录在一个单独的文件中来隔离它们。 要做到这一点,你需要重写access_log是在HTTP部分定义与另一指令access_log在服务器方面的指令。

http {
      ...
      ...
      access_log  /var/log/nginx/access.log;
    
         server {
                  listen 80; 
                  server_name domain1.com
                  access_log  /var/log/nginx/domain1.access.log;
                  ...
                  ...
                }
}

Reload NGINX to apply the new settings. To view the access logs for the domain domain1.com in the file /var/log/nginx/domain1.access.log, use the following tail command in the terminal.

重新加载NGINX以应用新设置。 要在文件/var/log/nginx/domain1.access.log查看域domain1.com的访问日志,请在终端中使用以下tail命令。

# tail -f /var/log/nginx/domain1.access.log

在访问日志中应用自定义格式 (Apply Custom Format in Access Log)

The default log format used to record an event in the access log is combined log format. You can override the default behavior by creating your own custom log format and then specify the name of the custom format in the access_log directive.

用于在访问日志中记录事件的默认日志格式是组合日志格式。 您可以通过创建自己的自定义日志格式来覆盖默认行为,然后在access_log指令中指定自定义格式的名称。

The following example defines a custom log format by extending the predefined combined format with the value of gzip compression ratio of the response. The format is then applied by indicating the log format with the access_log directive.

以下示例通过使用响应的gzip压缩比值扩展预定义的组合格式来定义自定义日志格式。 然后通过使用access_log指令指示日志格式来应用格式。

http {
            log_format custom '$remote_addr - $remote_user [$time_local] '
                           '"$request" $status $body_bytes_sent '
                           '"$http_referer" "$http_user_agent" "$gzip_ratio"';

            server {
                    gzip on;
                    ...
                    access_log /var/log/nginx/domain1.access.log custom;
                    ...
            }
}

Once you have applied above log format in your environment, reload NGINX. Now tail the access log to find the gzip ratio at the end of the log event.

在您的环境中应用上述日志格式后,请重新加载NGINX。 现在,在日志事件的末尾尾随访问日志以找到gzip比率。

# tail -f /var/log/nginx/domain1.access.log
47.29.201.179 - - [28/Feb/2019:13:17:10 +0000] "GET /?p=1 HTTP/2.0" 200 5316 "https://domain1.com/?p=1" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36" "2.75"

如何启用NGINX错误日志? (How to enable NGINX error log?)

The error_log directive sets up error logging to file or stderr, or syslog by specifying minimal severity level of error messages to be logged. The syntax of error_log directive is:

error_log伪指令通过指定要记录的错误消息的最低严重性级别,将错误记录设置为文件,stderr或syslog。 error_log指令的语法为:

error_log log_file log_level;

The first argument log_file defines the path of the log file and the second argument log_level defines the severity level of the log event to be recorded. If you don’t specify the log_level then by default, only log events with a severity level of error are recorded.

第一个参数log_file定义日志文件的路径,第二个参数log_level定义要记录的日志事件的严重性级别。 如果未指定log_level,则默认情况下,仅记录严重性级别为error的日志事件。

For example, the following example sets the severity level of error messages to be logged to crit. Further, the error_log directive in the http context implies that the error log for all the virtual host will be available in a single file.

例如,以下示例将要记录的错误消息的严重性级别设置为crit 。 此外,http上下文中的error_log指令表示所有虚拟主机的错误日志将在单个文件中可用。

http {
       	  ...
	  error_log  /var/log/nginx/error_log  crit;
	  ...
}

It is also possible to record error logs for all the virtual host separately by overriding the error_log directive in the server context. The following example exactly does that by overriding error_log directive in the server context.

通过覆盖服务器上下文中的error_log指令,还可以单独记录所有虚拟主机的错误日志。 下面的示例通过在服务器上下文中重写error_log指令来实现此目的。

http {
       ...
       ...
       error_log  /var/log/nginx/error_log;
       server {
	        	listen 80;
		        server_name domain1.com;
       		        error_log  /var/log/nginx/domain1.error_log  warn;
                        ...
	   }
       server {
	        	listen 80;
		        server_name domain2.com;
      		        error_log  /var/log/nginx/domain2.error_log  debug;
                        ...
	   }
}

All the examples described above records the log events to a file. You can also configure the error_log directive for sending the log events to a syslog server. The following error_log directive sends the error logs to syslog server with an IP address of 192.168.10.11 in debug format.

上面描述的所有示例都将日志事件记录到文件中。 您还可以配置error_log指令,以将日志事件发送到syslog服务器。 以下error_log指令以调试格式将错误日志发送到IP地址为192.168.10.11的syslog服务器。

error_log syslog:server=192.168.10.11 debug;

In some situation, you might want to disable the error log. To do that, set the log file name to /dev/null.

在某些情况下,您可能要禁用错误日志。 为此,请将日志文件名设置为/dev/null

error_log /dev/null;

Nginx错误日志的严重性级别 (Nginx Error Log Severity Levels)

There are many types of log levels that are associated with a log event and with a different priority. All the log levels are listed below. In the following log levels, debug has top priority and includes the rest of the levels too. For example, if you specify error as a log level, then it will also capture log events those are labeled as crit, alert and emergency.

有许多类型的日志级别与日志事件相关联,并且具有不同的优先级。 下面列出了所有日志级别。 在以下日志级别中,调试具有最高优先级,并且还包括其余级别。 例如,如果您将错误指定为日志级别,则它将同时捕获标记为暴击,警报和紧急事件的日志事件。

  1. emerg: Emergency messages when your system may be unstable.

    emerg :系统可能不稳定时的紧急消息。
  2. alert: Alert messages of serious issues.

    alert :严重问题的警报消息。
  3. crit: Critical issues that need to be taken care of immediately.

    暴击 :需要立即处理的关键问题。
  4. error: An error has occured. Something went wrong while processing a page.

    错误 :发生错误。 处理页面时出了点问题。
  5. warn: A warning messages that you should look into it.

    警告 :警告消息,您应该对其进行调查。
  6. notice: A simple log notice that you can ignore.

    注意 :您可以忽略的简单日志通知。
  7. info: Just an information messages that you might want to know.

    info :您可能想知道的信息消息。
  8. debug: Debugging information used to pinpoint the location of error.

    debug :用于查明错误位置的调试信息。

摘要 (Summary)

The access and error logs in NGINX will not only keep a tab on users activity but also save your time and effort in the process of debugging. Further, you can also customize the access log if you need more information at your disposal. It is always better to enable access and error logs because these two files contain all the clues for better maintenance of the NGINX server.

NGINX中的访问和错误日​​志不仅可以保持用户活动的状态,而且可以节省调试过程中的时间和精力。 此外,如果您需要更多信息,也可以自定义访问日志。 最好启用访问日志和错误日志,因为这两个文件包含所有有关更好维护NGINX服务器的线索。

翻译自: https://www.journaldev.com/26756/nginx-access-logs-error-logs

你可能感兴趣的:(NGINX访问日志和错误日志)