是否可以从浏览器内部检测到浏览器对HTTP2 / SPDY客户端的支持?
我试图向用户展示他们的浏览器是否支持HTTP2 / SPDY或将使用传统的非HTTP2 / SPDY HTTPs协议。
谢谢,帕特里克。我听取了您的建议,并利用了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>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句