HTTP / 2服务器推送的资产无法加载(HTTP2_CLIENT_REFUSED_STREAM)

时间:2020-02-04 02:50:38

标签: laravel google-chrome nginx cloudflare http2

对于通过http2推送的全部或部分资产,我在chrome devtools控制台中遇到以下错误ERR_HTTP2_CLIENT_REFUSED_STREAM

ERR_HTTP2_CLIENT_REFUSED_STREAM

刷新页面并随机清除缓存可以部分甚至有时完全修复此问题。

我正在使用启用了http2(通过certbot的ssl)和cloudflare的nginx。

server {
    server_name $HOST;
    root /var/www/$HOST/current/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

    set $auth "dev-Server.";

    if ($request_uri ~ ^/opcache-api/.*$){
    set $auth off;
    }

    auth_basic $auth;
    auth_basic_user_file /etc/nginx/.htpasswd;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

    location ~* \.(?:css|js)$ {
        access_log off;
        log_not_found off;
        # Let laravel query strings burst the cache
          expires 1M;
          add_header Cache-Control "public";
        # Or force cache revalidation.
          # add_header Cache-Control "public, no-cache, must-revalidate";
    }

    location ~* \.(?:jpg|jpeg|gif|png|ico|xml|svg|webp)$ {
        access_log off;
        log_not_found off;
        expires 6M;
        add_header Cache-Control "public";
    }

    location ~* \.(?:woff|ttf|otf|woff2|eot)$ {
        access_log        off;
        log_not_found     off;
        expires max;
        add_header Cache-Control "public";
        types     {font/opentype otf;}
        types     {application/vnd.ms-fontobject eot;}
        types     {font/truetype ttf;}
        types     {application/font-woff woff;}
        types     {font/x-woff woff2;}
    }


    listen 443 ssl http2;
    ssl_certificate /etc/letsencrypt/live/$HOST/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/$HOST/privkey.pem; # managed by Certbot
    # include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}
server {
    if ($host = $HOST) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    server_name $HOST;
    listen 80;
    return 404; # managed by Certbot


}

谷歌搜索此错误不会返回太多结果,如果有帮助,它是一个laravel 6应用来推送这些资产,如果我禁用了laravel推送资产,那么所有资产都将正确加载。

我什至不知道从哪里开始寻找。


更新1

我启用了chrome日志记录,并按照here提供的说明使用Sawbuck检查了日志,发现实际错误与414 HTTP响应有一定关系,这暗示了一些缓存问题。

log


更新2

我发现了很棒的The browser can abort pushed items if it already has them,其中指出:

如果推送缓存中已有该项,Chrome将拒绝推送。它使用PROTOCOL_ERROR而不是CANCELREFUSED_STREAM拒绝。

它还链接到一些chromemozilla错误。

这导致我完全禁用cloudflare并直接与服务器进行测试,我尝试了各种Cache-Control指令,还尝试禁用了标头,但是在清除缓存后刷新后,也会发生相同的错误。

显然,chrome取消了http / 2推送的资产,即使推送缓存中不存在,也会导致页面损坏。

目前,我正在暂时禁用laravel应用中的http / 2服务器推送。

1 个答案:

答案 0 :(得分:0)

我们刚刚遇到了与您描述的问题完全相同的问题。我们的其中一个Javascript文件获得了“ net :: ERR_HTTP2_CLIENT_REFUSED_STREAM”。重新加载和清除缓存可以正常工作,但是随后问题又出现了,似乎是随机的。 Chrome和Edge(基于Chromium)中存在相同的问题。然后,我在Firefox中尝试并得到了相同的行为,但是Firefox抱怨该URL的响应为“ text / html”。我的猜测是,由于某种原因,我们在Cloudflare中为该URL缓存了一个“ text / html”响应。当我直接在Firefox中打开该URL时,出现了“ application / javascript”,然后问题就消失了。仍然不太确定这一切是怎么发生的。

编辑: 在我们的案例中,事实证明,服务器使用401阻止了.js文件的响应,并且我们没有发送任何缓存头。 Cloudflare会尝试提供帮助,因为浏览器希望使用.js文件,因此即使状态为401也会缓存响应。此后由于其他原因而失败,因为我们尝试http2将状态为401的text / html响应作为.js文件推送。 Luckliy Firefox为我们提供了更好,可行的错误消息。

EDIT2: 原来这不是HTTP标头缓存问题。这是因为我们对.js文件进行了cookie身份验证,而且http2推送请求似乎并不总是包含cookie。解决方法是允许对.js文件执行无cookie请求。

相关问题