要使用http2,你需要做三件事:

搭建一個(gè)web server(nginx, apache, nodejs…)

配置https

開啟http2

下面進(jìn)入正題。

安裝nginxInstalling nginx


為了支持http2,ngi" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁 > 營銷資訊 > 網(wǎng)站運(yùn)營 > 快速搭建一個(gè)http2的網(wǎng)站

快速搭建一個(gè)http2的網(wǎng)站

時(shí)間:2022-08-11 07:12:01 | 來源:網(wǎng)站運(yùn)營

時(shí)間:2022-08-11 07:12:01 來源:網(wǎng)站運(yùn)營

本文講述了如何快速簡(jiǎn)單地搭建一個(gè)http2的網(wǎng)站。

要使用http2,你需要做三件事:

  1. 搭建一個(gè)web server(nginx, apache, nodejs…)

  2. 配置https

  3. 開啟http2

下面進(jìn)入正題。

安裝nginx

Installing nginx


為了支持http2,nginx需要安裝http_v2_module這個(gè)擴(kuò)展,執(zhí)行nginx -V來檢查你的nginx是否已經(jīng)安裝了這個(gè)擴(kuò)展:

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已經(jīng)安裝了http2的擴(kuò)展,nginx從1.9.5版本后就開始支持http_v2module這個(gè)擴(kuò)展,如果你沒有這個(gè)擴(kuò)展,可以下載新的nginx源碼包,編譯時(shí)加入--with-http_v2_module這個(gè)參數(shù):

./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成為當(dāng)前主流的http2協(xié)商協(xié)議,而OpenSSL 1.0.2 才開始支持 ALPN,要支持http2,你還需要把openssl的版本升級(jí)到1.0.2之上。

要升級(jí)openssl,你需要把openssl 1.0.2的源碼包下載下來重新編譯:

wget https://www.openssl.org/source/openssl-1.0.2-latest.tar.gz執(zhí)行openssl version查看openssl當(dāng)前版本,已經(jīng)為1.0.2了:

openssl versionOpenSSL 1.0.2k 26 Jan 2017如果你對(duì)以上安裝nginx和升級(jí)openssl的步驟感到麻煩,你也可以通過docker下載nginx alpine版本的鏡像,里面已經(jīng)內(nèi)置了openssl的最新版本以及支持httpv2的nginx,只需要把證書和網(wǎng)站目錄映射到nginx容器內(nèi)部,運(yùn)行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

配置HTTPS

Let's Encrypt是一個(gè)提供免費(fèi)SSL/TLS證書的數(shù)字證書認(rèn)證機(jī)構(gòu),下面使用Let's Encrypt來為網(wǎng)站建立數(shù)字證書。

certbot是一個(gè)部署Let's Encrypt項(xiàng)目的自動(dòng)化工具,打開certbot的官方頁面,看到如下圖的選擇框:


選擇自己機(jī)器上使用的服務(wù)器(如Nginx,Apache...)以及操作系統(tǒng),即可得到對(duì)應(yīng)的操作步驟,例如我的是nginx+debian,官方給出了下面的安裝和使用命令:


安裝certbot:

sudo apt-get install certbot接著使用certbot來部署證書,輸入certbot certonly進(jìn)入命令行的向?qū)В?br>
$ certbot certonly?How would you like to authenticate with the ACME CA?-------------------------------------------------------------------------------1: Spin up a temporary webserver (standalone)2: Place files in webroot directory (webroot)certbot提供了兩種驗(yàn)證方式:

官方推薦的是webroot模式,于是選擇webroot模式。

在webroot模式下我們需要在web server配置一個(gè)路徑以供Let's Encrypt進(jìn)行驗(yàn)證,下面修改nginx的配置,目錄/var/www/le即為L(zhǎng)et's Encrypt進(jìn)行驗(yàn)證的目錄:

location ^~ /.well-known/acme-challenge/ { default_type "text/plain"; root /var/www/le;}?location = /.well-known/acme-challenge/ { return 404;}下一步輸入你網(wǎng)站的域名:

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驗(yàn)證成功,生成證書:

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執(zhí)行certbot certificates可以發(fā)現(xiàn)證書和私鑰都已經(jīng)在/etc/letsencrypt/live這個(gè)目錄下了。

$ 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;}打開網(wǎng)頁,發(fā)現(xiàn)左邊有了把綠色的小鎖了,說明https證書已經(jīng)被正確安裝了:

對(duì)一個(gè)https網(wǎng)站進(jìn)行抓包可以看到tls的通信過程:


1.

Client Hello階段,客戶端向服務(wù)器提供支持的加密方法、壓縮方法和協(xié)議版本,以及一個(gè)隨機(jī)數(shù),發(fā)送給服務(wù)器。

2. Server Hello階段,服務(wù)器根據(jù)客戶端傳來的信息確定稍后使用的加密方法,壓縮方法和協(xié)議版本,以及生成服務(wù)器端的隨機(jī)數(shù)返回給客戶端。

同時(shí)也把證書信息發(fā)送給客戶端:


必要的話服務(wù)器端會(huì)發(fā)送帶有公鑰信息的報(bào)文給客戶端,讓客戶端得到服務(wù)器的證書和公鑰,這個(gè)階段叫Server Key Exchange。

3. 緊接著客戶端會(huì)對(duì)服務(wù)器端發(fā)送過來的證書進(jìn)行驗(yàn)證,確認(rèn)傳輸狀態(tài),這兩個(gè)過程為Client Key ExchangeChange Cipher Spec階段,確認(rèn)無誤后客戶端生成一個(gè)隨機(jī)數(shù)并用服務(wù)器的公鑰加密,給服務(wù)器發(fā)送通知。

4. 最后服務(wù)器已經(jīng)收到客戶端的通知了,用私鑰解密客戶端發(fā)送過來的隨機(jī)數(shù),這時(shí)候雙方都擁有3個(gè)隨機(jī)數(shù)的信息了,于是可以通過加密方法生成對(duì)話密鑰,加密發(fā)送的數(shù)據(jù)了。服務(wù)器這時(shí)候也進(jìn)入Change Cipher Spec階段,確認(rèn)傳輸狀態(tài),雙方可以進(jìn)行加密的通信了,這時(shí)候服務(wù)器還會(huì)給客戶端發(fā)送一個(gè)session ticket,客戶端根據(jù)session ticket可以在客戶端保存會(huì)話狀態(tài),這個(gè)session ticket是被密鑰加密的,所以不用擔(dān)心會(huì)話被竊聽。

啟用http2

要啟用http2,我們修改剛才nginx的配置,在監(jiān)聽的端口后面加上http2參數(shù):

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服務(wù),打開網(wǎng)頁,此時(shí)通過chrome的控制臺(tái)可以看到協(xié)議一列的值為h2,說明網(wǎng)站使用http2協(xié)議進(jìn)行通信:

在chrome地址欄輸入chrome://net-internals/#http2,可以看到當(dāng)前http2的會(huì)話信息,點(diǎn)擊session id,我們可以觀察到這個(gè)會(huì)話的傳輸信息:

通過mimtproxy可以看出這個(gè)http2請(qǐng)求的一些信息,以下結(jié)果顯示了這個(gè)請(qǐng)求使用了HTTP/2.0協(xié)議,TLS版本為1.2,證書機(jī)構(gòu)為L(zhǎng)et's Encrypt:


至此,http2的升級(jí)就算完成了。


---

參考:

專題 | JerryQu 的小站

SSL/TLS原理詳解 - Sean's Notes - SegmentFault

關(guān)鍵詞:

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉