15.Nginx負載均衡&SSL密鑰對&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
Nginx負載均衡
負載均衡在服務端開發中算是一個比較重要的特性。因為Nginx除了作為常規的Web服務器外,還會被大規模的用於反向代理前端,因為Nginx的異步框架可以處理很大的並發請求,把這些並發請求hold住之後就可以分發給後臺服務端(backend servers,也叫做服務池, 後面簡稱backend)來做復雜的計算、處理和響應,這種模式的好處是相當多的:隱藏業務主機更安全,節約了公網IP地址,並且在業務量增加的時候可以方便地擴容後臺服務器。
負載均衡可以分為硬件負載均衡和軟件負載均衡,前者一般是專用的軟件和硬件相結合的設備,設備商會提供完整成熟的解決方案,通常也會更加昂貴。軟件的復雜均衡以Nginx占據絕大多數.
1.修改虛擬主機配置文件(以baidu.com為例)
[root@xavi vhost]# yum install -y bind-utils [root@xavi vhost]# dig baidu.com ; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7_4.2 <<>> baidu.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24796 ;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;baidu.com. IN A ;; ANSWER SECTION: baidu.com. 174 IN A 111.13.101.208 baidu.com. 174 IN A 220.181.57.216 ;; Query time: 41 msec ;; SERVER: 119.29.29.29#53(119.29.29.29) ;; WHEN: 日 3月 18 17:28:34 CST 2018 ;; MSG SIZE rcvd: 70
看到有兩個IP,可以看到兩個IP,有兩個IP就可以走負載均衡了
2.編輯配置文件
[root@xavi vhost]# pwd /usr/local/nginx/conf/vhost [root@xavi vhost]# vim load.conf uptream baidu_com { ip_hash; server 111.13.101.208:80; server 220.181.57.216:80; } server { listen 80; server_name www.baidu.com; location / { proxy_pass http://baidu_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 baidu_com
#名字自定義
{
ip_hash;
# 目的:同一個用戶保持在同一個服務器上
# 即當域名指向多個IP時,保證每個用戶始終解析到同一IP
server 111.13.101.208:80;
server 220.181.57.216:80;
# 指定web服務器的IP
}
3.測試
由於指向dig qq.com時候也是有問題,只能獲得一個IP,百度有兩個IP打算拒絕訪問.
[root@xavi vhost]# curl -x127.0.0.1 baidu.com -I
curl: (7) Failed connect to 127.0.0.1:1080; 拒絕連接
二、ssl原理
SSL工作流程
瀏覽器發送一個https的請求給服務器;
服務器要有一套數字證書,可以自己制作(後面的操作就是阿銘自己制作的證書),也可以向組織申請,區別就是自己頒發的證書需要客戶端驗證通過,才可以繼續訪問,而使用受信任的公司申請的證書則不會彈出>提示頁面,這套證書其實就是一對公鑰和私鑰;
服務器會把公鑰傳輸給客戶端;
客戶端(瀏覽器)收到公鑰後,會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串隨機數,並用收到的公鑰加密;
客戶端把加密後的隨機字符串傳輸給服務器;
服務器收到加密隨機字符串後,先用私鑰解密(公鑰加密,私鑰解密),獲取到這一串隨機數後,再用這串隨機字符串加密傳輸的數據(該加密為對稱加密,所謂對稱加密,就是將數據和私鑰也就是這個隨機字符串>通過某種算法混合在一起,這樣除非知道私鑰,否則無法獲取數據內容);
服務器把加密後的數據傳輸給客戶端;
客戶端收到數據後,再用自己的私鑰也就是那個隨機字符串解密;
三、生成ssl密鑰對
1.生成key即“私鑰”:openssl genrsa
[root@xavi conf]# openssl genrsa -des3 -out tmp.key 2048
Generating RSA private key, 2048 bit long modulus
...+++
...................+++
e is 65537 (0x10001)
Enter pass phrase for tmp.key:
Verifying - Enter pass phrase for tmp.key:
//這一步操作是生成key即“私鑰”,2048為加密字符長度,會讓我們輸入密碼,不能太短,否者不成功。
2.把上一步生成的tmp.key在轉換成xavilinux.key
[root@xavi conf]# openssl rsa -in tmp.key -out xavilinux.key
Enter pass phrase for tmp.key:
writing RSA key
把tmp.key轉化成zlinux.key,目的是刪除剛才設置的密碼,如果不清除密碼,後續nginx加載它的時候要輸入它的密碼,很不方便.
3.生成證書請求文件
生成證書請求文件,key文件和csr文件生成最終的公鑰文件。Common Name為後面配置Nginx配置文件server_name
[root@xavi conf]# rm -f tmp.key
[root@xavi conf]# openssl req -new -key xavilinux.key -out xavilinux.csr
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]:86
State or Province Name (full name) []:jiangsu
Locality Name (eg, city) [Default City]:suzhou
Organization Name (eg, company) [Default Company Ltd]:dsfss
Organizational Unit Name (eg, section) []:dsf
Common Name (eg, your name or your server‘s hostname) []:xavi.com
Email Address []:[email protected]
Please enter the following ‘extra‘ attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:dsf
4.最終生成CRT證書文件
[root@xavi conf]# openssl x509 -req -days 365 -in xavilinux.csr -signkey xavilinux.key -out xavilinux.crt
Signature ok
subject=/C=86/ST=jiangsu/L=suzhou/O=dsfss/OU=dsf/CN=xavi.com/[email protected]
Getting Private key
以上操作最終目的是生成xavilinux.key和xavilinux.crt兩個文件,其實購買SSL證書主要得到這兩個文件,有了這兩個文件就可以配置nginx了
[root@xavi conf]# ls |grep xavilinux
xavilinux.crt
xavilinux.csr
xavilinux.key
四、Nginx配置ssl
1.編輯配置文件
[root@xavi ~]# cd /usr/local/nginx/conf/vhost
[root@xavi vhost]# vim /usr/local/nginx/conf/vhost/ssl.conf
server
{
listen 443;
server_name xavi.com;
index index.html index.php;
root /data/wwwroot/xavi.com;
ssl on;
ssl_certificate xavilinux.crt;
ssl_certificate_key xavilinux.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
}
2.檢查配置文件有無問題,結果說明當前的Nginx不支持SSL
[root@xavi 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
這說明當前Nginx並不支持SSL,因為之前Nginx編譯時並沒有配置支持SSL的參數.
3.重新編譯一次,加上SSL參數:
[root@xavi vhost]# /usr/local/nginx/sbin/nginx -V //了解版本號
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC)
configure arguments: --prefix=/usr/local/nginx
[root@xavi vhost]# cd /usr/local/src/nginx-1.12.1
確定版本號,找到配置文件中那部分是支持ssl服務的
[root@xavi nginx-1.12.1]# ./configure --help |grep -i ssl
--with-http_ssl_module enable ngx_http_ssl_module
--with-mail_ssl_module enable ngx_mail_ssl_module
--with-stream_ssl_module enable ngx_stream_ssl_module
--with-stream_ssl_preread_module enable ngx_stream_ssl_preread_module
--with-openssl=DIR set path to OpenSSL library sources
--with-openssl-opt=OPTIONS set additional build options for OpenSSL
4.增加支持SSL的參數./configure --prefix=/usr/local/nginx --with-http_ssl_module
[root@xavi nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
[root@xavi nginx-1.12.1]# make
[root@xavi nginx-1.12.1]# make install
5. 編譯完成,再來檢驗一次,啟動nginx服務
[root@xavi nginx-1.12.1]# /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@xavi nginx-1.12.1]# /etc/init.d/nginx start
Starting nginx (via systemctl): [ 確定 ]
查看443端口
[root@xavi nginx-1.12.1]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6156/nginx: master
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1779/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1047/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1044/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1589/master
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 6156/nginx: master
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::22 :::* LISTEN 1047/sshd
tcp6 0 0 ::1:631 :::* LISTEN 1044/cupsd
tcp6 0 0 ::1:25
6.沒有問題,然後創建對應的目錄和測試文件:
[root@xavi nginx-1.12.1]# mkdir /data/wwwroot/xavi.com
mkdir: 無法創建目錄"/data/wwwroot/xavi.com": 文件已存在
[root@xavi nginx-1.12.1]# cd /data/wwwroot/xavi.com
[root@xavi xavi.com]# vi index.html
7.curl https://xavi.com報錯,編輯/etc/hosts文件,加上xavi.com
再編輯hosts文件,寫入一行(hosts路徑為C:\Windows\System32\drivers\etc),用瀏覽器查看
查看防火墻:iptables -nvL
關閉防火墻: iptables -F
高級網站:12306
15.Nginx負載均衡&SSL密鑰對&Nginx配置SSL