萨尔法保护 发表于 2017-12-22 11:34:08

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_namelocalhost;
  

  
ssl_certificate   server.crt;
  
ssl_certificate_keyserver.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. 验证
  

# ./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.iyunv.com/Linux/2016-01/127588.htm
页: [1]
查看完整版本: nginx支持HTTP2的配置过程