文章目錄
Nginx之HTTPS
HTTPS安全證書基本概述
爲什麼需要使用HTTPS,因爲HTTP不安全,當我們使用http網站時,會遭到劫持和篡改,如果採用https協議,那麼數據在傳輸過程中是加密的,所以黑客無法竊取或者篡改數據報文信息,同時也避免網站傳輸時信息泄露。
那麼我們在實現https時,需要了解ssl協議,但我們現在使用的更多的是TLS加密協議。
那麼TLS是怎麼保證明文消息被加密的呢?在OSI七層模型中,應用層是http協議,那麼在應用層協議之下,我們的表示層,是ssl協議所發揮作用的一層,他通過(握手、交換祕鑰、告警、加密)等方式,是應用層http協議沒有感知的情況下做到了數據的安全加密
那麼在數據進行加密與解密過程中,如何確定雙方的身份,此時就需要有一個權威機構來驗證雙方身份,那麼這個權威機構就是CA機構,那麼CA機構又是如何頒發證書
我們首先需要申請證書,先去登記機構進行身份登記,我是誰,我是幹嘛的,我想做什麼,然後登記機構再通過CSR發給CA,CA中心通過後會生成一堆公鑰和私鑰,公鑰會在CA證書鏈中保存,公鑰和私鑰證書我們拿到後,會將其部署在WEB服務器上
1.當瀏覽器訪問我們的https站點時,他回去請求我們的證書
2.Nginx這樣的web服務器會將我們的公鑰證書發給瀏覽器
3.瀏覽器會去驗證我們的證書是否合法有效
4.CA機構會將過期的證書放置在CRL服務器,CRL服務的驗證效率是非常差的,所以CA有推出了OCSP響應程序,OCSP響應程序可以查詢指定的一個證書是否過去,所以瀏覽器可以直接查詢OSCP響應程序,但OSCP響應程序性能還不是很高
5.Nginx會有一個OCSP的開關,當我們開啓後,Nginx會主動上OCSP上查詢,這樣大量的客戶端直接從Nginx獲取證書是否有效
流程:
1、瀏覽器發起往服務器的443端口發起請求,請求攜帶了瀏覽器支持的加密算法和哈希算法。
2、服務器收到請求,選擇瀏覽器支持的加密算法和哈希算法。
3、服務器下將數字證書返回給瀏覽器,這裏的數字證書可以是向某個可靠機構申請的,也可以是自制的。
4、瀏覽器進入數字證書認證環節,這一部分是瀏覽器內置的TLS完成的:
4.1 首先瀏覽器會從內置的證書列表中索引,找到服務器下發證書對應的機構,如果沒有找到,此時就會提示用戶該證書是不是由權威機構頒發,是不可信任的。如果查到了對應的機構,則取出該機構頒發的公鑰。
4.2 用機構的證書公鑰解密得到證書的內容和證書籤名,內容包括網站的網址、網站的公鑰、證書的有效期等。瀏覽器會先驗證證書籤名的合法性(驗證過程類似上面Bob和Susan的通信)。簽名通過後,瀏覽器驗證證書記錄的網址是否和當前網址是一致的,不一致會提示用戶。如果網址一致會檢查證書有效期,證書過期了也會提示用戶。這些都通過認證時,瀏覽器就可以安全使用證書中的網站公鑰了。
4.3 瀏覽器生成一個隨機數R,並使用網站公鑰對R進行加密。
5、瀏覽器將加密的R傳送給服務器。
6、服務器用自己的私鑰解密得到R。
7、服務器以R爲密鑰使用了對稱加密算法加密網頁內容並傳輸給瀏覽器。
8、瀏覽器以R爲密鑰使用之前約定好的解密算法獲取網頁內容。
模擬服務器篡改內容
配置目標網站nginx
[root@web01 conf.d]# cat test.conf
server {
listen 80;
server_name www.gjy.com;
root /data/code;
index index.html;
charset utf-8;
}
配置網頁
[root@web01 conf.d]# mkdir -p /data/code
[root@web01 conf.d]# cat /data/code/index.html
<h1>標題一</h1>
<h2>標題二</h2>
<h3>標題三</h3>
<h4>標題四</h4>
<h5>標題五</h5>
訪問頁面查看
瀏覽器輸入www.gjy.com ,
配置攔截服務器
#配置web02 代理web01,篡改web01配置的內容
[root@web02 conf.d]# cat lanjie.conf
upstream lanjie {
server 172.16.1.7:80;
}
server {
listen 80;
server_name www.gjy.com;
location / {
proxy_pass http://lanjie;
proxy_set_header Host $http_host;
sub_filter '<h3>' '<h1>';
sub_filter '</h3>' '</h1>';
}
}
瀏覽器驗證篡改
先在cmd上測試下,ping www.gjy.com -------> 10.0.0.8
瀏覽器訪問www.gjy.com ,界面出現的結果是被篡改的,(三級標題篡改成了一級標題)
篡改添加廣告配置
[root@web02 conf.d]# cat lanjie.conf
upstream lanjie {
server 172.16.1.7:80;
}
server {
listen 80;
server_name www.haoda.com;
location / {
sub_filter '<h3>' '<img src="http://img5.imgtn.bdimg.com/it/u=2318812076,766139919&fm=26&gp=0.jpg">';
proxy_pass http://lanjie;
proxy_set_header Host $http_host;
}
}
那麼證書是怎樣組成的呢,接下來我們看一下證書的幾種類型
HTTPS證書購買選擇
保護一個域名 www
保護五個域名 www images cdn test m
通配符域名 *.haoda.com
HTTPS注意事項:
https不支持續費,證書到期需要重新申請並進行替換 https不支持三級域名解析,如 test.m.haoda.com https顯示綠色,說明整個網站的url都是https的 https顯示黃色,因爲網站代碼中包含http的不安全鏈接 https顯示紅色,那麼證書是假的或者證書過期。
Nginx單臺實現HTTPS實戰
環境準備
#nginx必須有ssl模塊
[root@web03 ~]# nginx -V
--with-http_ssl_module
#創建存放ssl證書的路徑
[root@web03 ~]# mkdir -p /etc/nginx/ssl_key
[root@web03 ~]# cd /etc/nginx/ssl_key
使用openssl命令充當CA權威機構創建證書(生產不使用此方式生成證書,不被互聯網認可的黑戶證書)
[root@web03 ssl_key]# openssl genrsa -idea -out server.key 2048
Generating RSA private key, 2048 bit long modulus
...............................................+++
............................................+++
e is 65537 (0x10001)
#密碼暫時使用1234
Enter pass phrase for server.key:
#驗證密碼
Verifying - Enter pass phrase for server.key:
[root@web03 ssl_key]# ls
server.key
生成自簽證書,同時去掉私鑰的密碼
[root@web03 ssl_key]# 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]:china
string is too long, it needs to be less than 2 bytes long
#國家名稱 (只能兩個字符)
Country Name (2 letter code) [XX]:ch
#州或省名稱
State or Province Name (full name) []:shanghai
#地區名稱(如城市)
Locality Name (eg, city) [Default City]:shanghai
#組織名稱(如公司)(默認有限公司)
Organization Name (eg, company) [Default Company Ltd]:oldboy
#組織單元名稱(例如,部分)
Organizational Unit Name (eg, section) []:yunwei
# 常見的名字(例如你的名字或你的服務器的主機名)
Common Name (eg, your name or your server's hostname) []:gjy.com
#電子郵件地址
Email Address []:[email protected]
# req --> 用於創建新的證書
# new --> 表示創建的是新證書
# x509 --> 表示定義證書的格式爲標準格式
# key --> 表示調用的私鑰文件信息
# out --> 表示輸出證書文件信息
# days --> 表示證書的有效期
證書申請完成後需要了解Nginx如何配置https
#啓動ssl功能
Syntax: ssl on | off;
Default: ssl off;
Context: http,server
#證書文件
Syntax: ssl_certificate file;
Default: -
Context: http,server
#私鑰文件
Syntax: ssl_certificate_key fil;
Default: -
Context: http,server
Nginx配置https實例
[root@web03 conf.d]# cat ssl.conf
server {
listen 443 ssl;
server_name ssl.gjy.com;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
root /code;
index index.html;
}
}
#配置將用戶訪問http請求強制跳轉https
server {
listen 80;
server_name ssl.haoda.com;
return 302 https://$server_name$request_uri;
}
#準備對應的站點目錄,並重啓Nginx
[root@web03 conf.d]# echo "Https" > /code/index.html
[root@web03 conf.d]# nginx -s reload
瀏覽器訪問測試
Nginx集羣實現HTTPS實踐
實戰Nginx負載均衡+Nginx WEB配置HTTPS安全
環境準備
主機名 | 外網IP(NAT) | 內網IP(LAN) | 角色 |
---|---|---|---|
lb01 | 10.0.0.5 | 172.16.1.5 | 負載均衡 |
web02 | 10.0.0.8 | 172.16.1.8 | web服務器 |
web03 | 10.0.0.9 | 172.16.1.9 | web服務器 |
配置web01、web02服務器監聽80端口
[root@web01 conf.d]# cat ssl.conf
server {
listen 80;
server_name ssl.gjy.com;
location / {
root /code;
index index.html;
}
}
[root@web01 code]# cat index.html
gjy.test_web01
[root@web02 conf.d]# cat ssl.conf
server {
listen 80;
server_name ssl.gjy.com;
location / {
root /code;
index index.html;
}
}
[root@web02 code]# cat index.html
Https_web02
把證書直接拿到lb服務器
[root@lb01 conf.d]# cd ..
[root@lb01 nginx]# scp -rp 172.16.1.9:/etc/nginx/ssl_key ./
配置lb01的nginx配置
[root@lb01 conf.d]# cat proxy_ssl.conf
upstream website {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 443 ssl;
server_name ssl.gjy.com;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
proxy_pass http://website;
proxy_set_header Host $http_host;
}
}
server {
listen 80;
server_name ssl.gjy.com;
return 302 https://$server_name$request_uri;
}
瀏覽器訪問查看
瀏覽器訪問ssl.gjy.com
真實業務場景實現HTTPS實踐
配置知乎、博客對應的負載均衡lb01服務器的配置
配置web01,web02配置文件
#配置wordpress配置文件
[root@web01 conf.d]# cat blog.conf
server {
listen 80;
server_name blog.gjy.com;
location / {
root /code/wordpress;
index index.php index.html;
}
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
include fastcgi_params;
}
}
#配置知乎配置文件
[root@web01 conf.d]# cat zh.conf
server {
listen 80;
server_name zh.gjy.com;
root /code/zh;
index index.php index.html;
location ~ \.php$ {
root /code/zh;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#告訴PHP我前置的負載使用的是https協議
fastcgi_param HTTPS on;
include fastcgi_params;
}
}
配置代理配置文件
#配置代理配置文件
[root@lb01 conf.d]# cat proxy_haoda.conf
upstream blog {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
#用戶的http請求跳轉至https
server {
listen 80;
server_name blog.gjy.com;
return 302 https://$server_name$request_uri;
}
server {
listen 80;
server_name zh.gjy.com;
return 302 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name blog.gjy.com;
ssl_certificate /etc/nginx/ssl_key/server.crt;
ssl_certificate_key /etc/nginx/ssl_key/server.key;
location / {
proxy_pass http://blog;
proxy_set_header Host $http_host;
}
}
server {
listen 443 ssl;
server_name zh.gjy.com;
ssl_certificate /etc/nginx/ssl_key/server.crt;
ssl_certificate_key /etc/nginx/ssl_key/server.key;
location / {
proxy_pass http://blog;
proxy_set_header Host $http_host;
}
}
#重啓負載nginx
[root@lb01 conf.d]# nginx -t
[root@lb01 conf.d]# nginx -s reload
瀏覽器查看效果
瀏覽器訪問wordpress blog.gjy.com
瀏覽器訪問知乎 ,zh.gjy.com
修正亂碼效果,配置知乎、博客對應的web服務器的配置
#負載訪問使用的https後端web使用的是http,對於PHP來說他並不知道用的到底是什麼所以會出現錯誤;
#修正該問題配置
[root@web01 conf.d]# cat zh.conf
server {
listen 80;
server_name zh.gjy.com;
root /code/zh;
index index.php index.html;
location ~ \.php$ {
root /code/zh;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#告訴PHP我前置的負載使用的是https協議
fastcgi_param HTTPS on;
include fastcgi_params;
}
}
[root@web02 conf.d]# cat wordpress.conf
server {
listen 80;
server_name blog.gjy.com;
root /code/wordpress;
index index.php index.html;
client_max_body_size 100m;
location ~ \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
include fastcgi_params;
}
}
#重啓兩臺nginx
[root@web01 conf.d]# nginx -s reload
[root@web02 conf.d]# nginx -s reload
瀏覽器再次查看效果
wordpress早期安裝如果是使用的http方式,那開啓https後會導致圖片出現破損或加載不全的情況
建議:1、在安裝WordPress之前就配置好https;2、在WordPress後臺管理頁面,設置–>常規–>修改(WordPress地址及站點地址)爲 https:// 3、注意:WordPress很多鏈接在安裝時被寫入數據庫中。
配置PHPmyadmin負載均衡lb01服務器的配置
[root@lb01 conf.d]# cat proxy_php.conf
upstream php {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 80;
server_name php.gjy.com;
return 302 https://$server_name$request_uri;
}
server {
listen 443;
ssl on;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
server_name php.gjy.com;
location / {
proxy_pass http://php;
include proxy_params;
}
}
瀏覽器查看效果
配置PHPmyadmin的web服務器配置
[root@web01 conf.d]# cat php.conf
server {
listen 80;
server_name php.gjy.com;
root /code/phpMyAdmin-4.9.0.1-all-languages;
location / {
index index.php index.html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
include fastcgi_params;
}
}
瀏覽器再次查看效果
配置PHPmyadmin的web服務器配置
[root@web01 conf.d]# cat php.conf
server {
listen 80;
server_name php.gjy.com;
root /code/phpMyAdmin-4.9.0.1-all-languages;
location / {
index index.php index.html;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS on;
include fastcgi_params;
}
}