主要參考https://bg2bkk.github.io/post/HTTP2%E7%9A%84%E5%AE%9E%E8%B7%B5%E8%BF%87%E7%A8%8B/,和https://fangpeishi.com/http2_proxy.html。
第三個挺有價值的鏈接是https://wzyboy.im/post/1052.html,但很多內容和上面的重復了。實際操作時,不必借鑒這個博客。
在/etc/pki/CA下創建初始文件
$ touch serial index.txt
$ echo 01 > serial
$ cd /etc/pki/CA
$ openssl genrsa -out private/cakey.pem 2048
使用req指令,通過私鑰,生成自簽證書
$ openssl req -new -x509 -key private/cakey.pem -out cacert.pem
為nginx server生成密鑰
$ mkdir /root/data/nginx_ssl;cd /root/data/nginx_ssl
$ openssl genrsa -out nginx.key 2048
為nginx生成 證書簽署請求
$ openssl req -new -key nginx.key -out nginx.csr
向CA請求證書
$ openssl ca -in nginx.csr -out nginx.crt 如果失敗,可以嘗試以下命令
$ openssl x509 -req -in nginx.csr -CA /etc/pki/CA/cacert.pem -CAkey /etc/pki/CA/private/cakey.pem -CAcreateserial -out nginx.crt
配置nginx
listen 3128;
......
server {
listen 8443 ssl http2;
ssl_certificate "/root/data/nginx_ssl/nginx.crt";
ssl_certificate_key "/root/data/nginx_ssl/nginx.key"
systemctl start nginx
用 lynx http://192.168.3.135:3128, 和 lynx https://192.168.3.135:8443 測試
接下來我們來配置nghttpx,yum -y install nghttp2
實際測試,發現在centos7下可以安裝nghttp2的三個app,在centos6下,沒法找到安裝好的3個app。
nghttpd作為http2 server
http2-no-tls
nghttpd -v 8080 -n 24 --no-tls -d ~/workspace/Nginx_ABTesting/utils/html/
http2-with-tls
nghttpd -v 8080 -n 24 /usr/lib/ssl/nginx.key /usr/lib/ssl/nginx.crt -d ~/workspace/Nginx_ABTesting/utils/html/
實際測試發現no-tls不work。
不關注nghttp2作為客戶端的運行情況,關注
nghttpx作為proxy,轉向nginx后端的情況,這里主要參考開頭提到的第二個鏈接來操作。
編輯配置文件 /etc/nghttpx/nghttpx.conf
:
frontend=0.0.0.0,443 backend=127.0.0.1,3128 private-key-file=/root/data/nginx_ssl/nginx.key certificate-file=/root/data/nginx_ssl/nginx.crt http2-proxy=yes errorlog-syslog=yes workers=1 add-x-forwarded-for=no no-via=yes no-ocsp=yes #tls-proto-list=TLSv1.2 tls-min-proto-version=TLSv1.0 tls-max-proto-version=TLSv1.2 ciphers=ECDHE+AES128
這個是通過測試的。原來配的backend的端口是8443,發現無法work。
tls-proto-list=TLSv1.2提示“deprecated”,修改為...min...和...max....
作為服務,systemctl restart nghttpx 這樣啟動nghttpx更合適。
最終測試: https://192.168.3.135, 135機器上,443作為nghttpx的前端,收到request,轉發到3128上,nginx正好監聽這個端口,處理后,把主頁返回到客戶端瀏覽器。瀏覽器必須
是支持http2的瀏覽器,chrome或者firefox。