在浏览器内部能否检测浏览器对HTTP2/SPDY客户端的支持?
我正在尝试向用户展示他们的浏览器是否支持HTTP2/SPDY,或者使用传统的非HTTP2/SPDY HTTPS协议。
谢谢,Patrick。我采纳了你的建议,利用nginx的$http2
变量,并使用PHP返回动态JS变量来检测浏览器。(如果其他读者喜欢的话,传递cookie或添加响应头进行AJAX检测也是可选项)。
NGINX配置增加内容
server {
...
if ($http2) {
rewrite ^/detect-http2.js /detect-http2.js.php?http2=$http2 last;
}
# fallback to non-HTTP2 rewrite
rewrite ^/detect-http2.js /detect-http2.js.php last;
# add response header if needed in the future
add_header x-http2 $http2;
}
detect-http2.js.php
<?
header('content-type: application/javascript');
if (isset($_REQUEST['http2'])) {
echo "var h2Version='". $_REQUEST['http2'] . "';\n";
}
?>
detect-http2.html
<html>
<body>
<script src="https://DOMAIN_NAME/detect-http2.js"></script>
<script>
document.write('HTTP2-Supported Browser: '+ (typeof h2Version !== 'undefined'));
</script>
</body>
</html>
Alternate-Protocol
头以响应https请求。这将使浏览器在可能的情况下在未来响应SPDY请求(我在更新的SPDY规范中找不到这个信息,所以可能是过时的信息。请谨慎考虑)。