开启网站或接口对http2的支持

http2完全兼容http1.1。http2的优点请参考:https://zhuanlan.zhihu.com/p/26559480

这里以nginx为例:

条件:

1. 软件:

nginx -V查版本,需大于1.9.5,并且已经开启“ with-http_v2_module

openssl version查版本,需大于1.0.2k

2. 需要开启https:

listen 443 ssl;

改成

listen 443 ssl http2; 

然后重启nginx即可。

全部nginx的server配置如下:


server {
	listen 80;
	listen 443 ssl http2; # 支持http2
	server_name m.xxx.com;
	index index.php index.html index.htm;
	
	root /home/wwwroot/default/m.xxx.com;
	
	if ($scheme = http ) {
		return 301 https://$host$request_uri;
	}
	
	if ($server_port = 80 ) {
		return 301 https://$host$request_uri;
	}

	#ssl on;   //这行必须要注释掉
	ssl_certificate /usr/local/nginx/conf/https/m.xxx.com_chain.crt;
	ssl_certificate_key /usr/local/nginx/conf/https/m.xxx.com_key.key;
	
	# 其他
	
	#astcgi_split_path_info ^(.+\.php)(/.+)$;
	fastcgi_param   PATH_INFO   $fastcgi_path_info;
	fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;

	#error_page   404   /404.html;

	# Deny access to PHP files in specific directory
	#location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }

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

	include enable-php.conf;

	location /nginx_status
	{
		stub_status on;
		access_log   off;
	}

	location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
	{
		expires      30d;
	}

	location ~ .*\.(js|css)?$
	{
		expires      12h;
	}

	location ~ /.well-known {
		allow all;
	}

	location ~ /\.
	{
		deny all;
	}

	#access_log  /home/wwwlogs/access.log;
	
}

3. 查看网站或接口是否已经支持http2

方法1:浏览器安装一个插件:HTTP Indicator ,下载:https://chrome.google.com/webstore/detail/http-indicator/hgcomhbcacfkpffiphlmnlhpppcjgmbl

显示为彩色则表示已开启:

方法2:使用js:

(function(){
    // 保证这个方法只在支持loadTimes的chrome浏览器下执行
    if(window.chrome && typeof chrome.loadTimes === 'function') {
        var loadTimes = window.chrome.loadTimes();
        var spdy = loadTimes.wasFetchedViaSpdy;
        var info = loadTimes.npnNegotiatedProtocol || loadTimes.connectionInfo;
        // 就以 「h2」作为判断标识
        if(spdy && /^h2/i.test(info)) {
            return console.info('本站点使用了HTTP/2');
        }
    }
    console.warn('本站点没有使用HTTP/2');
})();

结果:

-

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值