nginx支持HTTP2的配置過程


一、獲取安裝包

http://zlib.net/zlib-1.2.11.tar.gz 

https://www.openssl.org/source/openssl-1.0.2e.tar.gz (openssl的版本必須在1.0.2e及以上)

http://nginx.org/download/nginx-1.10.3.tar.gz   (nginx的版本必須在1.9.5以上)

二、解壓三個包到相同目錄編譯nginx

 ./configure --prefix=/usr/local/nginx --with-openssl=../openssl-1.0.2e --with-pcre --with-zlib=../zlib-1.2.11 --with-stream --with-stream_ssl_module --with-http_ssl_module --with-http_v2_module --with-threads 
make
make install

三、配置運行

配置nginx偽證書、配置文件中開啟https即可

步驟1: 到需要放置證書的目錄(選在nginx的conf目錄下就可以),建立服務器的私鑰(此過程需輸入密碼短語)

openssl genrsa -des3 -out server.key 1024

步驟2: 創建證書簽名請求csr

openssl req -new -key server.key -out server.csr

步驟3:對於使用上面私鑰啟動具有ssl功能的nginx,有必要移除輸出的密碼

cp server.key server.key.org
openssl rsa -in server.key.org -out server.key

步驟4: 使用上面的私鑰和CSR對證書進行簽名

openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

步驟5:配置nginx

    server {
        listen       443 ssl http2;
        server_name  localhost;

        ssl_certificate     server.crt;
        ssl_certificate_key  server.key;
    }

步驟6:啟動nginx並用firefox瀏覽器訪問

(firefox瀏覽器的版本必須在35以上,並且在"about:config"中開啟了"network.http.spdy.enabled.http2draft"選項)

訪問后查看報文,協議版本是否為HTTP 2.0和firefox是否插入了自己的頭"X-Firefox-Spdy:"

當然如果到這成功的話,事情也就結束了,偏偏我連升了3次fireforx的版本,都在35以上,最后升級到52版本,然並卵,"about:config"中只有"network.http.spdy.enabled.http2"這一項,而且還是開啟的。但是訪問的時候,查看請求頭,依然是HTTP1.1。谷歌也是一樣不行。來不及懷疑人生,只能換curl試試。

四、curl模擬HTTP2.0請求

1. 下載源碼安裝包

nghttp2:https://codeload.github.com/nghttp2/nghttp2/zip/master

curl: https://curl.haxx.se/download/curl-7.53.1.tar.gz

2. 安裝http2

yum install install git g++ make binutils autoconf automake autotools-dev libtool pkg-config zlib1g-dev libcunit1-dev libssl-dev libxml2-dev libev-dev libevent-dev libjansson-dev libjemalloc-dev cython python3-dev python-setuptools
unzip nghttp2-master.zip
cd nghttp2-master
autoreconf -i
automake
autoconf
./configure
make
sudo make install

3. 安裝curl

https://curl.haxx.se/download/curl-7.53.1.tar.gz
tar -zxvf curl-7.53.1.tar.gz
cd curl-7.53.1
./configure --with-nghttp2=/usr/local --with-ssl
make

4. 驗證

[root@localhost src]# ./curl --version
curl 7.53.1 (x86_64-pc-linux-gnu) libcurl/7.53.1 OpenSSL/1.0.1e zlib/1.2.7 nghttp2/1.22.0-DEV
Protocols: dict file ftp ftps gopher http https imap imaps pop3 pop3s rtsp smb smbs smtp smtps telnet tftp 
Features: IPv6 Largefile NTLM NTLM_WB SSL libz HTTP2 UnixSockets HTTPS-proxy 

紅色字體說明curl已經支持HTTP2和HTTPS,然后到curl的程序目錄,執行,查看nginx是否支持http2;

cd src
./curl --http2 -v https://192.168.0.1/a.jpg -k

日志太多,但是中間會有一行"Using HTTP2, server supports multi-use"說明是用了http2的;

在nginx的access.log中也可以看見訪問記錄為"GET /a.jpg HTTP/2.0" 200 18082 "-" "curl/7.53.1"也可以說明是用的HTTP2.0

最后我用常用的搜狗瀏覽器訪問"https://192.168.0.1/a.jpg"發現使用的也是HTTP2.0,但是搜狗瀏覽器的F12里面並沒顯示這個請求頭,nginx的日志中可以看見。

五、參考網址

http://wiki.jikexueyuan.com/project/http-2-explained/firefox.html

https://my.oschina.net/leicc/blog/601293?p={{totalPage}}

http://www.linuxidc.com/Linux/2016-01/127588.htm


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM