การเฝ้าระวังสถานะของเซิร์ฟเวอร์ Nginx เป็นหนึ่งในภารกิจสำคัญของผู้ดูแล VPS และ Cloud Server ทุกคน เมื่อเซิร์ฟเวอร์รับ traffic มากขึ้น การดูสถานะแบบ real-time จะช่วยให้คุณเห็นปัญหาก่อนที่มันจะกลายเป็นเรื่องร้ายแรง Nginx Status Module (หรือ stub_status) คือเครื่องมือที่มาพร้อมกับ Nginx ซึ่งช่วยให้คุณตรวจสอบจำนวน connection ที่กำลังทำงาน สถิติคำขออื่นๆ และข้อมูลสำคัญอื่นๆ ในทันทีทันใด
บทความนี้จะสอนคุณจากการติดตั้ง stub_status module จนถึงการสร้าง dashboard สำหรับการเฝ้าระวังแบบขั้นสูง เพื่อให้คุณสามารถควบคุมเซิร์ฟเวอร์ของคุณได้อย่างมีประสิทธิภาพ
Nginx stub_status Module คืออะไร
Nginx stub_status module เป็นมอดูลในตัวของ Nginx ที่ออกแบบมาเพื่อให้ข้อมูลทางสถิติแบบ real-time เกี่ยวกับการทำงานของเซิร์ฟเวอร์ มันไม่เหมือนกับ Nginx Plus (รุ่นชำระเงิน) ที่มีฟีเจอร์การเฝ้าระวังขั้นสูง แต่ stub_status ก็เพียงพอสำหรับการติดตามพื้นฐาน
ข้อมูลที่ stub_status ให้มาอาจมีดังนี้:
- จำนวน connections ที่กำลังเปิด (active connections)
- จำนวน requests ที่เซิร์ฟเวอร์ยอมรับทั้งหมด (accepts)
- จำนวน connections ที่เซิร์ฟเวอร์ประมวลผลทั้งหมด (handled)
- จำนวน requests ทั้งหมด (requests)
- connections ที่กำลังอ่านข้อมูลจาก client (reading)
- connections ที่กำลังเขียนข้อมูลไปยัง client (writing)
- connections ที่รอการประมวลผล (waiting)
การตรวจสอบและเปิดใช้ stub_status Module
ขั้นแรก ให้ตรวจสอบว่า Nginx ของคุณได้รับการคอมไพล์พร้อมกับ stub_status module หรือไม่ โดยรันคำสั่งต่อไปนี้:
nginx -V 2>&1 | grep -o "with-http_stub_status_module"
หากคำสั่งส่งออก “with-http_stub_status_module” แสดงว่ามอดูลนี้พร้อมใช้งาน หากไม่ได้ผล คุณอาจต้องติดตั้ง Nginx ใหม่ด้วย stub_status module
หากใช้ package manager เช่น apt บน Ubuntu/Debian คุณสามารถติดตั้งได้โดย:
sudo apt update
sudo apt install nginx
Package ของ Nginx จาก repository ส่วนใหญ่มักมา พร้อม stub_status module อยู่แล้ว
การตั้งค่า Nginx Status Endpoint
หลังจากยืนยันว่ามี stub_status module แล้ว ขั้นต่อไปคือการตั้งค่า endpoint เพื่อแสดงสถานะ เปิดไฟล์ configuration ของ Nginx:
sudo nano /etc/nginx/sites-available/default
ค้นหาบล็อก server{} และเพิ่มบล็อก location สำหรับ status endpoint:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# Nginx Status Endpoint
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
allow ::1;
deny all;
}
# Your other configurations...
}
การตั้งค่านี้เปิดให้ access เฉพาะจาก localhost (127.0.0.1 และ ::1) เท่านั้น หากต้องการเข้าถึงจากที่อื่นก็สามารถเพิ่ม IP address ได้
หลังจากแก้ไข ให้ test configuration และ reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
ตอนนี้ลองทดสอบ endpoint ด้วย curl:
curl http://localhost/nginx_status
การอ่านและเข้าใจ Nginx Status Output
ผลลัพธ์จาก status endpoint จะมีลักษณะดังนี้:
active connections: 42
server accepts handled requests
1234 1230 5678
Reading: 5 Writing: 10 Waiting: 27
มาทำความเข้าใจแต่ละส่วน:
- active connections: จำนวน connections ที่เปิดอยู่ในขณะนี้ รวมถึงการเชื่อมต่อที่รอ
- server accepts: จำนวน TCP connection requests ที่ server ยอมรับทั้งหมด
- handled: จำนวน connection ที่เซิร์ฟเวอร์ประมวลผลทั้งหมด (ปกติจะเท่ากับ accepts)
- requests: จำนวน requests ที่ได้รับทั้งหมด (requests ต่อ connection อาจมีหลายรายการ)
- Reading: จำนวน connections ที่ Nginx กำลังอ่านคำขอจาก client
- Writing: จำนวน connections ที่ Nginx กำลังเขียนคำตอบให้ client
- Waiting: จำนวน connections ที่ว่าง (idle) รอคำขอใหม่
การจำกัดการเข้าถึง Status Page
ความปลอดภัยคือสิ่งสำคัญ หากต้องการให้ IP address บางตัวเข้าถึง status page สามารถระบุได้ในการตั้งค่า:
location /nginx_status {
stub_status on;
access_log off;
# Allow localhost
allow 127.0.0.1;
allow ::1;
# Allow monitoring server
allow 192.168.1.100;
# Allow office network
allow 203.0.113.0/24;
# Deny everything else
deny all;
}
การติดตามสถานะด้วย Bash Script
ทำให้การเฝ้าระวังเป็นอัตโนมัติ โดยสร้าง bash script เพื่อตรวจสอบสถานะ:
#!/bin/bash
# nginx_monitor.sh
URL="http://localhost/nginx_status"
THRESHOLD=100
while true; do
STATUS=$(curl -s $URL)
ACTIVE=$(echo "$STATUS" | grep "active" | awk '{print $3}')
echo "$(date): Active connections: $ACTIVE"
if [ "$ACTIVE" -gt "$THRESHOLD" ]; then
echo "WARNING: Active connections exceeded threshold!"
# Send alert via email or webhook
fi
sleep 10
done
รัน script นี้ในพื้นหลังเพื่อติดตามอย่างต่อเนื่อง
การใช้ Python สำหรับการวิเคราะห์สถานะที่ลึกขึ้น
สำหรับการวิเคราะห์ที่ซับซ้อนมากขึ้น ลองใช้ Python script:
#!/usr/bin/env python3
import requests
import json
from datetime import datetime
def get_nginx_status(url="http://localhost/nginx_status"):
response = requests.get(url)
lines = response.text.strip().split('\n')
status = {
'active_connections': int(lines[0].split()[2]),
'accepts': int(lines[2].split()[0]),
'handled': int(lines[2].split()[1]),
'requests': int(lines[2].split()[2]),
'reading': int(lines[3].split()[1]),
'writing': int(lines[3].split()[3]),
'waiting': int(lines[3].split()[5]),
'timestamp': datetime.now().isoformat()
}
return status
if __name__ == "__main__":
data = get_nginx_status()
print(json.dumps(data, indent=2))
การรวมกับเครื่องมือการเฝ้าระวังระดับมืออาชีพ
นอกเหนือจากการตรวจสอบด้วยตนเอง Nginx status module สามารถรวมเข้ากับเครื่องมือการเฝ้าระวังที่มีชื่อเสียง เช่น:
- Prometheus: ใช้ Nginx Prometheus exporter เพื่อเก็บข้อมูล metrics
- Grafana: สร้าง dashboard ทำให้สามารถมองเห็นข้อมูล metrics ได้ชัดเจน
- Datadog: รวมข้อมูล Nginx เข้ากับระบบการเฝ้าระวังกลาง
- New Relic: ติดตามประสิทธิภาพของ Nginx ในจำนวนเงิน subscription
- Zabbix: ระบบการเฝ้าระวังแบบ open-source ที่มีความสามารถสูง
ตัวอย่าง: Prometheus Exporter สำหรับ Nginx
ถ้าใช้ Prometheus คุณสามารถใช้ nginx-module-vts หรือ prometheus-nginx-module เพื่อแยกข้อมูลรายละเอียด
curl http://your-monitoring-server:9113/metrics | grep nginx
Nginx Plus Extended Status
สำหรับสภาพแวดล้อมที่ต้องการข้อมูลรายละเอียดมากขึ้น Nginx Plus (paid version) ให้ extended status API ที่มี metrics เพิ่มเติม เช่น:
- Per-location statistics
- Per-upstream server metrics
- Cache performance data
- Load balancing information
- SSL/TLS statistics
หากใช้ Nginx Plus endpoint สามารถเข้าถึงข้อมูลได้ดังนี้:
curl http://localhost/api/9/nginx
การสร้าง Dashboard ง่ายสำหรับการเฝ้าระวัง
ถ้าต้องการ dashboard ที่เรียบง่าย เป็นไปได้ที่จะสร้าง HTML page ด้วย JavaScript เพื่อแสดงสถานะ real-time:
<!DOCTYPE html>
<html>
<head>
<title>Nginx Status Dashboard</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.stat { display: inline-block; margin: 10px; padding: 10px; border: 1px solid #ccc; }
.stat-value { font-size: 24px; font-weight: bold; }
</style>
</head>
<body>
<h1>Nginx Status Dashboard</h1>
<div id="stats"></div>
<script>
function updateStatus() {
fetch('/nginx_status')
.then(response => response.text())
.then(data => {
let lines = data.trim().split('\n');
let active = lines[0].split()[2];
let reading = lines[3].split()[1];
let writing = lines[3].split()[3];
let waiting = lines[3].split()[5];
document.getElementById('stats').innerHTML = `
<div class="stat">
<div>Active</div>
<div class="stat-value">${active}</div>
</div>
<div class="stat">
<div>Reading</div>
<div class="stat-value">${reading}</div>
</div>
<div class="stat">
<div>Writing</div>
<div class="stat-value">${writing}</div>
</div>
<div class="stat">
<div>Waiting</div>
<div class="stat-value">${waiting}</div>
</div>
`;
});
}
setInterval(updateStatus, 5000);
updateStatus();
</script>
</body>
</html>
Health Check ตาม Nginx Status
สามารถใช้ status module เพื่อสร้าง health check endpoint ที่จะบอกว่าเซิร์ฟเวอร์ normal หรือไม่:
#!/bin/bash
# health_check.sh
ACTIVE=$(curl -s http://localhost/nginx_status | grep "active" | awk '{print $3}')
# Set threshold
THRESHOLD=500
if [ "$ACTIVE" -lt "$THRESHOLD" ]; then
echo "Server is healthy"
exit 0
else
echo "Server is overloaded"
exit 1
fi
สามารถใช้ script นี้กับ load balancer หรือ monitoring tool เพื่อ auto-scaling
การทำให้ Alert อัตโนมัติ
สร้าง script ที่ส่ง notification เมื่อเซิร์ฟเวอร์มีปัญหา:
#!/bin/bash
# alert_monitor.sh
WEBHOOK_URL="https://hooks.slack.com/services/YOUR/WEBHOOK/URL"
THRESHOLD_ACTIVE=200
THRESHOLD_READING=50
STATUS=$(curl -s http://localhost/nginx_status)
ACTIVE=$(echo "$STATUS" | grep "active" | awk '{print $3}')
READING=$(echo "$STATUS" | grep "Reading" | awk '{print $2}')
if [ "$ACTIVE" -gt "$THRESHOLD_ACTIVE" ]; then
curl -X POST $WEBHOOK_URL \
-d '{"text":"Alert: High active connections: '"$ACTIVE"'"}'
fi
if [ "$READING" -gt "$THRESHOLD_READING" ]; then
curl -X POST $WEBHOOK_URL \
-d '{"text":"Alert: High reading connections: '"$READING"'"}'
fi
ข้อพิจารณาด้านความปลอดภัย
- ไม่เปิด Status Endpoint ต่อ Public: จำกัดการเข้าถึงเฉพาะ localhost หรือ IP ที่เชื่อถือได้
- ใช้ HTTPS สำหรับการเข้าถึงจากระยะไกล: ถ้าต้องเข้าถึง status จากเซิร์ฟเวอร์อื่น ให้ใช้ reverse proxy ที่มี SSL/TLS
- ปิด access_log: ลดการบันทึกสำหรับ status endpoint เพื่อลดค่าใช้จ่าย I/O
- ติดตั้ง Web Application Firewall: ป้องกันการ DDoS และการโจมตีอื่นๆ
- ตรวจสอบการเปลี่ยนแปลงของสถานะ: ถ้าสถานะเปลี่ยนแปลงอย่างรวดเร็ว อาจบ่งชี้ถึงปัญหาด้านความปลอดภัย
Best Practices สำหรับการใช้ Nginx Status Module
- วางแผนการเฝ้าระวังล่วงหน้า: ตัดสินใจว่าต้องติดตามข้อมูลใดบ้าง
- ตั้งค่า Alert อย่างสมเหตุสมผล: หลีกเลี่ยง False positives ที่มากเกินไป
- เก็บ Historical Data: เปรียบเทียบประสิทธิภาพตามเวลา
- ใช้ Rate Limiting: ลดจำนวน requests ต่อ connection เพื่อป้องกันการใช้ resources มากเกินไป
- Tune Nginx Configuration: ปรับ worker_connections และ keepalive_timeout ตามความต้องการ
- รวมกับ Load Balancer: ใช้ status module เพื่อตรวจสอบสุขภาพของ backend servers
- ปิด Status Module ในสภาพแวดล้อม Production ที่ไม่จำเป็น: ถ้าไม่ใช้ ก็ปิดเสีย
สรุป
Nginx Status Module (stub_status) เป็นเครื่องมือที่ง่ายแต่ทรงพลังสำหรับการเฝ้าระวังเซิร์ฟเวอร์ของคุณ ด้วยการตั้งค่าขั้นพื้นฐาน เปิด endpoint /nginx_status และจำกัดการเข้าถึง คุณสามารถติดตามจำนวน connections และ requests ได้ทันทีทันใด
นอกจากนี้ คุณสามารถสร้าง automation scripts (Bash, Python) และรวมกับระบบการเฝ้าระวังที่ทรงพลังเช่น Prometheus + Grafana เพื่อให้ได้ข้อมูลที่ลึกขึ้นและตัดสินใจได้ดีขึ้น ไม่ว่าสภาพแวดล้อมเล็กหรือใหญ่ การเข้าใจสถานะของ Nginx จะช่วยให้คุณรักษาประสิทธิภาพและเพิ่มความเสถียรของเซิร์ฟเวอร์
แนะนำบริการ DE Cloud VPS และ Cloud Hosting
หากคุณกำลังเรียนรู้การเฝ้าระวัง Nginx และต้องการสภาพแวดล้อม VPS ที่เสถียรเพื่อทดลอง DE Cloud VPS เป็นทางเลือกที่ดีเยี่ยม ด้วยฮาร์ดแวร์ที่มีประสิทธิภาพสูง การอัปเกรด/ดาวน์เกรด resource ได้ง่าย และการสนับสนุน 24/7 DE ช่วยให้คุณมั่นใจในการตั้งค่า monitoring ได้อย่างเต็มที่
นอกจากนี้ DE Cloud Hosting ก็เหมาะสำหรับสถานการณ์ที่คุณต้องการการจัดการที่เรียบง่ายมากขึ้น โดยทีมของ DE จะจัดการ Nginx configuration และการเฝ้าระวังให้คุณ ทำให้คุณสามารถมุ่งเน้นไปที่การพัฒนาแอปพลิเคชันได้
ไม่ว่าคุณจะเลือก Cloud VPS หรือ Cloud Hosting บริการของ DE ต่างก็มาพร้อมกับ Nginx ที่ตั้งค่าอย่างดีและเตรียมไว้สำหรับการเฝ้าระวัง เยี่ยมชมเว็บไซต์ของ DE วันนี้เพื่อรู้เพิ่มเติม!

