監視 NGINX

nginx 標誌 nginxplus 標誌

Instana 可協助您收集通過 NGINX 的要求的度量及分散式追蹤資料。

安裝 Instana 主機代理程式之後,會自動安裝 NGINX 感應器。 在您依照 配置 一節所概述來配置 NGINX 感應器之後,您可以在 Instana 使用者介面中檢視與 NGINX 相關的度量值。

若要使用「分散式追蹤」功能,您需要完成 分散式追蹤 區段中的配置步驟。

支援的資訊

支援的作業系統

NGINX 感應器和 NGINX 追蹤具有不同的版本和平台需求。

對於 NGINX 感應器,支援的作業系統與主機代理程式的需求一致,可以在每一個主機代理程式的「支援的作業系統」區段中進行檢查,例如 Linux

對於 NGINX 追蹤,支援下列作業系統:

作業系統 架構 位元
Alpine Linux:edge、 3.18、 3.17、 3.16、 3.15、 3.14、 3.13、 3.12、 3.11、 3.10 x86_64 64
Amazon Linux: 2、2023、2022 x86_64 64
CentOS: Centos 7、Stream 9、Stream 8 x86_64 64
Debian: 12、11、10、9 x86_64 64
Ubuntu:LTS x86_64 64

支援的 NGINX 版本和平台

NGINX 感應器和 NGINX 追蹤具有不同的版本和平台需求。 如需相關資訊,請參閱 支援的 NGINX 版本和平台

其他支援的資訊

NGINX 追蹤支援下列 Docker 儲存器映像檔:

容器映像檔 架構 位元
3scale openresty x86_64 64
ingress-nginx (us.gcr.io/k8s-artifacts-prod/ingress-nginx/controller): v0.34.0..v1.8.2 x86_64 64
nginx: alpine , sable-alpine x86_64 64
openresty/openresty Debian 型 x86_64 64
openresty/openresty CentOS 型 x86_64 64

配置

啟用度量值收集

若要讓 Instana 自動收集並監視您的 NGINX 程序,您需要啟用度量值收集,如下所示:

NGINX 的度量值

對於 NGINX 度量值集合, Instana 使用 ngx_http_stub_status_module 進行遠端度量值集合。 若要啟用此集合,請確定模組已啟用或可用,並在 NGINX 配置開頭新增下列 Snippet:

location /nginx_status {
  stub_status  on;
  access_log   off;
  allow 127.0.0.1; # Or the Remote IP of the Instana Host Agent
  deny  all;
}

依預設, Instana 代理程式會在任何可用的程序引數中搜尋配置檔的位置; 否則會撤回至 /etc/nginx/nginx.conf

附註: 當 Instana 代理程式在 Kubernetes 叢集中執行時,請確保容許使用 allow <host-ip-address> 配置來執行代理程式的主機 IP 位址。

NGINX Plus 的度量值

若要啟用 NGINX Plus 度量監視,請確定 ngx_http_api_module (*) 已安裝或可用,並新增下列區塊以啟用模組:

location /api {
    api write=off;
    allow 127.0.0.1; # Or the Remote IP of the Instana Host Agent
    deny all;
}

Kubernetes Ingress NGINX 的度量值

從 Kubernetes Ingress NGINX 0.23.0 版開始,已停用在埠 18080 上接聽的伺服器。 若要讓 Instana 監視此 NGINX 實例,請將下列 Snippet 新增至 Configmap ,以還原伺服器:

http-snippet: |
  server {
    listen 18080;

    location /nginx_status {
      stub_status on;
      access_log  off;
      allow 127.0.0.1; # Or the Remote IP of the Instana Host Agent
      deny  all;
    }

    location / {
      return 404;
    }
  }

如需相關資訊,請參閱 NGINX Ingress 版本注意事項

檢視度量值

完成 配置 區段中的配置步驟之後,您可以在 Instana 使用者介面中檢視與 NGINX 相關的度量值。

若要檢視度量值,請完成下列步驟:

  1. 在 Instana 使用者介面的資訊看板中,選取 基礎架構
  2. 按一下特定的受監視主機。

然後,您可以查看包含所有已收集度量及受監視處理程序的主機儀表板。

配置資料

  • PID
  • 工作者處理程序數目
  • 工作者連線數
  • 啟動時間
  • 版本
  • 建置 (*)
  • 地址 (*)
  • 世代 (*)
  • PPID (*)

效能度量

  • 要求
  • 連線
  • 處理程序 (*)
  • SSL (*)
  • 快取 (*)
  • 伺服器區域 (*)
  • 上游 (*)

性能簽章

每一個感應器都有一個經過策劃的性能簽章知識庫,可針對送入的度量值持續進行評估,並用來提出取決於使用者影響的問題或發生事件。

內建事件 會根據實體上失敗的性能簽章來觸發問題或突發事件,而 自訂事件 會根據任何特定實體的個別度量值臨界值來觸發問題或突發事件。

如需 NGINX 感應器內建事件的相關資訊,請參閱 內建事件參照

NGINX 追蹤 (NGINX 的分散式追蹤)

為 Instana 代理程式配置 NGINX Ingress

若要使用 NGINX 追蹤,您必須指定下列配置值:

  • 在 ConfigMap 中針對 NGINX Ingress 新增下列項目:

      data:
        enable-opentracing: "true"
        zipkin-collector-host: $HOST_IP
        zipkin-collector-port: "42699"
    
  • 在 NGINX Pod 規格中新增環境變數,如下所示 (它應該已經有 POD_NAMEPOD_NAMESPACE):

    env:
    - name: HOST_IP
        valueFrom:
        fieldRef:
            fieldPath: status.hostIP
    

附註:

  • Ingress NGINX 會自動設定 POD_NAMEPOD_NAMESPACE 環境變數。 因此,您不需要將 POD_NAMEPOD_NAMESPACE 環境變數新增至 NGINX Pod 規格。

  • 此配置使用 Kubernetes DownwardAPI 來使主機 IP 可作為環境變數 (HOST_IP) ,而 ConfigMap 會挑選此項目。

  • 埠可以修正為 42699 ,這是 Instana 代理程式埠。

  • 服務命名為預設 nginx ,或者需要由參數 zipkin-service-name改寫,該參數可以在 ConfigMap中配置。

如需 NGINX Ingress 及 OpenTracing的相關資訊,請參閱 Kubernetes Ingress NGINX 文件。

NGINX、NGINX Plus 及 OpenResty

若要在設定中安裝 NGINX 追蹤,請完成下列步驟:

  1. 針對您的 NGINX 版本 取得正確的二進位檔
  2. 複製二進位檔 , NGINX 伺服器可以在其中存取它們。
  3. 編輯 NGINX 配置
  4. 重新啟動 NGINX 處理程序或 觸發配置重新載入 傳送 reload 指令

1. 下載二進位檔

NGINX HTTP 追蹤模組是以 nginx-opentracing v0.22.1 模組為基礎,具有可啟用更多功能及更容易使用的自訂作業。

NGINX 分散式追蹤二進位檔 頁面上提供受支援 NGINX 發行套件的 Instana 二進位檔下載鏈結。

2. 複製二進位檔

在前一個步驟中下載並解壓縮的兩個二進位檔必須放在 NGINX 處理程序可以存取的檔案系統上,包括位置和檔案許可權。

如果 NGINX 直接在作業系統上執行,而不是在儲存器中執行,通常最好將兩個 Instana 二進位檔複製到包含其他 NGINX 模組的資料夾中。 您可以執行 nginx -V 指令來尋找 NGINX 預期模組所在的位置,並尋找 --modules-path 配置選項,例如,請參閱 StackOverflow上的這個回應

在容器化環境中,此作法可能表示將它們新增至容器映像檔,或將檔案以磁區形式裝載至容器; 例如,請參閱 Docker 的 連結裝載 文件,或如何 將磁區裝載至 Kubernetes中的 Pod。

3. 編輯 NGINX 配置

每個受支援的 NGINX 版本都有兩個分開的壓縮保存檔,一個用於 GLIBC ,另一個用於 MUSLGLIBC 適用於所有 Linux Distros ,但 Alpine除外,它需要 MUSL

在如下配置 NGINX 之前,您需要將下載的模組重新命名為 modules/ngx_http_opentracing_module.so ,或根據下載的模組名稱來變更 nginx.conf 檔中的配置行 load_module modules/ngx_http_opentracing_module.so; 。 例如,將配置行 load_module modules/ngx_http_opentracing_module.so; 變更為 load_module modules/musl-nginx-1.23.3-ngx_http_ot_module.so;

# The following line adds the basic module Instana uses to get tracing data.
# It is required that you use the version of this module built by Instana,
# rather than the one shipped in many NGINX distros, as there are some
# modifications in the Instana version that are required for tracing to work
load_module modules/ngx_http_opentracing_module.so;

# Whitelists environment variables used for tracer configuration to avoid
# that NGINX wipes them. This is only needed if instana-config.json
# should contain an empty configuration with "{}" inside to do the
# configuration via these environment variables instead.
env INSTANA_SERVICE_NAME;
env INSTANA_AGENT_HOST;
env INSTANA_AGENT_PORT;
env INSTANA_MAX_BUFFERED_SPANS;
env INSTANA_DEV;

events {}

error_log /dev/stdout info;

http {
  error_log /dev/stdout info;

  # The following line loads the Instana libsinstana_sensor library, that
  # gets the tracing data from ngx_http_opentracing_module.so and converts
  # them to Instana AutoTrace tracing data.
  # The content of instana-config.json is discussed as follows.
  opentracing_load_tracer /usr/local/lib/libinstana_sensor.so /etc/instana-config.json;

  # Propagates the active span context for upstream requests.
  # Without this configuration, the Instana trace will end at
  # NGINX, and the systems downstream (those to which NGINX
  # routes the requests) monitored by Instana will generate
  # new, unrelated traces
  opentracing_propagate_context;

  # Optional: This logs subrequests like e.g. created by the `auth_request`
  # directive so that authorization requests can be traced.
  log_subrequest on;

  # If you use upstreams, Instana will automatically use them as endpoints,
  # and it is really cool :-)
  upstream backend {
    server server-app:8080;
  }

  server {
    error_log /dev/stdout info;
    listen 8080;
    server_name localhost;

    location /static {
      root /www/html;
    }

    location ^~ /api {
      proxy_pass http://backend;
    }

    location ^~ /other_api {
      proxy_set_header X-AWESOME-HEADER "truly_is_awesome";

      # Using the `proxy_set_header` directive voids for this
      # location the `opentracing_propagate_context` defined
      # at the `http` level, so here it needs to be set again.
      # It needs to be set for every block where `proxy_set_header`
      # is found. This can also be the case at `server` level.
      opentracing_propagate_context;

      proxy_pass http://backend;
    }
  }
}

特殊案例 opentracing_propagate_context:

除了主要 (http) 層次之外,還需要針對每個設定 proxy_set_header 指引的區塊 (serverlocation) 新增 opentracing_propagate_context 指引。 原因是 OpenTracing 環境定義傳播在內部以 proxy_set_header 為基礎,否則它會被它作廢。 這是 NGINX 模組 API 的限制。

以下是 instana-config.json的範例:

{
  "service": "nginxtracing_nginx", # Change this line to give your NGINX service a different name in Instana
  "agent_host": <host_agent_address>, # Change this line with the IP address or DNS name of the Instana agent on the same host as your NGINX process
  "agent_port": 42699, # This is the default, and you should never change it unless instructed by the Instana support
  "max_buffered_spans": 1000
}

上述 Snippet 中的配置表示下列:

  • service: 將在 Instana 後端中與此 NGINX 程序相關聯的名稱。 如果未指定,則會根據例如 HTTP 主機名稱或其他方法來計算服務名稱。
  • agent_host: 本端主機代理程式的 IP 位址或 DNS 名稱。 您必須變更此配置,以符合與 NGINX 程序相同的主機上 Instana 代理程式的網路名稱
  • agent_port:NGINX 追蹤延伸將嘗試聯絡主機代理程式的埠。 請注意,此埠 不可配置 代理程式端。 NGINX 追蹤延伸可讓您在設定需要埠轉遞或埠對映時配置它。
  • max_buffered_spans:NGINX 追蹤延伸將在本端保留的跨距數量上限 (每個要求一個) ,然後再將它們清除至代理程式; 預設值為 1000。 NGINX 追蹤延伸一律會清除每秒本端緩衝的跨距。 當 NGINX 伺服器每秒處理的要求數超過 1000 個時,此設定可讓您減少本端緩衝量,而且您想要透過更快清除追蹤資料來減少 NGINX 伺服器的記憶體覆蓋區。

替代方案是透過環境變數來配置追蹤程式。 這些優先,但仍需要檔案 instana-config.json 。 請執行下列動作:

  • 將空的配置 {} 放入 instana-config.json
  • 執行 NGINX 配置中環境變數的白名單,如上所示
  • 在啟動 NGINX 之前設定環境變數

將 Instana 代理程式主機設為 Kubernetes 叢集中的主機 IP 時,此方法特別有用。

下列範例 Kubernetes 部署 YAML 組件顯示此方法:

        env:
        - name: INSTANA_SERVICE_NAME
          value: "nginxtracing_nginx"
        - name: INSTANA_AGENT_HOST
          valueFrom:
            fieldRef:
              fieldPath: status.hostIP

如需相關資訊,請參閱 環境變數參照

4. 重新啟動或重新載入

透過傳送 reload 指令,重新啟動 NGINX 處理程序或 觸發配置重新載入

支援 W3C 追蹤環境定義

NGINX Tracer 1.8.0開始,支援傳播 W3C 追蹤環境定義標頭。

支援其他 NGINX OpenTracing 模組建置

不支援使用來自 3rd 的 NGINX OpenTracing 模組建置,包括 NGINX 本身支援的那些建置。 需要 NGINX OpenTracing 模組的 Instana 建置的原因是技術: 不支援自行編譯 (亦即,您建置自己的版本) ,因為這會過度地造成 Instana 支援的壓力,無法嘗試在完全不同且無法預期的設定中找出編譯程序中發生的錯誤; 同樣地,不支援 F5 所提供的模組, 因為那些缺乏 Instana 追蹤所需的功能,以及那些使用動態鏈結至標準 C++ 程式庫且在許多情況下會導致 segfault的功能。 實際上,為了避免 segfault ,已建置 Instana NGINX OpenTracing 模組,包括靜態鏈結的標準 C++ 程式庫,用於統一測試,而且即使在較舊的發行套件上,也適用於現代 C++ 程式碼。

Kubernetes Ingress NGINX 的分散式追蹤

Instana AutoTrace WebHook 可以在 Kubernetes 和 OpenShift上自動配置 Ingress NGINX 和 NGINX 的分散式追蹤。 啟用 NGINX 的自動追蹤 時, WebHook 會自動將先前提及的配置 Snippet 及追蹤二進位檔注入 NGINX 及 Ingress NGINX 儲存器的配置。

Kubernetes 使用 Zipkin 追蹤器的 Ingress NGINX

Kubernetes Ingress NGINX 容許透過 OpenTracing 專案進行分散式追蹤。 由於 Instana 代理程式也能夠吸收 Jaeger 及 Zipkin 追蹤資料,因此可以使用將追蹤資料轉遞至 Instana 的方式來配置 NGINX Ingress。

附註: 雖然支援此設定,但 Instana 將無法從 OpenTracing 追蹤中接管追蹤環境定義,這表示見解僅限於隔離呈現的 NGINX 跨距。 只有在透過 OpenTracing 追蹤所有服務時,才會保留環境定義,且 Instana 會顯示完整分散式追蹤。

附註: 需要 nginx-ingress 0.23.0 版或更新版本; 舊版不支援變數擴充。

附註: 全部 Jaeger 支援的限制Zipkin 適用。

Nginx 追蹤範例

Instana 提供公用儲存庫,以預覽 Nginx 感應器的追蹤功能。 如需相關資訊,請參閱 nginx-tracing

疑難排解

尋找日誌

Instana 追蹤程式會將其日誌行寫入 NGINX 的標準錯誤。 這些行具有字首 [lis]

如果使用 autotrace-mutating-webhook進行自動檢測,請使用 NGINX 自動追蹤所涉及的二進位檔日誌對問題進行疑難排解。 您也可以使用這些日誌 向 IBM 支援中心開立支援案例 ,以取得更精細的環境定義。 自動檢測涉及兩個二進位檔: 程式庫 libinstana_init 和執行檔 watcher_nginx

  • libinstancea_sensor 的日誌檔位於 /tmp/instana/lii_logs/$PID.log
  • watcher_nginx 的日誌檔位於 /tmp/instana/iwn_logs/$PID.log

其中 $PID 代表所涉及處理程序的處理程序 ID。

依預設,記載層次會設為 error 。 若要使用不同的記載層次來部署 NGINX ,請透過設定環境變數來變更記載層次。

對於 libinstana_init 程式庫,請設定下列環境變數:

INSTANA_LII_LOG_LEVEL=debug

對於 watcher_nginx 執行檔,請設定下列環境變數:

INSTANA_IWN_LOG_LEVEL=debug

Nginx API 無法存取

監視問題類型: nginx_api_not_accessible

若要解決此問題,請參閱如何配置 Instana 代理程式來收集所有 NGINX 度量值的步驟,如 啟用度量值收集 一節中所述。

Nginx 狀態端點無法存取

監視問題類型: nginx_status_not_accessible

若要解決此問題,請參閱如何配置 Instana 代理程式來收集所有 NGINX 度量值的步驟,如 啟用度量值收集 一節中所述。

找不到 Nginx API

監視問題類型: nginx_api_not_found

若要解決此問題,請參閱如何配置 Instana 代理程式來收集所有 NGINX 度量值的步驟,如 啟用度量值收集 一節中所述。

找不到 NGINX 狀態

監視問題類型: nginx_status_not_found

若要解決此問題,請參閱如何配置 Instana 代理程式來收集所有 NGINX 度量值的步驟,如 啟用度量值收集 一節中所述。

未探索到 NGINX 配置位置

監視問題類型: nginx_config_location_not_discovered

若要解決此問題,請參閱如何配置 Instana 代理程式來收集所有 NGINX 度量值的步驟,如 啟用度量值收集 一節中所述。

OpenResty Lua 程式碼的追蹤連續已中斷

問題: 無法自動追蹤從 Lua 程式碼發出的自訂 HTTP 要求。

解決方案: 出埠 Instana 標頭需要從其各自的 NGINX OpenTracing 變數延伸。 因此,在傳送 HTTP 要求 (例如使用 lua-resty-http 函數 request_uri()) 之前,請將標頭 X-INSTANA-TX-INSTANA-SX-INSTANA-L 從 NGINX 變數 opentracing_context_x_instana_topentracing_context_x_instana_sopentracing_context_x_instana_l 新增至出埠要求標頭 Lua 變數。 如需相關資訊,請參閱 lua-nginx-module Readmelua-resty-http Readme

請參閱下列 Lua 程式碼範例:

local http_c = http.new()
...
local req_headers = {}
req_headers["X-INSTANA-T"] = ngx.var.opentracing_context_x_instana_t
req_headers["X-INSTANA-S"] = ngx.var.opentracing_context_x_instana_s
req_headers["X-INSTANA-L"] = ngx.var.opentracing_context_x_instana_l

local response, error = http_c:request_uri(request_url, {
    method = "POST",
    headers = req_headers,
    body = req_body
  }
);

SELinux 防止 NGINX 處理程序載入 OpenTracing 模組

問題: 未追蹤對 NGINX 的呼叫,且 NGINX 錯誤檔顯示下列錯誤:

/etc/nginx/modules/ngx_http_opentracing_module.so: failed to map segment from shared object: Permission denied

解決方案: SELinux 阻止 NGINX 處理程序從 OpenTracing 模組 (共用物件) 讀取及對映記憶體。 若要驗證 SELinux 是否負責錯誤,您可以執行煙霧測試,如下所示:

  1. 暫時停用 SELinux
  2. 重新啟動 NGINX

透過停用 SELinux 並重新啟動 NGINX ,錯誤訊息會從 NGINX 錯誤日誌中消失,並讓 Instana 追蹤呼叫。

停用 SELinux 不是長期解決方案。 正確且安全的方法是建立 SELinux 原則,以容許 NGINX 處理程序從 OpenTracing 模組讀取並對映記憶體。 您可以透過檢查 NGINX 配置目錄來找到 OpenTracing 模組。 如果 NGINX 配置目錄是 /etc/nginx,則模組位於 /etc/nginx/modules 目錄中。 您的 DevOps 或 IT 部門必須配置 SELinux。 如需相關資訊,請參閱下列線上資源,其中記載 Instana 支援之部分 Linux 發行套件的 SELinux 配置:

如需 NGINX 和 SELinux 整合的相關資訊,請參閱 使用 NGINX 和 NGINX Plus 搭配 SELinux

已知限制

  • 從 NGINX 追蹤器收集的追蹤資料不包括 Span 詳細資料中的堆疊追蹤資料。 原因是 NGINX 追蹤器是 C/C++ 感應器,目前不存在任何提案來包括 C/C++ 工具的堆疊追蹤。 對於有意義的資料,這種追蹤需要 C/C++ 應用程式中所涉及程式庫的所有除錯套件。 不過,這些套件通常不會安裝在正式作業環境中。