如何监视NGINX服务器

NGINX is becoming an increasingly popular HTTP server and has been deployed in more than 400 million sites. If left unattended, issues may crop up from time to time. As such it is imperative to monitor NGINX server regularly for performance and troubleshooting point of view.

NGINX正在成为越来越流行的HTTP服务器,并已部署在超过4亿个站点中。 如果无人看管,问题可能会不时出现。 因此,必须定期监视NGINX服务器的性能和故障排除角度。

Let us look at few most common and easy procedures that you can undertake regularly to monitor and troubleshoot an NGINX server effectively.

让我们看一下您可以定期进行的一些最常见,最简单的过程,以有效地监视NGINX服务器并进行故障排除。

先决条件 (Prerequisites)

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

    通过从此处开始遵循我们的教程,您已经在服务器中安装了NGINX。

启用NGINX访问和错误日​​志 (Enable NGINX access and error log)

Enable access logs in NGINX to monitor real-time traffic. To do that, edit the Nginx configuration file and add the following access_log directive.

在NGINX中启用访问日志以监视实时流量。 为此,请编辑Nginx配置文件并添加以下access_log指令。

server {
...
...
        access_log /var/log/nginx/access_log combined;
...
...

}

It is also possible that there is not enough information logged to the error log. In that case, change the log level of the error_log directive to debug in the NGINX configuration file.

也有可能没有足够的信息记录到错误日志中。 在这种情况下,请更改error_log指令的日志级别以在NGINX配置文件中进行调试。

server {
...
...
         error_log /var/log/nginx/error_log debug;
...
...
}

Once you are done with enabling access log and log level of error log to debug, restart Nginx and tail the log file to see them in real time.

完成启用访问日志和错误日志的日志级别调试后,请重新启动Nginx并尾随日志文件以实时查看它们。

# systemctl restart nginx
# tail -f /var/log/nginx/access_log
# tail -f /var/log/nginx/error_log

启用Nginx状态页面 (Enable Nginx Status Page)

The collection of NGINX metrics is paramount for monitoring it effectively. These metrics provide valuable insights on the activity of NGINX servers like request per second, response time, active connections and more, thereby enabling you to preempt any possible issues that your NGINX server will probably face.

NGINX指标的收集对于有效监控它至关重要。 这些指标可提供有关NGINX服务器活动的宝贵见解,例如每秒请求,响应时间,活动连接等,从而使您能够避免NGINX服务器可能面临的任何可能的问题。

The ngx_http_stub_status_module (stub_status) module does this for you by exposing information about NGINX activity. This module is not compiled by default if you have installed NGINX by compiling from source. Just use the following command to find if NGINX have been built with stub_status module:

ngx_http_stub_status_module (stub_status)模块通过公开有关NGINX活动的信息来为您执行此操作。 如果您通过源代码编译安装了NGINX,则默认情况下不会编译该模块。 只需使用以下命令来查找NGINX是否已使用stub_status模块构建:

# nginx -V 2>&1 | grep -o with-http_stub_status_module

If the above command produces a blank output then you need to rebuild NGINX from source by including --with-http_stub_status_module parameter to the configure script.

如果以上命令产生空白输出,则需要通过在configure脚本中包含--with-http_stub_status_module参数来从源代码重建NGINX。

Now enable the status module on nginx virtual host file by adding the following location block. Make sure this page is accessible to only you by adding allow and deny directive.

现在,通过添加以下位置块,在nginx虚拟主机文件上启用状态模块。 通过添加allowdeny指令,确保只有您可以访问此页面。

...
...
location /nginx_status {
        stub_status on;
        allow 127.0.0.1;
        deny all;
}
...
...

Reload NGINX to apply new settings:

重新加载NGINX以应用新设置:

# systemctl restart nginx

Now to get the status metrics, make a CURL query to the NGINX server using the URL https://SERVER_IP/nginx_status, An output like below confirms that the status page is configured correctly:

现在要获取状态度量,请使用URL https://SERVER_IP/nginx_status对NGINX服务器进行CURL查询,如下所示的输出确认状态页面已正确配置:

# curl https://127.0.0.1/nginx_status
Active connections: 1
server accepts handled requests
 549 549 21583
Reading: 0 Writing: 1 Waiting: 0

The Nginx status page will provide you with real-time data about Nginx’s health enabling you to tweak Nginx configuration if needed.

Nginx状态页面将为您提供有关Nginx健康状况的实时数据,使您可以根据需要调整Nginx配置。

Therefore by monitoring NGINX metrics, you will have valuable insights of both active and rudimentary issues within your nginx server.

因此,通过监视NGINX指标,您将对nginx服务器中的活动和基本问题都有宝贵的见解。

使用Amplify监视NGINX (Monitor NGINX with Amplify)

NGINX Amplify is a fantastic monitoring tool for NGINX and NGINX Plus. Using NGINX Amplify one can view the system and NGINX performance metrics in a visual dashboard. Amplify enables you to not only monitor the performance of your application but also monitor the overall health and status of your NGINX server.

NGINX Amplify是用于NGINX和NGINX Plus的出色监视工具。 使用NGINX Amplify可以在可视仪表板中查看系统和NGINX性能指标。 Amplify使您不仅可以监视应用程序的性能,还可以监视NGINX服务器的整体运行状况和状态。

创建扩大账户 (Create amplify account)

To start using amplify, visit the home page of NGINX amplify and create an account. Once the account creation process completes successfully, a popup screen will provide you with the download URL of Amplify along with API key.

要开始使用amplify,请访问NGINX amplify的主页并创建一个帐户。 帐户创建过程成功完成后,将弹出一个屏幕,向您提供Amplify的下载URL以及API密钥。

Create Amplify Account

Create Amplify Account

创建扩大账户

Just copy the installation command of Amplify in a notepad and Click ‘Continue’. Wait for a minute, while Amplify connects to the remote NGINX instance.

只需在记事本中复制Amplify的安装命令,然后单击“继续”。 等待一会儿,同时Amplify连接到远程NGINX实例。

安装放大 (Install amplify)

Head back to the terminal to download and run the installation script in the terminal using following commands in the terminal.

返回终端以使用终端中的以下命令在终端中下载并运行安装脚本。

# wget https://github.com/nginxinc/nginx-amplify-agent/blob/master/packages/install.sh
# chmod u+x install.sh
# API_KEY='fae543a26f7664f84c61846f66d998f7' sh ./install.sh
Successful Installation Of Amplify

Successful Installation Of Amplify

成功安装放大

You will get a confirmation of the successful installation of amplify in your server just like above.

就像上面一样,您将获得在服务器中成功安装amplify的确认。

添加stub_status页面 (Add stub_status page)

Amplify makes use of the stub_status module to access the NGINX server URL via HTTP. It does this by looking for a location block containing stub_status directive in the NGINX configuration file.

Amplify利用stub_status模块通过HTTP访问NGINX服务器URL。 它通过在NGINX配置文件中查找包含stub_status指令的位置块来完成此操作。

# vi your_nginx_virtual_host.conf
server {
         ...
         ...    
         location /nginx_status {
             stub_status;
             allow 127.0.0.1;
             deny all;
       }
       ...
       ...
}

配置日志记录格式 (Configure logging format)

It is also possible to log the metrics that you are interested in. To do that, Create a custom log format and just add it in the HTTP section of your NGINX configuration file. The following section defines a custom log format by the name custom_format that can be used in any NGINX server block by specifying its name with access_log directive.

也可以记录您感兴趣的度量。为此,请创建一个自定义日志格式,然后将其添加到NGINX配置文件的HTTP部分中。 下一节通过名称custom_format定义了一个自定义日志格式,该名称可以通过使用access_log指令指定其名称来在任何NGINX服务器块中使用。

http {
...
...
log_format custom_format '$remote_addr - $remote_user [$time_local]"$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" rt=$request_time rt="$upstream_response_time"';
...
...
}

Next add access_log and error_log directive in your specific NGINX virtual host file

接下来,在您的特定NGINX虚拟主机文件中添加access_logerror_log指令

# vi your_nginx_virtual_host.conf
server {
...
...
    access_log  /var/log/nginx/access.log  custom_format;
    error_log  /var/log/nginx/error.log warn;
...
...
}

Check the configuration files for any syntactical error and restart NGINX.

检查配置文件中是否存在语法错误,然后重新启动NGINX。

# nginx -t
# systemctl restart nginx

The metrics will be available immediately in the amplify dashboard. Use the tabs at the top of the page to view the metrics that are appropriate for you.

这些指标将立即在放大仪表板中提供。 使用页面顶部的标签查看适合您的指标。

NGINX Amplify Dashboard

NGINX Amplify Dashboard

NGINX放大仪表板

使用ngxtop监视NGINX日志 (Monitor NGINX logs with ngxtop)

ngxtop is another awesome utility that can parse nginx access log and outputs few important metrics about nginx server.

ngxtop是另一个很棒的实用程序,可以解析nginx访问日志并输出有关nginx服务器的一些重要指标。

Using ngxtop command, you can view the summary of request count, requested URI, the number of request by status code and much more.

使用ngxtop命令,您可以查看请求计数的摘要,请求的URI,状态代码的请求数等等。

Once you have executed ngxtop, you will notice that it looks much like top output but with Nginx related information.

一旦执行了ngxtop,您将注意到它看起来很像top输出,但是带有Nginx相关信息。

To install ngxtop in Ubuntu 18.04, you need to install Python PIP beforehand. Once you are done with installing python pip, use it to install ngxtop in your server.

要在Ubuntu 18.04中安装ngxtop,您需要事先安装Python PIP 。 安装完python pip后,请使用它在服务器中安装ngxtop。

# apt-get install python-pip
# pip install ngxtop

Type ngxtop command to view the summary of request count, requested URI, the number of request by status code.

键入ngxtop命令以按状态代码查看请求计数,请求的URI和请求数的摘要。

# ngxtop

Find more about usages on ngxtop from here.

此处查找有关ngxtop的更多用法。

使用日志分析服务 (Use Log Analytics Service)

Finally, You can also choose a log analytics service from a wide range of options that are available freely in the market to parse and monitor NGINX logs.

最后,您还可以从市场上免费提供的多种选项中选择一种日志分析服务,以分析和监视NGINX日志。

For example, using syslog-ng or filebeat you can store and forward NGINX logs to any log-analytics services. This enables you to create a separate dedicated instance for processing NGINX logs.

例如,使用syslog-ngfilebeat,您可以存储NGINX日志并将其转发到任何日志分析服务。 这使您可以创建一个单独的专用实例来处理NGINX日志。

Once the logs are parsed and filtered, just create dashboards or generate alerts using the log analytics service.

解析和过滤日志后,只需使用日志分析服务创建仪表板或生成警报。

Therefore, by using any log analytic service it is possible to monitor NGINX logs in real time and take corrective measures if some anomalies are detected.

因此,通过使用任何日志分析服务,可以实时监视NGINX日志并在发现某些异常时采取纠正措施。

摘要 (Summary)

Although we have covered only a few options to monitor NGINX servers. If implemented properly, it will definitely improve the monitoring capabilities of NGINX servers at your disposal. You are now ready to monitor NGINX servers in your infrastructure by using any of the above-mentioned methods.

尽管我们仅介绍了监视NGINX服务器的几种方法。 如果实施得当,它肯定会提高您对NGINX服务器的监视能力。 现在,您可以使用任何上述方法来监视基础结构中的NGINX服务器。

翻译自: https://www.journaldev.com/26215/monitor-nginx-server

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值