我的服务器上出现了“读取上游时上游过早关闭连接”的中间错误。该错误仅发生在通过 Amazon S3 向服务器请求的数据上,而不会发生在服务器本身的任何文件上。
我的设置如下:
1 基于 Nginx 的负载均衡器 nginx 版本:nginx/1.11.5(来自 Ubuntu 存储库)
它的配置是:
server {
server_name example.com
client_body_timeout 12s;
client_header_timeout 12s;
keepalive_timeout 15s;
access_log off;
proxy_connect_timeout 1000s;
proxy_send_timeout 1000s;
proxy_read_timeout 1000s;
send_timeout 1000s;
location / {
set $ua $http_user_agent;
if ($http_user_agent = "")
{
set $ua "Fixing-Empty-User-Agent";
}
proxy_pass http://front_least;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
proxy_ignore_client_abort on;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Accept-Encoding "";
}
}
upstream front_least {
least_conn;
server localF1.example.com;
server localF2.example.com;
server f1.example.com backup; # Public ports
server f2.example.com backup; # Public ports
keepalive 32;
}
- 2 个安装了 Nginx 的前端服务器。它们之间的配置完全相同,这里我仅展示部分配置,完整配置太大了。
F 配置(相关部分恕我直言):
# Fix S3 Content Type error
map $uri $custom_content_type {
default "binary/octet-stream";
~(.*\.png)$ "image/png";
~(.*\.jpg)$ "image/jpeg";
~(.*\.jpeg)$ "image/jpeg";
~(.*\.gif)$ "image/gif";
~(.*\.ico)$ "image/x-icon";
}
server {
listen 80;
server_name example.com *.example.com;
ssl_prefer_server_ciphers on;
ssl_protocols SSLv3 TLSv1;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_proxied any;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/javascript text/xml application/xml+rss;
gzip_vary on;
pagespeed on;
...
Huje amount of lines related to pagespeed
...
location /gallery/ {
try_files $uri @s3icons;
}
location /gallery_thumbnails/ {
try_files $uri @s3icons;
}
location /icon/ {
try_files $uri @s3icons;
}
location /screenshot/ {
try_files $uri @s3icons;
}
location /vendor/ {
try_files $uri @s3icons;
}
location @s3icons {
proxy_set_header Host 'bucket_name.s3.amazonaws.com';
proxy_set_header Authorization '';
proxy_hide_header x-amz-id-2;
proxy_hide_header x-amz-request-id;
proxy_hide_header Set-Cookie;
proxy_ignore_headers "Set-Cookie";
proxy_intercept_errors on;
proxy_hide_header Content-Type;
add_header Content-Type $custom_content_type;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://s3_icons;
}
include common/error.conf;
include common/deny.conf;
}
# Define a mapping used to mark HTML as uncacheable.
map $upstream_http_content_type $new_cache_control_header_val {
default $upstream_http_cache_control;
"~*text/html" "no-cache, max-age=0";
}
upstream s3_images {
server bucket_1.s3.amazonaws.com;
keepalive 10;
}
upstream s3_icons {
server bucket_2.s3.amazonaws.com;
keepalive 10;
}
现在,我只会在来自 S3 的图像上收到错误,如果我将负载均衡器的来源指向前端服务器,则不会出现错误。如果我在 Curl 或浏览器中检查日志中的同一文件,我会毫无问题地获取图像。但日志显示存在问题。