Browser stalls request when serving static files with Nginx

1.9k Views Asked by At

I have a situation where when I open the page for the first time, everything works fast, if I reload the page soon after everything works ok, and if I reload the page 3rd time browser stalles the request for about 25 seconds. Sometimes more, sometimes less. Sometimes it's a request for root, sometimes for some static file. If I wait some time and refresh again, everything is opening fast again until about 2nd or 3rd refresh of the webpage.

What could this be? If I use Nginx but serve static files with Node, I don't have that kind of a problem?

daemon off;
worker_processes <%= ENV['NGINX_WORKERS'] || 4 %>;

worker_rlimit_nofile 10000;

events {

    use epoll;

    accept_mutex on;

    multi_accept    on;

    worker_connections 1024;
}

error_log   logs/nginx/error.log;

http {

  charset   utf-8;

  include mime.types;

  default_type  application/octet-stream;

  access_log     off;

   log_format l2met 'measure#nginx.service=$request_time request_id=$http_x_request_id';

    access_log logs/nginx/access.log l2met;

    sendfile        on;

    tcp_nopush      on;

    tcp_nodelay       on;

    types_hash_max_size       2048;

    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid   30s;
    open_file_cache_min_uses    2;
    open_file_cache_errors  on;

    limit_conn_zone $binary_remote_addr zone=conn_limit_per_ip:10m;

    limit_req_zone $binary_remote_addr zone=req_limit_per_ip:10m rate=5r/s;

    client_body_buffer_size 16k;

    client_header_buffer_size   1k;

    client_max_body_size    8m;

    large_client_header_buffers 2 1k;


    # # - Configure Timeouts

    reset_timedout_connection on;

    client_body_timeout 12;

    client_header_timeout   12;

    keepalive_timeout   15;

    keepalive_requests 100;

    send_timeout    10;

    server_tokens   off;

  # # - Dynamic gzip compression
  gzip                      on;
    gzip_http_version         1.1;
    gzip_disable              "msie6";
    gzip_vary                 on;
    gzip_min_length           20;
    gzip_buffers              4 16k;
    gzip_comp_level           4;
    gzip_proxied              any;


    # Turn on gzip for all content types that should benefit from it.

    gzip_types  application/ecmascript;
    gzip_types  application/javascript;
    gzip_types  application/json;
    gzip_types  application/pdf;
    gzip_types  application/postscript;
    gzip_types  application/x-javascript;
    gzip_types  image/svg+xml;
    gzip_types  text/css;
    gzip_types  text/csv;
    gzip_types  text/javascript;
    gzip_types  text/plain;
    gzip_types  text/xml;
    gzip_types  text/json;


  # proxying requests to other servers

    upstream nodebeats {
      server unix:/tmp/nginx.socket fail_timeout=0;
    }

    server {
        listen       <%= ENV['PORT'] %>;
        server_name  _;
        root        "/app/";

    limit_conn conn_limit_per_ip 5;
    limit_req zone=req_limit_per_ip burst=10 nodelay;

    location ~* \.(js|css|jpg|png|ico|json|xml|svg)$ {
      root               "/app/src/dist/";
      add_header         Pragma public;
      add_header         Cache-Control public;
      expires            1y;
      gzip_static        on;
      gzip               off;
      log_not_found      off;
      access_log         off;
    }

    location / {
      proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header   Host $http_host;
      proxy_set_header   Connection "";
      proxy_redirect     off;
      proxy_http_version 1.1;
      proxy_pass         http://nodebeats;
      add_header         Cache-Control no-cache;
      proxy_read_timeout 60s;
    }
}

}

1

There are 1 best solutions below

0
On

Ok after inspecting I saw that this is happening because of some Chrome (and other browsers) limitation of 6 tcp connection per server simultaneals. When I look into chrome://net-internals/#sockets I see this. The problem is ssl_socket_pool and 6 active connections. They take a long time to go from active to idle (and then page continues to load). How to fix that?

enter image description here

I tried opening some other pages that have much more static content and http request than mine (which has 8) and they reload fast always. I looked at the same place and saw that there is nothing in Active. All the connections are immediately idle after page reload.