為什么需要使用HTTPS,因?yàn)镠TTP不安全,當(dāng)使用http進(jìn)行消息傳輸時(shí),可能會遭到黑客的劫持和篡改,如果采用https協(xié)議,那么數(shù)據(jù)在傳輸過程中是加密的,所以黑客無法竊取或者篡改數(shù)據(jù)報(bào)文信息,同時(shí)也避免網(wǎng)站傳輸時(shí)信息泄露。
實(shí)現(xiàn)https:需要使用ssl協(xié)議。
一、證書簡介
首先需要申請證書,先去登記機(jī)構(gòu)進(jìn)行身份登記,然后登記機(jī)構(gòu)再通過CSR發(fā)給CA,CA中心通過后會生成一堆公鑰和私鑰,公鑰會在CA證書鏈中保存,公鑰和私鑰證書被部署在WEB服務(wù)器上
當(dāng)瀏覽器訪問https站點(diǎn)時(shí),會去請求我們的證書
Nginx的web服務(wù)器會將公鑰證書發(fā)給瀏覽器
瀏覽器再去驗(yàn)證證書是否合法有效
CA機(jī)構(gòu)會將過期的證書放置在CRL服務(wù)器,CRL服務(wù)的驗(yàn)證效率是非常差的,所以CA又推出了OCSP響應(yīng)程序,OCSP響應(yīng)程序可以查詢指定的一個(gè)證書是否過期,所以瀏覽器可以直接查詢OSCP響應(yīng)程序,但OSCP響應(yīng)程序性能還不是很高
Nginx會有一個(gè)OCSP的開關(guān),當(dāng)我們開啟后,Nginx會主動(dòng)上OCSP上查詢,這樣大量的客戶端直接從Nginx獲取證書是否有效
1.1 證書的類型
對比 |
域名型 DV |
企業(yè)型 OV |
增強(qiáng)型 EV |
綠色地址欄 |
小鎖標(biāo)記+https |
小鎖標(biāo)記+https |
小鎖標(biāo)記+企業(yè)名稱+https |
一般用途 |
個(gè)人站點(diǎn)和應(yīng)用; 簡單的https加密需求 |
電子商務(wù)站點(diǎn)和應(yīng)用; 中小型企業(yè)站點(diǎn) |
大型金融平臺; 大型企業(yè)和政府機(jī)構(gòu)站點(diǎn) |
審核內(nèi)容 |
域名所有權(quán)驗(yàn)證 |
全面的企業(yè)身份驗(yàn)證; 域名所有權(quán)驗(yàn)證 |
最高等級的企業(yè)身份驗(yàn)證; 域名所有權(quán)驗(yàn)證 |
頒發(fā)時(shí)長 |
10分鐘-24小時(shí) |
3-5個(gè)工作日 |
5-7個(gè)工作日 |
單次申請年限 |
1年 |
1-2年 |
1-2年 |
1.2 https注意事項(xiàng)
https不支持續(xù)費(fèi),證書到期需要重新申請并進(jìn)行替換
https不支持三級域名解析,如 test.aa.bb.com
https顯示綠色,說明整個(gè)網(wǎng)站的url都是https的
https顯示黃色,因?yàn)榫W(wǎng)站代碼中包含http的不安全鏈接
https顯示紅色,那么證書是假的或者證書過期。
二、nginx配置https
2.1 安裝nginx
以安裝nginx-1.23.3為例:
#下載nginx
wget http://nginx.org/download/nginx-1.23.3.tar.gz
#解壓
tar -zxvf nginx-1.23.3.tar.gz
cd nginx-1.23.3
#安裝主要依賴包
yum install -y libxml2 libxslt libxslt-devel libxml2-devel gd gd-devel geoip-devel
#編譯
./configure \
--prefix=/usr/local/nginx \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_addition_module \
--with-http_xslt_module \
--with-http_image_filter_module \
--with-http_geoip_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_auth_request_module \
--with-http_random_index_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_ssl_preread_module
#安裝
make && make install
2.2 關(guān)于證書
可以去阿里云或者騰訊云免費(fèi)申請,或者用openssl命令自制證書;
阿里云申請地址
https://yundun.console.aliyun.com/?p=cas#/overview
openssl自制證書文章來源:http://www.zghlxwxcb.cn/news/detail-815455.html
#創(chuàng)建證書目錄
[root@centos7 ~]# mkdir -p /usr/local/nginx/cert
[root@centos7 ~]# cd /usr/local/nginx/cert
##使用openssl命令充當(dāng)CA權(quán)威機(jī)構(gòu)創(chuàng)建證書(生產(chǎn)不使用此方式生成證書,不被互聯(lián)網(wǎng)認(rèn)可的黑戶證書)
#創(chuàng)建證書
[root@centos7 cert]# openssl genrsa -idea -out server.key 2048
Generating RSA private key, 2048 bit long modulus
............+++
..................+++
e is 65537 (0x10001)
Enter pass phrase for server.key: #輸入密碼1234
Verifying - Enter pass phrase for server.key: #再次確認(rèn)密碼1234
#生成自簽證書,同時(shí)去掉私鑰的密碼
[root@centos7 cert]# openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
Generating a 2048 bit RSA private key
..+++
........................................................+++
writing new private key to 'server.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN # 國家名,最多2個(gè)字符
State or Province Name (full name) []:CQ # 省全稱
Locality Name (eg, city) [Default City]:Cq # 城市全稱
Organization Name (eg, company) [Default Company Ltd]:test # 組織機(jī)構(gòu)名稱
Organizational Unit Name (eg, section) []:test # 組織單位名稱
Common Name (eg, your name or your server's hostname) []:www.test.com # 證書要保護(hù)的域名
Email Address []:137708020@qq.com # email
2.3 配置ssl
修改nginx配置文件,開啟https文章來源地址http://www.zghlxwxcb.cn/news/detail-815455.html
server {
listen 443 ssl;
server_name 127.0.0.1;
#填寫證書文件名稱
ssl_certificate /usr/local/nginx/cert/server.crt;
#填寫證書私鑰文件名稱
ssl_certificate_key /usr/local/nginx/cert/server.key;
ssl_session_timeout 5m;
#表示使用的加密套件的類型
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
#表示使用的TLS協(xié)議的類型,您需要自行評估是否配置TLSv1.1協(xié)議。
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
location / {
root html;
index index.html index.htm;
}
......
......
}
2.4 將 http 重定向 https
server {
listen 80;
server_name 127.0.0.1;
#將請求轉(zhuǎn)成https
#早期寫法
rewrite ^(.*)$ https://$host$1;
#最新寫法
return 301 https://$server_name$request_uri;
}
到了這里,關(guān)于nginx配置https訪問的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!