要实时了解Nginx服务器的状态,您可以使用以下方法和工具:
/var/log/nginx/access.log
。/var/log/nginx/error.log
。在Nginx配置文件nginx.conf
中,您可以使用log_format
指令自定义日志格式,例如:
http {
log_format my_format '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'"$http_x_forwarded_for"';
access_log /var/log/nginx/access.log my_format;
}
grep "404" /var/log/nginx/access.log
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -n
tail -f /var/log/nginx/access.log
sudo apt-get install goaccess
goaccess /var/log/nginx/access.log -o report.html --log-format=COMBINED
Nginx提供了一个内置的状态信息监控页面,可以通过stub_status
模块实现。默认情况下,该功能是关闭的,需要在配置文件中开启并指定URI来访问数据。
server {
listen 80;
server_name default_server;
location /status {
stub_status on;
allow 127.0.0.1; # 允许的IP列表
}
}
重启Nginx后,可以通过浏览器访问http://<your-nginx-ip>/status
查看状态监控信息。
sudo pip install ngxtop
ngxtop
sudo apt-get install logwatch
logwatch
通过上述方法和工具,您可以有效地利用Nginx日志来监控服务器状态,确保服务的稳定运行。