Nginx error code 54: Connection reset by peer

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跨域设置,注释掉,问题解决。

你可能感兴趣的:(Web开发,nginx)