Nginx 54: Connection reset by peer

2018/05/28 17:12:42 [error] 19745#0: *76 kevent() reported that upstream closed connection (54: Connection reset by peer) while reading upstream, client: 127.0.0.1, server: api.test.com, request: "GET /api/test HTTP/1.1", upstream: "http://127.0.0.1:8099/api/test", host: "api.test.com", referrer: "http://www.test.com/test/"

很詭異的錯誤,瀏覽器單獨開啟介面OK,前端訪問就不行,提示訪問失敗,Nginx錯誤日誌如上:

修改了很多配置,按著正常環境一行一行修改,結果發現,response中多出如下內容:

Access-Control-Allow-Origin: http://www.test.com
Access-Control-Allow-Origin: *

原因是Nginx做了跨域設定,程式碼中也有Filter做跨域設定,瀏覽器懵逼了
Nginx 設定如下:

#跨域設定
    # add_header Access-Control-Allow-Origin *;
    # add_header Access-Control-Allow-Headers X-Requested-With;
    # add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

java filter片段

response.setHeader("Access-Control-Allow-Origin", String.format("%s//%s", requestScheme, origin));
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "content-type");
response.setHeader("Access-Control-Allow-Credentials", "true");
if (request.getMethod().equals("OPTIONS")) {
    response.setStatus(204);
    return;
}

將本地Nginx跨域設定,註釋掉,問題解決。