我正在运行 Nginx 以将请求传递给两个瘦服务器。该网站大约 90% 的时间都在工作,然后每隔一段时间它就会变得没有响应,我会收到这样的错误
2014/11/28 21:40:05 [error] 21516#0: *1458 upstream timed out (110: Connection timed out) while reading response header from upstream, client: X.X.X.X, server: www...com, request: "HEAD / HTTP/1.1", upstream: "http://127.0.0.1:5001/", host: "www.example.com", referrer: "http://www.example.com/"
我在网上搜索了解决方案,但不幸的是,大多数解决方案都与这个问题有关,因为它一直在发生。在那种情况下,它通常意味着 Thin 只是没有运行。就我而言,我的网站大部分时间都在运行。我可以自己 ping 瘦服务器。虽然我没有尝试在网站无响应时对它们执行 ping 操作。这可能会让我更深入地了解这个问题。
这是我的 nginx.conf 和 sites-available
user www-data;
worker_processes 2;
pid /var/run/nginx.pid;
events {
worker_connections 768;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 70;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
client_max_body_size 100M;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
gzip_static on;
gzip_comp_level 6;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/octet-stream;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
和站点启用/默认
map $http_upgrade $connection_upgrade {
default Upgrade;
'' close;
}
upstream example {
server 127.0.0.1:5000;
server 127.0.0.1:5001;
}
upstream websocket {
server 127.0.0.1:5001;
}
server {
listen 80;
listen 443 ssl;
keepalive_timeout 70;
root /data/example/;
index index.html index.htm;
server_name www.example.com;
ssl_certificate <PATH>;
ssl_certificate_key <PATH>;
location ~ ^/assets/ {
include /etc/nginx/block.list;
expires 1d;
add_header Cache-Control public;
add_header ETag "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://example;
}
location /websocket {
include /etc/nginx/block.list;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_pass http://websocket;
}
location / {
include /etc/nginx/block.list;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://example;
}
}
我做的最后一件事是删除配置文件中的一些 if
语句。我不确定这有什么作用。从那以后问题没有发生,但我认为时间还不够长。
编辑:问题再次出现。我回到原点。
if (-f $request_filename/index.html) {
rewrite (.*) $1/index.html break;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
set $flags "";
if (!-f $request_filename) {
set $flags "${flags}R";
}
if ($flags = "R") {
proxy_pass http://example;
break;
}
请您参考如下方法:
事实证明,解决方案很简单。这个问题花了我更长的时间来解决。
事实证明,Google Compute Engine 有一个防火墙规则,可以在 10 分钟后断开空闲的 TCP 连接。这意味着 Thin 与数据库的连接已断开。
但是,Thin 没有抛出超时错误。这使得很难确定 Nginx 超时的来源。所以这可能是 Thin 中的一个错误,我不确定,但我确实在 Thin 配置中设置了一个短时间的超时参数。
解决方案本身是设置 keepalive 设置,即使在空闲时也能保持 TCP 连接处于事件状态。详情请看这里:https://cloud.google.com/compute/docs/troubleshooting#communicatewithinternet .