nghttp2 和nginx的实践

主要参考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。