1. 程式人生 > >2018.3.16 12周5次課

2018.3.16 12周5次課

Linux學習

十二周五次課(3月16日)

12.17 Nginx負載均衡

12.18 ssl原理

12.19 生成ssl密鑰對

12.20 Nginx配置ssl

12.17 Nginx負載均衡

後端web服務器可以有多臺,就可以實現負載均衡

upstream來指定多個web server

查看解析域名的ip命令:dig

安裝dig命令:yum install -y bind-utils

vim /usr/local/nginx/conf/vhost/load.conf //寫入如下內容

upstream qq_com // 在這裏定義後端的web server ,通過upstream來指定多個web服務器,upstream後面的名字qq_com可以更改,如可以更改為qq

{

ip_hash; //它表示根據ip地址把請求分到不同的服務器上。目的是同一個用戶始終保持在同一個機器

server 61.135.157.156:80; //也可以寫成http:// 220.181.57.216:80; ,對應的server塊內的proxy_pass內直接寫qq_com即可,不需要寫https://

server 125.39.240.113:80;

}

server

{

listen 80;

server_name www.qq.com;

location /

{

proxy_pass http://qq_com; //這裏使用的是upstream名即qq_com

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

測試:

curl -x127.0.0.1:80 www.qq.com //通常情況下會訪問我們的默認虛擬主機

技術分享圖片

-t && -s reload

再次執行,就會變成了qq.com的主頁,顯示的是網頁的源碼

說明:nginx不支持代理https,即server語句內的端口無法使用443。

12.18 ssl原理

目前chrome、Firefox各大瀏覽器 也開始針對HTTPS做調整。你有沒有發現,使用百度搜問題時,百度的網址前面是HTTPS ,包括阿銘的猿課官網(ask.apelearn.com)同樣也支持了HTTPS訪問

這說明HTTPS已經成為一種趨勢,相信過不了10年,整個互聯網都會強制使用HTTPS通信,而廢棄目前主流的HTTP。那到底什麽是HTTPS呢?簡單講,它就是一種加密的HTTP協議,如果HTTP通信的數據包在傳輸過程中被截獲,我們可以破譯這些數據包裏面的信息,這裏面不乏一些用戶名、密碼、手機號等敏感的信息。 而如果使用HTTPS通信,即使數據包被截獲,我們也無法破譯裏面的內容圖

技術分享圖片

技術分享圖片

12.19 生成ssl密鑰對

cd /usr/local/nginx/conf

查詢某個命名由哪個包安裝的:rpm -qf `which 命令` (命令不能有別名)

需要用到openssl命令:rpm -qf `which openssl`

技術分享圖片

openssl genrsa -des3 -out tmp.key 2048 //生成rsa形式的私鑰文件,文件名tmp.key,長度2048

技術分享圖片

openssl rsa -in tmp.key -out aminglinux.key //轉換key,取消密碼。-in:指定哪個密鑰要轉換;-out:指定輸出的

技術分享圖片

tmp.key和aminglinux.key是一個文件,只不過前面的有密碼,後面的沒有密碼

rm -f tmp.key //刪除有密碼的文件

openssl req -new -key aminglinux.key -out aminglinux.csr //生成證書請求文件,需要拿這個文件和私鑰一起生產公鑰文件

技術分享圖片

openssl x509 -req -days 365 -in aminglinux.csr -signkey aminglinux.key -out aminglinux.crt //生成公鑰,這裏的aminglinux.crt為公鑰,

技術分享圖片

crt是私鑰,key是私鑰,csr是證書請求文件

技術分享圖片

12.20 Nginx配置ssl

vim /usr/local/nginx/conf/vhost/ssl.conf //加入如下內容

server

{

listen 443; //監聽443端口

server_name aming.com;

index index.html index.php;

root /data/wwwroot/aming.com;

ssl on; //開啟ssl,支持https

ssl_certificate aminglinux.crt; //指定公鑰文件

ssl_certificate_key aminglinux.key; //指定私鑰文件

ssl_protocols TLSv1 TLSv1.1 TLSv1.2; //指定ssl的協議

}

-t && -s reload //若報錯unknown directive “ssl” ,需要重新編譯nginx,加上--with-http_ssl_module

技術分享圖片

在前面編譯nginx時,沒有指定支持ssl

命令查看:/usr/local/nginx/sbin/nginx -V

技術分享圖片

configure arguments: --prefix=/usr/local/nginx

重新編譯nginx

cd /usr/local/src/ nginx-1.13.9

./configure --help |grep -i ssl //用help查看要加載哪個ssl模塊

技術分享圖片

要加--with-http_ssl_module

編譯:

./configure --prefix=/usr/local/nginx --with-http_ssl_module

make && make install

技術分享圖片

-t && -s reload //沒有報錯

技術分享圖片

重啟:/etc/init.d/nginx restart

技術分享圖片

查看監聽端口:netstat –lntp //多了443端口

技術分享圖片

echo “ssl test page.”>/data/wwwroot/aming.com/index.html

測試:

不能用curl -x127.0.0.1:443 https://aming.com會報400錯誤

技術分享圖片

編輯host:vi /etc/hosts

增加一行192.168.37.101 aming.com

curl https://aming.com

技術分享圖片

curl: (60) Peer's certificate issuer has been marked as not trusted by the user.

此種情況多發生在自簽名的證書,報錯含義是簽發證書機構未經認證,無法識別。解決辦法是將簽發該證書的私有CA公鑰cacert.pem文件內容,追加到/etc/pki/tls/certs/ca-bundle.crt。

用主機網頁測試:https://aming.com,如訪問不到,就要看看虛擬機有沒有設置防火墻

Iptables -nvL //查看防火

Iptables -F //清空規則,如果不想清空就加一個443端口的規則

firewall-cmd --add-port=443/tcp

firewall-cmd --list-port

firewall-cmd --query-port=443/tcp

技術分享圖片


2018.3.16 12周5次課