502 閘道錯誤 - DecompressionFailureAtResponse

您目前查看的是 Apigee Edge 說明文件。
前往 Apigee X 說明文件
info

問題

用戶端應用程式會收到 HTTP 狀態碼 502 Bad Gateway 和錯誤碼 messaging.adaptors.http.flow.DecompressionFailureAtResponse,做為 API 呼叫的回應。

錯誤訊息

用戶端應用程式會取得下列回應代碼:

HTTP/1.1 502 Bad Gateway

此外,您可能會看到類似下方的錯誤訊息:

{
   "fault":{
      "faultstring":"Decompression failure at response",
      "detail":{
         "errorcode":"messaging.adaptors.http.flow.DecompressionFailureAtResponse"
      }
   }
}

可能原因

只有在下列情況下才會發生這個錯誤:

  • HTTP 回應 (來自後端/目標伺服器) 標頭 Content-Encoding 中指定的編碼有效,且 Apigee Edge 支援該編碼
  • BUT

  • 後端/目標伺服器在 HTTP 回應中傳送的酬載格式,與 Content-Encoding 標頭中指定的編碼格式不符

這是因為酬載格式與 Content-Encoding 標頭中指定的編碼格式不同,因此 Apigee Edge 無法使用指定的編碼解碼酬載。

以下列舉幾個支援的 Content-Encoding值,以及 Apigee Edge 在這些情況下預期的酬載表示法:

情境 Content-Encoding 酬載表示法
單一編碼 gzip

Unix gzip 格式。

請參閱 RFC1952 GZIP 格式

單一編碼 deflate

這個格式使用 zlib 結構,並採用 Deflate 壓縮演算法。

請參閱 RFC1950 RFC1951.

多重編碼

多重編碼

舉例來說,如果編碼作業執行兩次,則可能為:

  • gzip、deflate
  • gzip、gzip
  • deflate、gzip
  • deflate、deflate
系統會按照標頭中顯示的順序,對酬載套用多種編碼。

這項錯誤的可能原因如下:

原因 說明 適用於以下裝置的疑難排解說明
回應酬載格式與 Content-Encoding 不符 後端/目標伺服器傳送的回應酬載格式未經過編碼,或與 Content-Encoding 標頭中指定的編碼不符。 Edge 公有和私有雲使用者

常見的診斷步驟

請使用下列其中一種工具/技術診斷這項錯誤:

API Monitoring

如要使用 API 監控功能診斷錯誤,請按照下列步驟操作:

  1. 以具備 適當角色的使用者身分登入 Apigee Edge UI。
  2. 切換至要調查問題的機構。

  3. 依序前往「Analyze」>「API Monitoring」>「Investigate」頁面。
  4. 選取您觀察到錯誤的特定時間範圍。
  5. 確認「Proxy」篩選器已設為「All」
  6. 繪製「錯誤代碼」與「時間」的關係圖。
  7. 選取含有故障代碼 messaging.adaptors.http.flow.DecompressionFailureAtResponse 的儲存格,如下所示:

    ( 查看較大圖片)

  8. 故障代碼資訊 messaging.adaptors.http.flow.DecompressionFailureAtResponse會顯示如下:

    ( 查看較大圖片)

  9. 按一下「查看記錄」,然後展開發生 502 錯誤的資料列。

    ( 查看較大圖片)

  10. 在「記錄」視窗中,記下下列詳細資料:
    • 狀態碼: 502
    • 錯誤來源: target
    • 故障代碼: messaging.adaptors.http.flow.DecompressionFailureAtResponse
  11. 如果「Fault Source」的值為 target,表示回應酬載格式與後端伺服器回應標頭 Content-Encoding 中指定的 支援編碼不符。

追蹤工具

如要使用「追蹤」工具診斷錯誤,請按照下列步驟操作:

  1. 啟用追蹤工作階段 和下列任一項目:
    1. 等待發生 502 Bad Gateway 錯誤,或
    2. 如果可以重現問題,請發出 API 呼叫並重現問題。502 Bad Gateway
  2. 確認已啟用「顯示所有流程資訊」

  3. 選取其中一個失敗的回應,並檢查追蹤記錄。
  4. 瀏覽追蹤記錄的不同階段,找出發生失敗的位置。
  5. 通常在流程中,您會在「Response Received from target server」(從目標伺服器收到回應) 階段後立即發現錯誤,如下所示:

    ( 查看較大圖片)

  6. 請注意追蹤記錄中屬性的值:

    • Content-Encoding: gzip
    • 回應內容主體: {"fault":{"faultstring":"Decompression failure at response","detail":{"errorcode":"messaging.adaptors.http.flow.DecompressionFailureAtResponse"}}}
  7. 在「Response Received from target server」(從目標伺服器收到回應) 階段後,前往錯誤階段:

    ( 查看較大圖片)

    請注意下列屬性:

    • 錯誤: Decompression failure at response
    • error.class: com.apigee.errors.http.server.BadGateway
    • error.cause: Not in GZIP format

      error.cause 指出回應酬載並非 GZIP 格式。 這表示 Apigee Edge 預期回應酬載會採用 GZIP 格式,如 Content-Encoding 標頭中所指定 (在上一個步驟中判斷)。因此,Apigee Edge 無法使用 gzip 解壓縮酬載,並會傳回 Decompression failure at response 錯誤。

    請注意,目標/後端伺服器的回應在此情況下為 200;不過,由於錯誤是由 Apigee Edge 傳回,因此用戶端應用程式會收到 502 回應。

  8. 在追蹤記錄中前往「Response Sent to Client」(已將回應傳送給用戶端) 階段,然後按一下該階段。

    ( 查看較大圖片)

    請注意追蹤記錄中的下列詳細資料:

    • 狀態碼: 502 Bad Gateway
    • 錯誤內容: {"fault":{"faultstring":"Decompression failure at response","detail":{"errorcode":"messaging.adaptors.http.flow.DecompressionFailureAtResponse"}}}
  9. 前往追蹤記錄中的「AX」(記錄的 Analytics 資料) 階段,然後按一下該階段。AX

  10. 向下捲動至「Phase Details」(階段詳細資料) 和「Error Headers」(錯誤標頭) 專區,然後判斷 X-Apigee-fault-codeX-Apigee-fault-source 的值,如下所示:

    ( 查看較大圖片)

  11. 您會看到 X-Apigee-fault-codeX-Apigee-fault-source 的值為 messaging.adaptors.http.flow.DecompressionFailureAtResponsetarget,表示回應酬載格式與 Content-Encoding 標頭中指定的編碼不符。
    回應標頭
    X-Apigee-fault-code messaging.adaptors.http.flow.DecompressionFailureAtResponse
    X-Apigee-fault-source target

NGINX

如要使用 NGINX 存取記錄診斷錯誤,請按照下列步驟操作:

  1. 如果您是私有雲使用者,可以透過 NGINX 存取記錄判斷 HTTP 502 錯誤的相關金鑰資訊。
  2. 檢查 NGINX 存取記錄:

    /opt/apigee/var/log/edge-router/nginx/ORG~ENV.PORT#_access_log

    說明: ORGENVPORT# 會替換為實際值。

  3. 搜尋特定時間範圍內是否有任何502錯誤 (如果問題發生在過去),或是否有任何回應仍502失敗。
  4. 如果發現任何 502 錯誤,且 X-Apigee-fault-codemessaging.adaptors.http.flow.DecompressionFailureAtResponse 的值相符,請判斷 X-Apigee-fault-source 的值。

    NGINX 存取記錄檔中的 502 錯誤範例:

    上述 NGINX 存取記錄檔的範例項目具有下列 X-Apigee-fault-codeX-Apigee-fault-source 值:

    回應標頭
    X-Apigee-fault-code messaging.adaptors.http.flow.DecompressionFailureAtResponse
    X-Apigee-fault-source target

原因:回應酬載格式與 Content-Encoding 不符

根據預設,如果回應標頭 Content-Encoding 包含有效且 支援的編碼,Apigee Edge 一律會解壓縮酬載。因此,回應酬載的格式應與回應標頭 Content-Encoding 中指定的編碼相符。如果不相符,就會收到這項錯誤訊息。

診斷

  1. 使用 API 監控、追蹤工具或 NGINX 存取記錄,判斷所觀察到的錯誤的錯誤代碼錯誤來源,如常見診斷步驟所述。
  2. 如果「Fault Code」messaging.adaptors.http.flow.DecompressionFailureAtResponse,且「Fault Source」的值為 target,表示後端/目標伺服器傳送的回應酬載格式與回應標頭 Content-Encoding 中指定的 支援編碼不符。
  3. 您可以透過下列其中一種方法,在 HTTP 回應中判斷不符情況:

    錯誤訊息

    如何使用錯誤訊息進行驗證:

    1. 如果您可以存取 Apigee Edge 傳送的完整錯誤訊息,請參閱 faultstring

      錯誤訊息範例:

      "faultstring":"Decompression failure at response"
    2. 在上述錯誤訊息中,系統會顯示 "Decompression failure at response",這表示無法使用 Content-Encoding 標頭中指定的編碼,解壓縮 response

    追蹤記錄

    如要使用追蹤驗證,請按照下列步驟操作:

    1. 使用「追蹤」,判斷 Content-Typeerror.cause,如「常見診斷步驟」一文所述。
    2. 範例追蹤記錄中的值如下:

      • Content-Encoding: gzip
      • error.cause: Not in GZIP format

      回應標頭 Content-Encoding 中的值為 gzip,但回應酬載並非 GZIP 格式 (如 error.cause 所示)。因此,Apigee Edge 會傳回 502 Bad Gateway 和錯誤代碼 messaging.adaptors.http.flow.DecompressionFailureAtResponse

    實際要求

    如要使用實際要求進行驗證,請按照下列步驟操作:

    如果您有權存取向目標/後端伺服器應用程式發出的實際要求,請執行下列步驟:

    1. 如果您是公有雲/私有雲使用者,請直接從後端伺服器或任何其他機器向後端伺服器提出要求,前提是您有權從該機器向後端伺服器提出要求。
    2. 如果您是私有雲使用者,也可以從其中一個訊息處理器向後端伺服器提出要求。
    3. 檢查後端伺服器傳送的回應,並判斷回應標頭 Content-Encoding. 中傳遞的值
    4. 判斷要求中傳送的酬載格式。
    5. 如果 Content-Encoding 標頭的值位於 支援的編碼清單中,但回應酬載的格式與 Content-Encoding 標頭中指定的編碼不符,這就是問題的原因。

      範例:

      curl -v https://HOSTALIAS/test
      

      ***trimmed***
      >
      < HTTP/1.1 200 OK
      < Accept-Ranges: bytes
      < Content-Encoding: gzip
      < Date: Mon, 02 Aug 2021 08:17:35 GMT
      < Transfer-Encoding: chunked
      <
      < response_payload.zip Response Body(not in GZIP format)>
      

      上述範例回應會將 gzip 值傳送至 Content-Encoding 標頭,這是 Apigee Edge 支援的編碼。不過,response_payload.zip 會以 ZIP 檔案形式傳送。因此,這項回應會因 502 Bad Gateway 錯誤而失敗,並傳回錯誤代碼:messaging.adaptors.http.flow.DecompressionFailureAtResponse

    訊息處理器記錄

    如要使用訊息處理器記錄進行驗證,請按照下列步驟操作:

    如果您是私有雲使用者,則可使用訊息處理器記錄檔,判斷 HTTP 502 錯誤的關鍵資訊。

    1. 查看訊息處理器記錄:

      /opt/apigee/var/log/edge-message-processor/logs/system.log

    2. 搜尋特定時間範圍內是否有任何 502 錯誤 (如果問題發生在過去),或是否有任何回應仍失敗並顯示 502。您可以使用下列搜尋字串:

      grep -ri "ZipException"
      
    3. 您會看到類似下列內容的 system.log 行:

      情境 1

      情境 1:API 回應含有標頭 Content-Encoding: gzip

      2021-08-02 06:50:25,433  NIOThread@2 ERROR HTTP.CLIENT -
      HTTPClient$Context.onInputException() :  ClientInputChannel(ClientChannel[Connected:
      Remote:3.8.1.1:9000 Local:10.0.115.32:41298]@38140 useCount=1 bytesRead=0
      bytesWritten=203 age=469ms  lastIO=0ms  isOpen=true).onExceptionRead exception: {}
      java.util.zip.ZipException: Not in GZIP format
      ---trimmed--
      2021-08-02 06:50:25,433  NIOThread@2 INFO  HTTP.CLIENT -
      HTTPClient$Context.logContextDetails() : Request details : host=null
      path=/folder/testFile method=GET. Channel details : Bytes read=0
      2021-08-02 06:50:25,434  NIOThread@2 ERROR ADAPTORS.HTTP.FLOW -
      AbstractResponseListener.onException() : AbstractResponseListener.onError(HTTPResponse@4806fdab, Not in GZIP format)
      2021-08-02 06:50:25,434  NIOThread@2 INFO  HTTP.SERVICE -
      ExceptionHandler.handleException() : Exception
      java.util.zip.ZipException: Not in GZIP format
      occurred while writing to channel null
      2021-08-02 06:50:25,434  NIOThread@2 INFO  HTTP.SERVICE -
      ExceptionHandler.handleException() : Exception trace:
      java.util.zip.ZipException: Not in GZIP format
      

      上述錯誤訊息中的 java.util.zip.ZipException: Not in GZIP format 行指出,雖然 Content-Encoding 指定為 gzip,但回應酬載並未以 GZIP 格式傳送。因此,Apigee Edge 會擲回例外狀況,並將 502 狀態碼和錯誤代碼 messaging.adaptors.http.flow.DecompressionFailureAtResponse 傳回給用戶端應用程式。

      情境 #2

      情境 #2:API 回應含有「Content-Encoding: deflate」標頭

      2021-08-02 06:35:21,215  NIOThread@0 ERROR HTTP.CLIENT -
      HTTPClient$Context.onInputException() :  ClientInputChannel(ClientChannel[Connected:
      Remote:3.8.1.1:9000 Local:192.168.194.140:35224]@36014 useCount=1 bytesRead=0
      bytesWritten=202 age=439ms  lastIO=2ms  isOpen=true).onExceptionRead exception: {}
      java.util.zip.ZipException: incorrect header check
      ---trimmed----
      Caused by:
      java.util.zip.DataFormatException: incorrect header check
      ---trimmed---
      2021-08-02 06:35:21,215  NIOThread@0 INFO  HTTP.CLIENT -
      HTTPClient$Context.logContextDetails() : Request details :
      host=null path=/folder/testFile method=GET. Channel details : Bytes read=0
      2021-08-02 06:35:21,216  NIOThread@0 ERROR ADAPTORS.HTTP.FLOW -
      AbstractResponseListener.onException() : AbstractResponseListener.onError(HTTPResponse@3966e277,
      incorrect header check)
      2021-08-02 06:35:21,216  NIOThread@0 INFO  HTTP.SERVICE -
      ExceptionHandler.handleException() : Exception
      java.util.zip.ZipException: incorrect header check occurred while writing to channel null
      2021-08-02 06:35:21,217  NIOThread@0 INFO  HTTP.SERVICE -
      ExceptionHandler.handleException() : Exception trace:
      java.util.zip.ZipException: incorrect header check
      
      

      上述錯誤訊息中的 java.util.zip.ZipException: incorrect header checkCaused by: java.util.zip.DataFormatException: incorrect header check 這幾行表示回應酬載並非以 deflate 格式傳送,且與 deflate 的 Content-Encoding 標頭中指定的編碼不符。因此,Apigee Edge 會擲回例外狀況,並向用戶端應用程式傳回 502 狀態碼和錯誤代碼 messaging.adaptors.http.flow.DecompressionFailureAtResponse

解析度

  1. 如果 Apigee Edge 的 API Proxy 流程和後端伺服器不需要壓縮回應酬載,請不要傳遞 Content-Encoding 標頭。如需壓縮回應酬載,請前往步驟 2。
  2. 如需壓縮回應酬載,請確保後端伺服器一律會傳送下列內容:
    • 任何 支援的編碼,做為回應中 Content-Encoding 標頭的值
    • 傳送至 Apigee Edge 的支援格式回應酬載,與 Content-Encoding 標頭中指定的編碼格式相符
  3. 在上述範例中,回應酬載為 ZIP 格式,但回應標頭指定 Content-Encoding: gzip。如要修正這個問題,請以 Content-Encoding: gzip 格式傳送回應標頭,並以 gzip 格式傳送回應酬載:
    curl -v https://HOSTALIAS/v1/test
    
    >
    < HTTP/1.1 200 OK
    < Accept-Ranges: bytes
    < Content-Encoding: gzip
    < Date: Mon, 02 Aug 2021 08:17:35 GMT
    < Transfer-Encoding: chunked
    <
    < response_payload.gz Response Body(in GZIP format)>
    

規格

Apigee Edge 會根據下列 RFC 規格,傳回狀態碼 502 Bad Gateway 和錯誤碼 messaging.adaptors.http.flow.DecompressionFailureAtResponse

規格
RFC 7231 第 6.5.1 節
RFC 7231 的 3.1.2.2 節

如果仍需要 Apigee 支援團隊協助,請參閱「 必須收集診斷資訊」。

必須收集診斷資訊

收集下列診斷資訊,然後聯絡 Apigee Edge 支援團隊

如果您是公有雲使用者,請提供下列資訊:

  • 機構名稱
  • 環境名稱
  • API Proxy 名稱
  • 用於重現 502 錯誤的完整 curl 指令
  • API 回應的追蹤記錄檔

如果您是 Private Cloud 使用者,請提供下列資訊:

  • 失敗回應的完整錯誤訊息
  • 環境名稱
  • API Proxy 套裝組合
  • API 回應的追蹤記錄檔
  • NGINX 存取記錄 /opt/apigee/var/log/edge-router/nginx/ORG~ENV.PORT#_access_log

    說明: ORGENVPORT# 會替換為實際值。

  • 訊息處理器系統記錄 /opt/apigee/var/log/edge-message-processor/logs/system.log