nginx 加载页面非常慢

nginx 加载页面非常慢

我刚刚在新环境中部署了 nginx,其配置与我以前使用的相同,但我发现了一些非常奇怪的行为。

Nginx 配置为 Solr 的反向代理,当我尝试通过 nginx 访问 Solr UI 时,大约需要一分钟才能加载。我检查了 Chrome 开发工具,看起来浏览器能够立即下载 6 个文件,但是所有其他请求都处于待处理状态。然后它就在那里停留了整整一分钟,之后所有内容都下载完毕并加载页面。之后,浏览速度很快;我认为这主要是因为本地浏览器缓存。

配置如下:

worker_processes  10;
error_log  /directory/path/error.log debug;
pid        /directory/path/nginx1.pid;

events {
     worker_connections  1024;
}

http {
 access_log  /directory/path/access.log;
 proxy_temp_path /directory/path/ 1 2;

upstream backend-test-old {
    server 1.1.1.1:7071;
    server 2.2.2.2:7071;
}

server {
    listen       7071;
    server_name  localhost;
    access_log /directory/path/access_7071.log;
    proxy_ignore_headers "Set-Cookie";
    proxy_hide_header "Set-Cookie";
    location /solr {
        proxy_pass http://backend-test-old;
    }
}
}

我还在日志中看到很多这样的内容:

2015/07/10 11:06:38 [warn] 31503#0: *120 an upstream response is buffered to a temporary file /dir/path/7/00/0000000007 while reading upstream, client: 111.111.111.111
2015/07/10 11:06:36 [info] 31503#0: *96 client prematurely closed connection, so upstream connection is closed too while connecting to upstream, 
2015/07/10 11:06:36 [error] 31503#0: *101 upstream timed out (110: Connection timed out) while connecting to upstream, client:

现在这在我的开发服务器上不是问题,它运行相同的配置并且应该配置相对相似,所以它可能与系统有关,但我不确定。

任何帮助将非常感激。

谢谢

编辑:

问题似乎是我通过 IP 而不是主机名指向后端主机。我不确定为什么会出现这个问题。我已经实现了此配置:

worker_processes  10;
error_log  /path/error.log debug;
pid        /path/nginx1.pid;

events {
    worker_connections  1024;
}

http {
     access_log  /path/access.log;
     proxy_temp_path /pathproxy_temp/ 1 2;

    upstream backend-test-old {
        server hostname-for-1.1.1.1:7071;
        server hostname-for-2.2.2.2:7071;
    }

    upstream backend-test-old2 {
        server 1.1.1.1:7071;
        server 2.2.2.2:7071;
    }

    server {
        listen       7071;
        server_name  localhost;
        access_log /path/access_7071.log;
        proxy_ignore_headers "Set-Cookie";
        proxy_hide_header "Set-Cookie";
        location /solr {
                proxy_pass http://backend-test-old;
        }
    }

        server {
        listen       7072;
        server_name  localhost;
        access_log /path/access_7072.log;
        proxy_ignore_headers "Set-Cookie";
        proxy_hide_header "Set-Cookie";
        location /solr {
                proxy_pass http://backend-test-old2;
        }
    }
}

如果我通过 7071,它工作正常,但是如果我通过 7072,就会发生上述性能问题。有人有什么想法吗?

答案1

缓冲会导致问题,请将其完全关闭:

proxy_buffering off;

官方文档详情请见:

当缓冲被禁用时,响应将在收到后立即同步传递给客户端。nginx 不会尝试从代理服务器读取整个响应。nginx 一次可以从服务器接收的最大数据大小由 proxy_buffer_size 指令设置。

答案2

尝试proxy.conf用以下值替换内容:

proxy_buffers           32 4m;
proxy_busy_buffers_size     25m;
proxy_buffer_size 512k;
proxy_ignore_headers "Cache-Control" "Expires";
proxy_max_temp_file_size 0;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size        1024m;
client_body_buffer_size     4m;
proxy_connect_timeout 300;
proxy_read_timeout 300;
proxy_send_timeout 300;
proxy_intercept_errors off;

相关内容