Nginx 错误 upstream timed out 10060

最近访问Nginx代理的Tomcat服务总是很慢,第一次访问正常,第二次访问就很慢,有时还连接超时,查看了Nginx的error.log日志,发现了以下错误:

1
2
3
2018/09/06 15:52:47 [error] 2316#3908: *1 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "POST /users/bind?_=0.9640442145540367 HTTP/1.1", upstream: "http://[::1]:8080/users/bind?_=0.9640442145540367", host: "localhost", referrer: "http://127.0.0.1/users/bind?code=021gjMVH1Hl4s7072dUH1bJ4WH1gjMVp"
2018/09/06 15:53:01 [error] 2316#3908: *5 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /users/bind?code=071Wp8wC06pDJe2oNEtC0QvnwC0Wp8wb HTTP/1.1", upstream: "http://[::1]:8080/users/bind?code=071Wp8wC06pDJe2oNEtC0QvnwC0Wp8wb", host: "localhost"
2018/09/06 16:22:58 [error] 2316#3908: *34 upstream timed out (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "POST /users/key?_=0.9410131127829147 HTTP/1.1", upstream: "http://[::1]:8080/users/key?_=0.9410131127829147", host: "localhost", referrer: "http://127.0.0.1/users/bind?code=021EfqOR0uENw82sAiQR0cpvOR0EfqOW"

我的Nginx配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
rewrite_log on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
}
server {
listen 80;
server_name localhost;
index index.html index.htm;
location / {
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}

于是我搜索相关资料,在http://nginx-win.ecsds.eu/download/documentation-pdf/nginx%20for%20Windows%20-%20documentation%201.8.pdf 看到相关解释,如下图说明:

nginx-upstream-timed-out

上图给的解决方法是修改proxy_pass的内容,将localhost改为127.0.0.1

1
2
3
4
5
6
7
8
9
10
11
12
13
server {
listen 80;
server_name localhost;
index index.html index.htm;
location / {
proxy_pass http://127.0.0.1:8080; # 修改
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

修改后重启Nginx,测试成功解决问题。

不过我也在网上看到另一解决方法,就是减少反向代理连接时间,修改配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
server {
listen 80;
server_name localhost;
index index.html index.htm;
location / {
proxy_pass http://localhost:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 1;
proxy_send_timeout 30;
proxy_read_timeout 60;
}
}

此解决方法经测试后也成功解决问题。