12.17 Nginx負載均衡
12.18 ssl原理
12.19 生成ssl密鑰對
12.20 Nginx配置ssl
擴充
針對請求的uri來代理 http://ask.apelearn.com/question/1049
根據通路的目錄來區分後端的web http://ask.apelearn.com/question/920
nginx長連接配接 http://www.apelearn.com/bbs/thread-6545-1-1.html
nginx算法分析 http://blog.sina.com.cn/s/blog_72995dcc01016msi.html
代理1台Web伺服器稱為代理
代理2台Web伺服器稱為負載均衡
nginx代理不支援https解析
1 域名解析qq.com的ip
利用dig工具檢視(yum install -y bind-utils)
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI9s2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xCM581dvRWYoNHLwEzX5xCMx8FesU2cfdGLwATMfRHLGZkRGZkRfJ3bs92YskmNhVTYykVNQJVMRhXVEF1X0hXZ0xiNx8VZ6l2cssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnLwIjNzcTM1YDM3gTO0ITNx8CX5IDNwgTMwIzLcNXZnFWbp9CXvwVbvNmLvR3YxUjL0M3Lc9CX6MHc0RHaiojIsJye.png)
# vim /usr/local/nginx/conf/vhost/load.conf
upstream qq_com
{
ip_hash;
server 61.135.157.156:80;
server 125.39.240.113:80;
}
server
{
listen 80;
server_name www.qq.com;
location /
{
proxy_pass http://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;
}
}
配置解釋:
借助子產品upstream,
proxy_pass不支援定義多個ip,proxy_pass後面跟的就是upstream的名字。
upstream 與proxy_pass後面接的都是名字,而不是主要域名。
主要域名,ip參數是此三項。
server_name www.qq.com;
server 61.135.157.156:80;
server 125.39.240.113:80;
ip_hash;
ip_hash 目的 同一個使用者始終保留在同一台機器上。
它表示根據ip位址把請求分到不同的伺服器上。
比如使用者A的ip是1.1.1.1,使用者B的IP是2.2.2.2,則A通路的時候會把請求轉發到第一個Web伺服器上,
B通路的時候會到第二個Web伺服器上。
ip_hash 下面可以定義多個ip,格式是ip:port
3 curl 測試(未重新加載前,配置未生效。)
[root@AliKvn vhost]# curl -x127.0.0.1:80 www.qq.com
this is a default site.
未配置的時候,從本機通路www.qq.com的時候是通路本機的預設網站。
4 檢查文法,并重新加載。
[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -s reload
5 curl測試,通過通路,顯示網頁代碼,狀态碼200.
HTTP與HTTPS的差別:
HTTPS是一種加密的HTTP協定,如果HTTP通信的資料包在傳輸過程中被截獲,我們可以破譯這些資料包的資訊,
這裡面可能會有一些使用者名,密碼,手機登敏感資訊。如果使用HTTPS通信,即使資料包被截獲,也無法破譯裡面的内容。
SSL的原理工作流程圖
文字解釋SSL工作流程
1 浏覽器發送一個https的請求給伺服器;
2 伺服器要有一套數字證書,可以自己制作(後面的操作就是阿銘自己制作的證書),也可以向組織申請,差別就是自己頒發的證書需要用戶端驗證通過,才可以繼續通路,而使用受信任的公司申請的證書則不會彈出>提示頁面,這套證書其實就是一對公鑰和私鑰;
3 伺服器會把公鑰傳輸給用戶端;
4 用戶端(浏覽器)收到公鑰後,會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串随機數,并用收到的公鑰加密;
5 用戶端把加密後的随機字元串傳輸給伺服器;
6 伺服器收到加密随機字元串後,先用私鑰解密(公鑰加密,私鑰解密),擷取到這一串随機數後,再用這串随機字元串加密傳輸的資料(該加密為對稱加密,所謂對稱加密,就是将資料和私鑰也就是這個随機字元串>通過某種算法混合在一起,這樣除非知道私鑰,否則無法擷取資料内容);
7 伺服器把加密後的資料傳輸給用戶端;
8 用戶端收到資料後,再用自己的私鑰也就是那個随機字元串解密;
https的工作原理就是反複給資料執行公鑰與私鑰的加密解密的過程。
1 進入/conf目錄
cd /usr/local/nginx/conf/
2 使用openssl工具生成私鑰檔案,定義密碼 .key為私鑰檔案
openssl genrsa -des3 -out tmp.key 2048
3 轉換key,取消密碼。因為密碼每次加密解密都輸入密碼過于頻繁,此處删除密碼。
openssl rsa -in tmp.key -out aminglinux.key
Enter pass phrase for tmp.key:
writing RSA key
4 删除tmp.key
rm -f tmp.key
5 生成證書請求檔案csr,需要拿這個檔案和私鑰一起生産公鑰檔案。
openssl req -new -key aminglinux.key -out aminglinux.csr
生成crt檔案
[root@AliKvn conf]# openssl x509 -req -days 365 -in aminglinux.csr -signkey aminglinux.key -out aminglinux.crt
[root@AliKvn conf]# ls aminglinux.*
aminglinux.crt aminglinux.csr aminglinux.key
這裡的aminglinux.crt為公鑰
6 有了.csr .crt檔案後,可以配置ssl了
1 配置ssl.conf
#vim /usr/local/nginx/conf/vhost/ssl.conf
server
{
listen 443;
server_name aming.com;
index index.html index.php;
root /data/wwwroot/aming.com;
ssl on;
ssl_certificate aminglinux.crt;
ssl_certificate_key aminglinux.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
}
ssl on;
開啟ssl
ssl_certificate aminglinux.crt
指定公鑰
ssl_certificate_key aminglinux.key;
指定私鑰
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
協定,一般三種都需要配置上。
2 檢查文法
[root@AliKvn vhost]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/vhost/ssl.conf:7
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
第七行報錯提示,unknown ssl,因為當初編譯沒有把ssl給編譯進去,是以需要重新編譯,加上--with-http_ssl_module
2.1 編譯操作:
[root@AliKvn nginx-1.8.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
[root@AliKvn nginx-1.8.0]# make && make install
2.2 -V檢查編譯參數
[root@AliKvn nginx-1.8.0]# /usr/local/nginx/sbin/nginx -V
2.3 再次檢查文法
[root@AliKvn nginx-1.8.0]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
3 重新開機nginx
[root@AliKvn nginx-1.8.0]# /etc/init.d/nginx restart
Restarting nginx (via systemctl): [ OK ]
4 檢查listen端口
80和443端口都已經被listen
5 建立目錄和測試檔案
[root@AliKvn aming.com]# mkdir /data/wwwroot/aming.com/
[root@AliKvn aming.com]# echo "ssl test" > /data/wwwroot/aming.com/index.html
[root@AliKvn aming.com]# ls
index.html
[root@AliKvn aming.com]# cat index.html
ssl test
6 在hosts寫入aming.com
[root@AliKvn aming.com]#vim /etc/hosts
127.0.0.1 aming.com
7 curl測試
證書被标記為不可信用,因為證書是自己建立的,不合法的。
8 在windows浏覽器通路(如果發現通路不到,可以檢查防火牆,iptables規則,或者建立開通443端口規則)
在windows編輯hosts檔案,添加aming.com進去
8.1 在浏覽器輸入https://aming.com 進行通路
8.2 因為這是自己的測試,是以站點的信任性是透明的,繼續前往通路,點選 1 進階 2 繼續前往
8.3 通路發現當初echo的頁面資訊,正常通路效果。