一、HTTPS解析
https 加密
私鑰
公鑰
http 的握手 是確認網絡是連通的。
https 的握手 是一個加密的過程 加密圖

二、 使用Nginx 部署HTTPS 服務
1.證書生成命令(https://gist.github.com/Jokcy/5e73fd6b2a9b21c142ba2b1995150808) copy 里面的命令 在 Git上面運行
2.訪問網站獲得的命令
openssl req -x509 -newkey rsa:2048 -nodes -sha256 -keyout localhost-privkey.pem -out localhost-cert.pem
3.在 git 上運行此命令

就會生成如下兩個文件:

4. 啟動 nginx 報錯:bind() to 0.0.0.0:443 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions
這是由於其他進程占用了nginx 的端口。
解決辦法:
運行 cmd, 輸入netstat -aon|findstr "443"
找到 0.0.0.0:443,找到 PID,在任務管理器結束進程。 vmware-hostd.exe
5.成功啟動Nginx

5.啟動一個 nodejs 的服務:
然后再瀏覽器輸入test,默認跳轉https 服務。

2.配置 Nginx 代碼
proxy_cache_path cache levels=1:2 keys_zone=my_cache:10m; # 把http變為 https server { listen 80 default_server; listen [::]:80 default_server; server_name test.com; return 302 https://$server_name$request_uri; } server { listen 443; server_name test.com; #開啟https驗證 ssl on; #Nginx 1.5 以后 不需要 ssl on 直接刪除這行代碼即可(但是刪除以后就不會出現https 服務了,所以還是不要刪除) ssl_certificate_key ../certs/localhost-privkey.pem; ssl_certificate ../certs/localhost-cert.pem; location / { proxy_cache my_cache; proxy_pass http://127.0.0.1:8888; proxy_set_header Host $host; } }
三、HTTP2的優勢和Nginx配置HTTP2的簡單實用
1.優勢:
信道復用
分幀傳輸
Server Push

2.開啟 http2 協議 僅僅支持在https協議。
效果圖:

查看http 2 的push 服務端推送特性 chrome://net-internals/#events

server.js 代碼:
const http = require('http') const fs = require('fs') http.createServer(function (request, response) { console.log('request come', request.url) const html = fs.readFileSync('test.html', 'utf8') const img = fs.readFileSync('test.jpg') if (request.url === '/') { response.writeHead(200, { 'Content-Type': 'text/html', 'Connection': 'keep-alive', 'Link': '</test.jpg>; as=image; rel=preload' //路徑 格式 服務器加載方式 }) response.end(html) } else { response.writeHead(200, { 'Content-Type': 'image/jpg', 'Connection': 'keep-alive' // or close }) response.end(img) } }).listen(8888) console.log('server listening on 8888')
test.html 代碼:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> </head> <body> <img src="/test.jpg" alt=""> </body> </html>
test.jpg

.conf
proxy_cache_path cache levels=1:2 keys_zone=my_cache:10m; server { listen 80 default_server; listen [::]:80 default_server; server_name test.com; return 302 https://$server_name$request_uri; } server { listen 443 http2; server_name test.com; http2_push_preload on; ssl on; ssl_certificate_key ../certs/localhost-privkey.pem; ssl_certificate ../certs/localhost-cert.pem; location / { proxy_cache my_cache; proxy_pass http://127.0.0.1:8888; proxy_set_header Host $host; } }
