首先,你須要有一張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,你難不倒我!