当上游服务器不可用时,为什么Nginx在Linux和Windows上的行为会有所不同?

时间:2016-11-04 12:09:08

标签: linux windows nginx

我已将Nginx 1.10.1配置为代理Tomcat。如果Tomcat没有运行,Nginx会执行以下操作:

  • 在Linux上,立即返回502 Bad Gateway。
  • 在Windows上,等待60秒,然后返回504网关超时。

这是我的Nginx配置:

events {
    worker_connections  1024;
}

http {
    upstream tomcat_server {
        server 127.0.0.1:8080;
    }

    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       8081;
        server_name  localhost;

        location / {
            proxy_pass http://tomcat_server;
            proxy_redirect http://tomcat_server/ $scheme://$host:$server_port/;
            proxy_set_header Host $host:$server_port;
        }
    }
}

使用Curl请求Tomcat资源会立即失败,在Linux和Windows上都会出现相同的错误(在这种情况下没有60秒的延迟):

# Windows:
> curl http://localhost:8080 -v
* Rebuilt URL to: http://localhost:8080/
* timeout on name lookup is not supported
*   Trying ::1...
* connect to ::1 port 8080 failed: Connection refused
*   Trying 127.0.0.1...
* connect to 127.0.0.1 port 8080 failed: Connection refused
* Failed to connect to localhost port 8080: Connection refused
* Closing connection 0
curl: (7) Failed to connect to localhost port 8080: Connection refused

# Linux:
> curl -v http://127.107.177.129:8080
* About to connect() to 127.107.177.129 port 8080 (#0)
*   Trying 127.107.177.129... Connection refused
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host

那么为什么行为不同?

Windows上的超时由proxy_connect_timeout控制 - 如果我将其减少到1秒,我会在1秒而不是60秒后得到502错误。顺便说一句,我也不知道Nginx究竟在60秒内做了什么 - 它似乎没有重试连接,因为如果我在10秒后启动Tomcat它仍会在50秒后超时。它似乎是在挥动它的拇指。

任何人都可以对这些行为中的任何一个有所了解吗?

0 个答案:

没有答案
相关问题