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.comaccess_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访问日志和错误日志相关推荐

  1. 日志php-error错误日志查看

    转载: https://blog.csdn.net/ty_hf/article/details/55505262 前言: 对于我们做php开发的人员,上了生产环境,一定要把相关debug,displa ...

  2. 日志-php-error错误日志查看

    前言: 对于我们做php开发的人员,上了生产环境,一定要把相关debug,display_errors错误提示等关掉.谁还难免不犯个错呢?这样能防止非致命性报错下,导致项目路径.数据库等信息泄漏. 问 ...

  3. mysql- 数据库的6种日志:错误日志、通用日志、慢日志、二进制日志、redo log、undo log

    MySQL日志 日志用来做什么? 1.用来排错 2.用来做数据分析 3.了解程序的运行情况,是否健康 日志放在哪里? 日志一般放在数据目录 日志分类 错误日志.通用日志.慢日志.二进制日志 一.错误日 ...

  4. logback日志配置(控制台日志、输出日志、错误日志)

    logback日志配置(控制台日志.输出日志.错误日志) 一.logback.xml相关配置: <?xml version="1.0" encoding="UTF- ...

  5. nginx php访问日志配置,nginx php-fpm 输出php错误日志的配置方法

    由于nginx仅是一个web 服务器,因此 nginx的access日志只有对访问页面的记录,不会有php 的 error log信息. nginx把对php的请求发给php-fpm fastcgi进 ...

  6. nginx php-fpm 输出php错误日志(转)

    nginx是一个web服务器,因此nginx的access日志只有对访问页面的记录,不会有php 的 error log信息. nginx把对php的请求发给php-fpm fastcgi进程来处理, ...

  7. nginx php-fpm 输出php错误日志

    nginx是一个web服务器,因此nginx的access日志只有对访问页面的记录,不会有php 的 error log信息. nginx把对php的请求发给php-fpm fastcgi进程来处理, ...

  8. Redis的Errorlog或者启动日志(错误日志)的配置

    Errorlog或者是运行日志是任何一个软件的运行中异常诊断必看的文件之一,折腾Redis的过程中以为有默认的错误日志(或启动日志),不过一直没有发现类似的日志文件, 在看了默认的配置文件之后,发现R ...

  9. 学习笔记(03):MySQL数据库运维与管理-01-mysql通用日志与错误日志

    立即学习:https://edu.csdn.net/course/play/10084/214943?utm_source=blogtoedu MYSQL日志管理 用于实现MYSQL数据库故障排查2, ...

最新文章

  1. JQuery $post函数
  2. 降序排序_新手需要掌握的Excel排序功能都在这儿了!
  3. 《数字视频和高清:算法和接口》一第2章 图像的采样和显示
  4. Spring Security 入门(五):在 Spring-Boot中的应用
  5. oracle与sqlserver差异,Oracle与SQLServer的SQL语法差异总结
  6. 背包例题の01,完全,多重
  7. c++ 不插入重复元素但也不排序_面试时写不出排序算法?看这篇就够了
  8. 网站内容批量抓取和《著作权法》
  9. 【美文赏析】世界上最遥远的距离
  10. Android 常用 adb 命令总结【转】
  11. jQuery 的CSS选择器 中 使用变量的方法
  12. 实现一个shared_ptr
  13. c语言计算总分和平均分float,用C语言编程平均分数
  14. nit计算机考试题目,NIT计算机考试大纲
  15. jmeter访问网址
  16. 绵阳python培训_绵怎么组词
  17. 字体图标svg改变颜色
  18. 认识计算机教案模板表格,word表格制作教学设计范文
  19. 【故障检测】基于 KPCA 的故障检测【T2 和 Q 统计指数的可视化】(Matlab代码实现)
  20. 如何开发一个酷炫的mdx

热门文章

  1. WPF自定义窗体仿新毒霸关闭特效(只能在自定义窗体中正常使用)
  2. Magento 获取分类的父分类和子分类
  3. [转载] python创建集合set()_python 之集合{}(Set)
  4. [转载] [Python图像处理] 二十二.Python图像傅里叶变换原理及实现
  5. Ubuntu 12.04 中文输入法的安装
  6. 启动Samples-Web-Start Web Server时,提示Could not open port 1080
  7. struts2与spring集成时,关于class属性及成员bean自动注入的问题
  8. NSDate NSTimerZone 时区转换
  9. [转载]Informix平安特征庇护数据的详细方法
  10. Asp.Net中OnClientClick与OnClick