如今不少網站都要是https,包括我以前作的微信小程序都是須要使用https傳輸的,特將以前學習的記錄下,以防忘記php
1、html
1.HTTPS簡介nginx
HTTPS實際上是有兩部分組成:HTTP + SSL / TLS,也就是在HTTP上又加了一層處理加密信息的模塊。服務端和客戶端的信息傳輸都會經過TLS進行加密,因此傳輸的數據都是加密後的數據git
2.https協議原理小程序
首先,客戶端與服務器創建鏈接,各自生成私鑰和公鑰,是不一樣的。服務器返給客戶端一個公鑰,而後客戶端拿着這個公鑰把要搜索的東西加密,稱之爲密文,並連並本身的公鑰一塊兒返回給服務器,服務器拿着本身的私鑰解密密文,而後把響應到的數據用客戶端的公鑰加密,返回給客戶端,客戶端拿着本身的私鑰解密密文,把數據呈現出來微信小程序
推薦博客服務器
2、證書和私鑰的生成微信
#注意:通常生成的目錄,應該放在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
3、配置文件(nginx服務器)session
在/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模塊未開啓
4、開啓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