Day 58 NginxHttps
1.1 keepalived
7x24小時不DOWN場景
2臺服務器
主 優先級150 virtu_router_id 50 lb01
備 優先級100 virtu_router_id 50 lb02
1.2 列腦(主和備上面都有虛擬IP --->俗稱VIP)
解決辦法:執行腳本
1.2.1 keepalived造成故障:主和備上都編寫一個腳本:
1.備判斷自己是否能ping通主
2.檢查自己是否存在VIP
3.建議使用kill命令殺死備機的keepalived
1.2.2 Nginx故障,導致請求通過VIP找到Master服務器無法提供服務。
1.檢查Nginx的進程是否存在,如果存在則sleep 5秒,再次檢查
2.如果不存在,則嘗試啓動一次Nginx
3.如果啓動成功,進入下一步,sleep 5秒
4.如果啓動不成功,強制殺掉keepalived讓地址漂移至備機
5.建議寫在Master上面即可。
1.2.3 keepalived用在哪
1.國企,傳統互聯網,全是物理服務器
1.2.4 keepalived不能用在哪
1.互聯網--->使用公有云的 (LB)
2.公有云不能使用keepalived工具,公有云本身負載均衡支持高可用
1.2.5 面試被問到:你們高可用如何實現的??
我們使用公有云的LB負載均衡,本身廠商就支持高可用,所以這一塊我們沒做考慮。
但:如果貴公司使用的是硬件服務器,那麼也可以使用keepalived開源軟件實現高可用。
虛擬機上面:
一臺服務器的https
前端負載均衡,後端是web服務器,實現https
阿里雲:
ecs 運行一個nginx 啓用https
slb+ecs實現https
1.3 Rewrite
1.3.1 什麼是rewrite
Rewrite即URL重寫,主要實現url地址重寫, 以及重定向, 就是把傳入Web的請求重定向到其他URL的過程。
1.3.2 Rewrite使用場景
1.URL地址跳轉,
1.如用戶訪問bgx.com將其跳轉到xuliangwei.com
2.當用戶通過http的方式訪問bgx.com時,將其跳轉至https的方式訪問bgx.com
2.URL僞靜態, 將動態頁面顯示爲靜態頁面方式的一種技術, 便於搜索引擎的錄入, 同時減少動態URL地址對外暴露過多的參數, 提升更高的安全性。
3.搜索引擎SEO優化依賴於url路徑, 以便支持搜索引擎錄入
$request_filename;
http://ds.oldboy.com/nginx.png --》 /soft/code/images/nginx.png
$request_uri;
http://ds.oldboy.com/nginx.png --》 /nginx.png
1.3.3 配置開啓Nginx的Rewrite
設置/etc/nginx/nginx.conf配置文件中的錯誤日誌級別爲notice(nginx中最低級別的錯誤)
error_log /var/log/nginx/error.log notice;
1.3.4 在http模塊層, 增加一行rewrite_log日誌
rewrite_log on;
1.3.5 配置rewrite,測試日誌是否生效
location / {
rewrite ^/ https://www.xuliangwei.com;
}
1.3.6 重啓nginx具體可能是 service nginx reload等命令OK,接下來你可以到錯誤日誌目錄查看具體的rewrite信息了。
2018/09/29 09:29:54 [notice] 1389#1389: *6 "^/" matches "/", client: 10.0.0.1, server: ds.oldboy.com, request: "GET / HTTP/1.1", host: "ds.oldboy.com"
2018/09/29 09:29:54 [notice] 1389#1389: *6 rewritten redirect: "https://www.xuliangwei.com",
1.4 r.oldboy.com
1.4.1 eg 1:用戶訪問/abc/1.html實際上真實訪問是/cc/bb/2.html
1.先配置好真實的路徑
server {
listen 80;
server_name r.oldboy.com;
location / {
root /code;
index index.html;
}
}
2.準備對應的站點目錄
[root@web03 ~ ]# mkdir /code/cc/bb -p
[root@web03 ~ ]# echo "cc_bb_2" > /code/cc/bb/2.html
[root@web03 ~ ]# systemctl restart nginx
3.配置rewrite跳轉規則
[root@web03 conf.d]# cat rewrite.conf
server {
listen 80;
server_name r.oldboy.com;
location / {
root /code;
index index.html;
}
#不區分大小寫匹配
location ~* /abc/1.html {
rewrite /abc/1.html /cc/bb/2.html;
}
}
3.配置rewrite跳轉規則【修訂1次】
[root@web03 conf.d]# cat rewrite.conf
server {
listen 80;
server_name r.oldboy.com;
location / {
root /code;
index index.html;
}
#不區分大小寫匹配
location ~* /abc/1.html {
rewrite (.*) /cc/bb/2.html redirect;
}
}
4.配置rewrite跳轉規則【修訂2次】
[root@web03 conf.d]# cat rewrite.conf
server {
listen 80;
server_name r.oldboy.com;
location / {
root /code;
index index.html;
}
#不區分大小寫匹配
location ~* /abc/1.html {
return 302 /cc/bb/2.html;
}
}
1.4.2 eg 2:用戶訪問/2018/ccc/bbb/2.html實際上真實訪問是/2014/ccc/bbb/2.html
#http://www.bgx.com/2018/cc/bb/2.html ==> http://www.bgx.com/2014/cc/bb/2.html
1.先配置好真實的路徑
server {
listen 80;
server_name r.oldboy.com;
location / {
root /code;
index index.html;
}
}
2.準備對應的站點目錄
[root@web03 ~ ]# mkdir /code/2014/cc/bb -p
[root@web03 ~ ]# echo "2014" > /code/2014/cc/bb/2.html
[root@web03 ~ ]# systemctl restart nginx
3.配置rewrite跳轉規則
[root@web03 conf.d]# cat rewrite.conf
server {
listen 80;
server_name r.oldboy.com;
location / {
root /code;
index index.html;
}
#不區分大小寫匹配
location ~* /2018/cc/bb/1.html {
rewrite (.*) /2014/cc/bb/2.html;
}
}
1.4.3 eg 3:用戶訪問/test目錄下任何內容, 實際上真實訪問是http://www.xuliangwei.com
[root@web03 conf.d]# cat rewrite.conf
server {
listen 80;
server_name r.oldboy.com;
location /test {
rewrite (.*) https://www.xuliangwei.com;
}
}
1.4.4 eg 4:用戶訪問course-11-22-33.html 實際上真實訪問是 /course/11/22/33/course_33.html
1.先配置好真實的路徑
server {
listen 80;
server_name r.oldboy.com;
location / {
root /code;
index index.html;
}
}
2.準備對應的站點目錄
[root@web03 ~ ]# mkdir /code/course/11/22/33/ -p
[root@web03 ~ ]# echo "bgx123.com" > /code/course/11/22/33/course_33.html
[root@web03 ~ ]# systemctl restart nginx
3.配置rewrite跳轉規則
[root@web03 conf.d]# cat rewrite.conf
server {
listen 80;
server_name r.oldboy.com;
location / {
root /code;
index index.html;
#僅針對一條規則
#rewrite ^/course-11-22-33.html /course/11/22/33/course_33.html;
# 如果課程路徑都一樣, 那這條規則靈活度更高
rewrite ^/(.*)-(.*)-(.*)-(.*).html /$1/$2/$3/$4/$1_$4.html;
}
}
1.4.5 eg 5:將http請求,跳轉至https
server {
listen 80;
server_name bgx.com;
rewrite ^(.*) https://$server_name$1 redirect;
#return 302 https://$server_name$request_uri;
}
server {
listen 443;
server_name bgx.com;
ssl on;
}
rewrite指令根據表達式來重定向URI, 或者修改字符串。 可以應用於server,location, if環境下, 每行rewrite指令最後跟一個flag標記,支持的flag標記有如下表格所示:
flag
last 本條規則匹配完成後,停止匹配,不在匹配後面的規則
break 本條規則匹配完成後,停止匹配,不在匹配後面的規則
redirect 返回302臨時重定向, 地址欄會顯示跳轉後的地址
permanent 返回301永久重定向, 地址欄會顯示跳轉後的地址
1.4.6 臨時跳轉302
location /test {
rewrite (.*) https://www.xuliangwei.com redirect;
}
每一次瀏覽器都會詢問服務端(瀏覽器不會記錄r.oldboy.com/test---》)
1.4.7 永久跳轉301
location /test {
rewrite (.*) https://www.xuliangwei.com permanent;
}
只要瀏覽器訪問一次,會記錄這個跳轉,下次再也不會詢問,直接跳轉
對比flag中break與last
1.4.8 公司目前產品有一個url地址是r.oldboy.com/2017_old隨着時間的推移,公司希望客戶通過新的url訪問www.oldboy.com/2019_new需要保證瀏覽器的url地址不發生變化
root@web03 conf.d]# vim rewrite.conf
server {
listen 80;
server_name r.oldboy.com;
root /code;
location ~ ^/2019_new {
rewrite ^/2019_new /2017_old/ break;
}
location ~ ^/2020_new {
rewrite ^/2020_new /2017_old/ last;
}
location ~ ^/2017_old {
root /code;
index index.html;
}
[root@web03 conf.d]# mkdir /code/2017_old -p
[root@web03 conf.d]# echo "2017_code" > /code/2017_old/index.html
[root@web03 conf.d]# systemctl restart nginx
1.5 last與break對比總結:
last r.oldboy.com/2020_new ----> r.oldboy.com/2017_old
1.last進行Rewrite匹配成功, 停止當前這個請求, 並根據rewrite匹配的規則重新向Server發起一個請求。
2.新請求的內容是域名+URL, 對應的地址爲www.oldboy.com/2017_old
break r.oldboy.com/2019_new ----> /2017_old 不存在404
1.break進行Rewrite匹配成功後, 不會像last重新發起請求, 首先查找站點目錄下/code/2017_old/默認返回頁是否存在, 如不存在則404, 如存在繼續往下匹配
2.根據Rewrite匹配的規則, 跳轉至r.oldboy.com/2017_old/的URL地址, 匹配成功後則不繼續匹配
2.1 配置HTTPS前預備知識
2.1.1 HTTPS證書購買選擇
HTTPS證書的選擇
專業版OV型證書 不顯示企業名稱
高級版EV型證書 顯示企業名稱
2.1.2 HTTPS證書購買的類型 oldboy.com (www m image 二級域名)
保護1個域名 www
保護5個域名 www images cdn test m
通配符域名 *.oldboy.com
2.1.3 HTTPS注意事項
Https不支持續費,證書到期需重新申請進行替換
https不支持三級域名解析 test.m.oldboy.com
Https顯示綠色, 說明整個網站的URL都是https的。
Https顯示×××, 因爲網站代碼中包含http的不安全連接。
Https顯示紅色,要麼證書是假的,要麼證書過期。
自己頒發的證書統統都是假的(我們只管使用)
2.1.4 配置ssl認證證書
1.檢查當前環境
//openssl必須是1.0.2
[root@Nginx ~]# openssl version
OpenSSL 1.0.2k-fips 26 Jan 2017
//nginx必須有ssl模塊
[root@Nginx ~]# nginx -V
--with-http_ssl_module
[root@Nginx ~]# mkdir /etc/nginx/ssl_key -p
[root@Nginx ~]# cd /etc/nginx/ssl_key
2.使用openssl充當CA權威機構創建私鑰(生產不可能使用此方式生成證書,不被互聯網CA權威承認的黑戶證書)
[root@Nginx ssh_key]# openssl genrsa -idea -out server.key 2048
3.生成自簽證書,同時去掉私鑰的密碼
[root@Nginx ssl_key]# openssl req -days 36500 -x509 \
-sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
4.配置Nginx
[root@web03 conf.d]# cat https.conf
server {
listen 443;
server_name s.oldboy.com;
ssl on;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
root /code;
index index.html;
}
}
5.創建目錄,啓動服務
[root@web03 conf.d]# mkdir /code
[root@web03 conf.d]# echo "Https...." > /code/index.html
[root@web03 conf.d]# systemctl restart nginx
6.配置Host解析
10.0.0.9 s.oldboy.com
7.強制跳轉訪問http的請求至https
[root@web03 conf.d]# cat https.conf
server {
listen 443;
server_name s.oldboy.com;
ssl on;
ssl_certificate ssl_key/server.crt;
ssl_certificate_key ssl_key/server.key;
location / {
root /code;
index index.html;
}
}
server {
listen 80;
server_name s.oldboy.com;
rewrite (.*) https://$server_name$request_uri redirect;
}
如果添加了flag標記,則會有跳轉到https的信息 301。
如果沒有添加flag標記,則沒有跳轉https的信息 200。
2.2 rewrite
2.2.1 開啓rewrite日誌
測試訪問rewrite日誌是否能查看到
測試rewrite的匹配規則
rewrite的flag標記
last與break
redirect和permanent
2.2.2 https加密
1.證書的購買
2.https證書購買一個域名,還是多個域名,還是泛域名 oldboy.com
3.https不支持三級域名解析