現在很多網站都要是https,包括我之前做的微信小程序都是需要使用https傳輸的,特將之前學習的記錄下,以防忘記
一、
1.HTTPS簡介
HTTPS其實是有兩部分組成:HTTP + SSL / TLS,也就是在HTTP上又加了一層處理加密信息的模塊。服務端和客戶端的信息傳輸都會通過TLS進行加密,所以傳輸的數據都是加密后的數據
2.https協議原理
首先,客戶端與服務器建立連接,各自生成私鑰和公鑰,是不同的。服務器返給客戶端一個公鑰,然后客戶端拿着這個公鑰把要搜索的東西加密,稱之為密文,並連並自己的公鑰一起返回給服務器,服務器拿着自己的私鑰解密密文,然后把響應到的數據用客戶端的公鑰加密,返回給客戶端,客戶端拿着自己的私鑰解密密文,把數據呈現出來
二、證書和私鑰的生成
#注意:一般生成的目錄,應該放在nginx/conf/ssl目錄,創建並進入 #1.創建服務器證書密鑰文件 server.key: openssl genrsa -des3 -out server.key 1024 #輸入密碼,確認密碼,自己隨便定義,但是要記住,后面會用到。需輸入4-1024位字符做為密碼 #2.創建服務器證書的申請文件 server.csr openssl req -new -key server.key -out server.csr #輸出內容為: Enter pass phrase for root.key: ← 輸入前面創建的密碼 Country Name (2 letter code) [AU]:CN ← 國家代號,中國輸入CN State or Province Name (full name) [Some-State]:ShangHai ← 省的全名,拼音 Locality Name (eg, city) []:ShangHai ← 市的全名,拼音 Organization Name (eg, company) [Internet Widgits Pty Ltd]:MyCompany Corp. ← 公司英文名 Organizational Unit Name (eg, section) []: ← 可以不輸入 Common Name (eg, YOUR name) []: 你的域名 Email Address []:admin@mycompany.com ← 電子郵箱,可隨意填 Please enter the following ‘extra’ attributes to be sent with your certificate request A challenge password []: ← 可以不輸入 An optional company name []: ← 可以不輸入 #4.備份一份服務器密鑰文件 cp server.key server.key.org #5.去除文件口令 openssl rsa -in server.key.org -out server.key #6.生成證書文件server.crt openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
三、配置文件(nginx服務器)
在/usr/local/nginx/conf/vhost.conf中,
server { listen 443 ssl; server_name www.jinzhaohui.cn; ssl_certificate ssl/1571883_www.jinzhaohui.cn.pem; ssl_certificate_key ssl/1571883_www.jinzhaohui.cn.key; #ssl_certificate ssl/server.crt; #ssl_certificate_key ssl/server.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { index index.php index.html index.htm; root /opt/hello; if (!-e $request_filename) { rewrite ^(.*)$ /index.php?s=$1 last; break; } } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /opt/hello$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; include fastcgi_params; } }
測試 sbin目錄下,執行.nginx -t
報錯:nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/vhost.conf:3
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
原因是:ssl模塊未開啟
四、開啟nginx的ssl模塊
#1.the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:37 #原因是nginx缺少http_ssl_module模塊,編譯安裝時帶上--with-http_ssl_module配置就可以了 #2.如果已經安裝過nginx,想要添加模塊看下面 #1)切換到nginx源碼包 cd /usr/local/src/nginx-1.11.3 #2)查看ngixn原有的模塊 /usr/local/nginx/sbin/nginx -V #3)重新配置 ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module #4)重新編譯,不需要make install安裝。否則會覆蓋 make #5)備份原有已經安裝好的nginx cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak #6)將剛剛編譯好的nginx覆蓋掉原來的nginx(ngixn必須停止) ps -ef|grep nginx kill -QUIT 2072 cp ./objs/nginx /usr/local/nginx/sbin/ #這時,會提示是否覆蓋,請輸入yes,直接回車默認不覆蓋 #7)啟動nginx,查看nginx模塊,發現已經添加 /usr/local/nginx/sbin/nginx -V