Nginx 服務管理與監控工具推薦 提升 Web 伺服器效率與安全性方案

me
林彥成
2022-12-25 | 4 min.
文章目錄
  1. 1. 如何有效管理與監控 Nginx 服務?
  2. 2. Nginx Proxy Manager
  3. 3. Nginx GIXY
  4. 4. Nginx 監控報表
    1. 4.1. Elastic Metricbeat
    2. 4.2. Elastic Filebeat
    3. 4.3. GoAccess
  5. 5. FAQ:Nginx 管理與監控常見問題
    1. 5.1. Q1:Nginx Proxy Manager 適合用於正式生產環境嗎?
    2. 5.2. Q2:GIXY 掃描出的 alias_traversal 問題該如何修正?
    3. 5.3. Q3:GoAccess 與 Elastic Stack (ELK) 兩者該如何抉擇?

如何有效管理與監控 Nginx 服務?

高品質的 Nginx 服務管理 需整合自動化與視覺化工具。推薦的解決方案包含:1. Nginx Proxy Manager:提供圖形化介面與 Let’s Encrypt 整合,大幅簡化反向代理設定;2. Nginx GIXY 安全檢測:自動化掃描配置缺陷,防範路徑穿越與 HTTP 分裂等資安風險;3. GoAccess 日誌分析:即時產生精美的 Nginx 監控報表,適合輕量級分析。若需更深度的運維監控,則可結合 Elastic Stack (Filebeat/Metricbeat) 實現高品質的數據監測。掌握這些工具,能讓開發者從繁瑣的設定檔中斷捨離,提升伺服器維運的穩定性與安全性。


延續之前對 Nginx 基礎設定的探討,本文將深入介紹三款強大的 Nginx 管理與監控工具Nginx Proxy ManagerNginx GIXY 以及 GoAccess。無論是圖形化介面管理、安全配置檢測,還是即時日誌分析,這些監控工具都能有效提升 Nginx 服務的運維效率與安全性。

Nginx Proxy Manager

Nginx Proxy Manager 提供圖形化的管理介面,並且內建支援 Let’s Encrypt 能免費支援網頁 HTTPS 服務。這是一個非常實用的 Nginx 管理工具

透過 GUI 的管理介面可以更清楚的瀏覽 Proxy 設定,並且透過 Log 也可以看到修改紀錄,蠻適合用在測試環境給新手共同維護。

照著官網的安裝也真的很快速,官方網站:https://nginxproxymanager.com/

  1. 加入 docker-compose.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
version: "3"
services:
app:
image: "jc21/nginx-proxy-manager:latest"
restart: unless-stopped
ports:
# These ports are in format <host-port>:<container-port>
- "80:80" # Public HTTP Port
- "443:443" # Public HTTPS Port
- "81:81" # Admin Web Port
# Add any other Stream port you want to expose
# - '21:21' # FTP

# Uncomment the next line if you uncomment anything in the section
# environment:
# Uncomment this if you want to change the location of
# the SQLite DB file within the container
# DB_SQLITE_FILE: "/data/database.sqlite"

# Uncomment this if IPv6 is not enabled on your host
# DISABLE_IPV6: 'true'

volumes:
- ./data:/data
- ./letsencrypt:/etc/letsencrypt
  1. docker-compose up -d
  2. http://127.0.0.1:81 預設的帳號密碼 admin@example.com/changeme

測試的話,windows 的開發環境可以修改 host 檔案,位置會在 C:\WINDOWS\system32\drivers\etc\hosts,來讓 domain 可以在開發機上生效,底下是我的 config

1
127.0.0.1 proxy.test.com

接著在 Nginx Proxy Manager 上進行設定,就可以看到已經 proxy 成功了。

Nginx Proxy Manager 設定
Nginx Proxy Manager

Nginx GIXY

Gixy 是一款專為 Nginx 配置設計的自動化缺陷檢測工具,主要目標是避免錯誤和配置導致資安漏洞。它能有效幫助我們提升 Nginx 配置的安全性。

有一個練習用的 Repo vulnerable-nginx 可以拿來測試常見的問題。

$uri: current URI in request, normalized
The value of $uri may change during request processing, e.g. when doing internal redirects, or when using index files.

使用 Nginx GIXY 分析結果如下:

1
2
3
4
5
6
7
location /cats {
alias C:\nginx-1.18.0\html\;
}

location /image-credits {
return 302 https://placekitten.com/attribution.html?originalPath=$uri;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
(base) PS C:\Users\yencheng> gixy C:\nginx-1.18.0\conf\nginx.conf

==================== Results ===================

>> Problem: [http_splitting] Possible HTTP-Splitting vulnerability.
Description: Using variables that can contain "\n" or "\r" may lead to http injection.
Additional info: https://github.com/yandex/gixy/blob/master/docs/en/plugins/httpsplitting.md
Reason: At least variable "$uri" can contain "\n"
Pseudo config:

server {
server_name localhost;

location /image-credits {
return 302 https://placekitten.com/attribution.html?originalPath=$uri;
}
}

------------------------------------------------

>> Problem: [alias_traversal] Path traversal via misconfigured alias.
Description: Using alias in a prefixed location that doesn't ends with directory separator could lead to path traversal vulnerability.
Additional info: https://github.com/yandex/gixy/blob/master/docs/en/plugins/aliastraversal.md
Pseudo config:

server {
server_name localhost;

location /cats {
alias C:\nginx-1.18.0\html\;
}
}

==================== Summary ===================
Total issues:
Unspecified: 0
Low: 0
Medium: 1
High: 1

Nginx 監控報表

有效率地監控 Nginx 服務的運行狀況至關重要。目前我有使用過最簡單的方式有兩個,來提供 Nginx 監控報表

  1. 透過 Elastic 提供的 Filebeat 和 Metricbeat 可以從狀態、日誌即時地去監控 Nginx
  2. 使用開源的 GoAccess 加上系統排程定時更新資料

Elastic Metricbeat

之前剛好使用過 Elastic 相關 Solution,發現透過簡單的配置就能夠知道目前伺服器的狀況,詳細的教學歡迎參考這篇文章: Elastic Metricbeat 搭配 Kibana 來監控 Nginx 伺服器狀態,就像下圖就是系統預設提供的樣板,可以簡單即時去監控 Nginx 的流量、記憶體、處理器當下使用量。

Metricbeat + Kibana
SystemMetric

Elastic Filebeat

那如果需要分析 Log 並產生報表呢? 當然也可以透過 Elastic 相關 Solution,透過 Filebeat 傳送 Nginx Access Log 到 Elasticsearch 中,最後透過 Kibana 即時監看相關分析。

Filebeat + Kibana
FilebeatDashBoard

GoAccess

GoAccess 是一套開源的監控工具,能夠分析 Log 並且產生報表的靜態網頁,是 Nginx 日誌分析的利器。不過由於是靜態網頁,所以使用上就需要透過設定作業系統排程來定時跑腳本更新頁面。

  1. 透過指令安裝
  • sudo apt-get update
  • sudo apt-get install goaccess
  1. 透過指令直接產生
  • goaccess -f /var/log/nginx/access.log

GoAccess Dashboard
GoAccessDashboard


FAQ:Nginx 管理與監控常見問題

Q1:Nginx Proxy Manager 適合用於正式生產環境嗎?

A:Nginx Proxy Manager 教學 中提到的 GUI 介面極大降低了維護門檻,對於中小型專案或開發/測試環境非常理想。然而,在超大規模或對效能有極致要求的生產環境中,高品質的建議依然是使用傳統的文字配置檔進行微調,以獲得最佳的資源利用率與更細緻的權限卡控。

Q2:GIXY 掃描出的 alias_traversal 問題該如何修正?

A:這通常是因為 location 指令與 alias 路徑的結尾不一致所致。高品質的修正方式是:確保 location /cats/alias /data/cats/ 同時具備(或同時不具備)結尾的斜線 /。若 location 缺少斜線而 alias 具備,攻擊者可能透過 ../ 存取到非預期的父目錄,造成嚴重的 網站安全性提升 破口。

Q3:GoAccess 與 Elastic Stack (ELK) 兩者該如何抉擇?

A:如果您追求快速、簡單且佔用資源極低,GoAccess 日誌分析 是高品質的輕量選擇,它能秒級產生 HTML 報表。但如果您需要長期保存日誌、進行跨伺服器的複雜關聯查詢,或建立更進階的資安告警,則建議部署完整的 Nginx 監控報表 系統(如 ELK Stack),以滿足高品質的運維需求。



喜歡這篇文章,請幫忙拍拍手喔 🤣