我有一个在 nginx 入口控制器后面运行 Kubernetes 的 Web 应用程序,它对于请求浏览工作正常,但来自浏览器的任何 AJAX/XMLHTTPRequest 都会从 nginx 收到 502 错误。
我捕获了常规请求和 AJAX 请求的 HTTP 标头,它们看起来很好,主机标头、协议等正确。我很困惑为什么只有 XMLHttpRequest 请求从 nginx 获取 502。没有延迟/挂起,502 是立即的。这些请求似乎永远不会到达应用程序,但会被 nginx 本身拒绝。将 nginx 切换为直接负载均衡器,问题就消失了。
我要进一步挖掘,但我想知道是否有其他使用 nginx 入口控制器的人之前见过这个问题并解决了它?
我从 nginx 日志中发现了这个错误,这表明容器返回的响应对于 nginx 缓冲区来说标头太大。但是我检查了 nginx.conf 并禁用了缓冲:“proxy_buffering off;”
2016/10/27 19:55:51 [error] 309#309: *43363 upstream sent too big header while reading response header from upstream, client: 10.20.51.1, server: foo.example.com, request: "GET /admin/pages/listview HTTP/2.0", upstream: "http://10.20.66.97:80/admin/pages/listview", host: "foo.example.com", referrer: "https://foo.example.com/admin/pages"
奇怪的是,仅当 XmlHttpRequest 请求 URL 时才会收到 504 错误。如果我使用curl请求相同的URL,它工作正常,响应头如下所示。同一 URL 的 AJAX/XmlHttpRequest 会使响应标头太大怎么办?
HTTP/1.1 200 OK
Server: nginx/1.11.3
Date: Thu, 27 Oct 2016 20:15:16 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 6596
Connection: keep-alive
X-Frame-Options: SAMEORIGIN
X-Powered-By: PHP/5.5.9-1ubuntu4.19
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: max-age=0, must-revalidate, no-transform, no-cache, no-store
Pragma: no-cache
X-Controller: CMSPagesController
X-Title: Example+Site+-+Pages
X-Frame-Options: SAMEORIGIN
Vary: X-Requested-With,Accept-Encoding,User-Agent
Strict-Transport-Security: max-age=15724800; includeSubDomains; preload