首先,你须要有一张SSL证书。若是没钱,能够免费申请一个,请参考个人另外一篇文章《letsencrypt在nginx下的配置》;nginx
而后,若是你的nginx版本太低,须要升级到nginx 1.9.5以上。在Cent OS上这样作:chrome
建立一个文件/etc/yum.repos.d/nginx.repo
segmentfault
输入如下内容:centos
[nginx] name=nginx repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=0 enabled=1
执行yum update nginx
,基本上这时能够顺利升级你的nginx到最高版本;浏览器
由于刚升级完nginx,它会把旧的conf文件更名为xxx.conf.rpmsave
,因此别忘了把conf文件弄回来:服务器
# mv /etc/nginx/conf.d/virtual.conf.rpmsave /etc/nginx/conf.d/virtual.conf # mv /etc/nginx/conf.d/ssl.conf.rpmsave /etc/nginx/conf.d/ssl.conf
修改ssl.conf
,把原来的listen 443;
改为listen 443 ssl http2;
;dom
重启nginx: service nginx restart
网站
就是这么简单。可是我怎么知道怎么证实个人网站确实使用了http2呢?google
这时候你须要在你的Chrome里安装一个扩展程序叫作“HTTP/2 and SPDY indicator
”,它会负责检查你的每一个标签页,若是你的网站采用了http2,那么右上角会出现一个蓝色小闪电,若是你的网站采用了低一下级别的SPDY,会出现一个绿色小闪电,若是什么也没采用,那么就是一个灰色的小闪电。url
仔细检查了一下,发现Google的网站几乎所有都有蓝色小闪电,而国内的网站几乎都没有,而咱们的——有了!经实测,速度确实有所提高。
怎么样,快来安装你的蓝色小闪电吧!彻底免费的哟!
2016年6月9日更新:
好吧,不幸的事情发生了,从2016年5月30日起,我忽然发现我辛辛苦苦作好的蓝色小闪电又消失了,一开始我觉得是服务器出了什么问题,可是随后发现连豆瓣的spdy绿色小闪电也没了,深入怀疑是插件或者浏览器的问题,因而安装了firefox,在firefox里也安装上http/2插件,这时候在firefox里能够看到咱们的蓝色小闪电还在,那么只有多是浏览器的问题。果不其然,在这篇文章里提到Google禁止了全部使用openssl 1.0.1版本的nginx服务器,由于它不支持ALPN。而最要命的是经过yum安装的nginx包里都是用openssl 1.0.1编译的,因而咱们只能本身手工编译本身的nginx服务器。
好吧,本着既然折腾就折腾到底的精神,下载安装openssl 1.0.2:
$ wget -c https://www.openssl.org/source/openssl-1.0.2h.tar.gz $ tar xf openssl-1.0.2h.tar.gz -C /usr/local/ $ cd /usr/local/openssl-1.0.2h $ ./config $ make depend $ make $ make test $ make install $ mv /usr/bin/openssl /usr/bin/openssl_1.0.1e $ ln -s /usr/local/ssl/bin/openssl /usr/bin/openssl $ openssl version OpenSSL 1.0.2h 3 May 2016
这只是第一步,安装好了openssl,下面咱们还要编译安装nginx:
$ wget http://nginx.org/download/nginx-1.11.1.tar.gz $ tar -zxf nginx-1.11.1.tar.gz $ cd nginx-1.11.1/ $ ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-openssl=/usr/local/openssl-1.0.2h --user=nginx --group=nginx --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-http_geoip_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' $ make $ make install
这之后,你再启动nginx的时候,就不是service了,而必须以命令行方式:
$ cd /usr/local/nginx $ cp /etc/nginx/nginx.conf conf/ $ service nginx stop $ /usr/local/nginx/sbin/nginx
之后要中止nginx服务的时候,先看一下nginx的pid是多少:
$ cat /var/run/nginx.pid
而后再温柔地杀死它就能够了:
$ kill -QUIT 12345
好吧,很少说了,具体步骤仍是看官方文档。
再进浏览器,看,咱们的蓝色小闪电又回来了!哼,Google,你难不倒我!