时间:2022-08-11 07:12:01 | 来源:网站运营
时间:2022-08-11 07:12:01 来源:网站运营
本文讲述了如何快速简单地搭建一个http2的网站。nginx version: nginx/1.10.3built by gcc 5.3.0 (Alpine 5.3.0)built with OpenSSL 1.0.2k 26 Jan 2017TLS SNI support enabledconfigure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --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_xslt_module=dynamic --with-http_image_filter_module=dynamic --with-http_geoip_module=dynamic --with-http_perl_module=dynamic --with-threads --with-stream --with-stream_ssl_module --with-http_slice_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-ipv6
可以看到最后一行显示nginx已经安装了http2的扩展,nginx从1.9.5版本后就开始支持http_v2module这个扩展,如果你没有这个扩展,可以下载新的nginx源码包,编译时加入--with-http_v2_module这个参数:./configure --prefix=/etc/nginx /--sbin-path=/usr/sbin/nginx /--modules-path=/usr/lib/nginx/modules /--conf-path=/etc/nginx/nginx.conf /--error-log-path=/var/log/nginx/error.log /--http-log-path=/var/log/nginx/access.log /--pid-path=/var/run/nginx.pid /--lock-path=/var/run/nginx.lock /--http-client-body-temp-path=/var/cache/nginx/client_temp /--http-proxy-temp-path=/var/cache/nginx/proxy_temp /--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp /--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp /--http-scgi-temp-path=/var/cache/nginx/scgi_temp /--user=nginx /--group=nginx /--with-http_ssl_module /--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_xslt_module=dynamic /--with-http_image_filter_module=dynamic /--with-http_geoip_module=dynamic /--with-http_perl_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 /
除此以外,由于ALPN逐渐取代NPN成为当前主流的http2协商协议,而OpenSSL 1.0.2 才开始支持 ALPN,要支持http2,你还需要把openssl的版本升级到1.0.2之上。wget https://www.openssl.org/source/openssl-1.0.2-latest.tar.gz
执行openssl version查看openssl当前版本,已经为1.0.2了:openssl versionOpenSSL 1.0.2k 26 Jan 2017
如果你对以上安装nginx和升级openssl的步骤感到麻烦,你也可以通过docker下载nginx alpine版本的镜像,里面已经内置了openssl的最新版本以及支持httpv2的nginx,只需要把证书和网站目录映射到nginx容器内部,运行docker容器:docker run -d /--name=nginx /--net=host /--privileged=true /-v /etc/nginx:/etc/nginx /-v /var/www:/var/www /-v /etc/letsencrypt:/etc/letsencrypt /nginx:stable-alpine
sudo apt-get install certbot
接着使用certbot来部署证书,输入certbot certonly进入命令行的向导:$ certbot certonlyHow would you like to authenticate with the ACME CA?-------------------------------------------------------------------------------1: Spin up a temporary webserver (standalone)2: Place files in webroot directory (webroot)
certbot提供了两种验证方式:location ^~ /.well-known/acme-challenge/ { default_type "text/plain"; root /var/www/le;}location = /.well-known/acme-challenge/ { return 404;}
下一步输入你网站的域名:Please enter in your domain name(s) (comma and/or space separated) (Enter 'c'to cancel):liangwentao.cc
然后下一步输入webroot的路径,就是之前nginx中root指令指向的目录:Input the webroot for test.testnode.com: (Enter 'c' to cancel):/var/www/le
最后certbot验证成功,生成证书:Waiting for verification...Cleaning up challengesGenerating key (2048 bits): /etc/letsencrypt/keys/0000_key-certbot.pemCreating CSR: /etc/letsencrypt/csr/0000_csr-certbot.pem IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at /etc/letsencrypt/live/liangwentao.cc/fullchain.pem. Your cert will expire on 2017-06-18. To obtain a new or tweaked version of this certificate in the future, simply run certbot again. To non-interactively renew *all* of your certificates, run "certbot renew" - If you like Certbot, please consider supporting our work by: Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
执行certbot certificates可以发现证书和私钥都已经在/etc/letsencrypt/live这个目录下了。$ certbot certificatesSaving debug log to /var/log/letsencrypt/letsencrypt.log-------------------------------------------------------------------------------Found the following certs: Certificate Name: liangwentao.cc Domains: liangwentao.cc Expiry Date: 2017-06-18 04:09:00+00:00 (VALID: 87 days) Certificate Path: /etc/letsencrypt/live/liangwentao.cc/fullchain.pem Private Key Path: /etc/letsencrypt/live/liangwentao.cc/privkey.pem-------------------------------------------------------------------------------
最后还要在nginx上添加配置,使用刚才生成的证书:server { listen 443 ssl; listen [::]:443 ssl ipv6only=on; server_name liangwentao.cc; ssl_certificate /etc/letsencrypt/live/liangwentao.cc/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/liangwentao.cc/privkey.pem; ssl_trusted_certificate /etc/letsencrypt/live/liangwentao.cc/chain.pem;}
打开网页,发现左边有了把绿色的小锁了,说明https证书已经被正确安装了:server { listen 443 http2 ssl; listen [::]:443 http2 ssl ipv6only=on; server_name liangwentao.cc; ssl_certificate /etc/letsencrypt/live/liangwentao.cc/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/liangwentao.cc/privkey.pem; ssl_trusted_certificate /etc/letsencrypt/live/liangwentao.cc/chain.pem;}
重启nginx服务,打开网页,此时通过chrome的控制台可以看到协议一列的值为h2,说明网站使用http2协议进行通信:关键词: