nginx的負載均衡
- nginx的負載均衡就是把代理服務器指向多個ip,讓用戶可以通過代理服務器訪問到多個web服務器,當其中一個web服務器宕機時,不影響用戶訪問網站。
- 配置如下
[root@akuilinux01 vhost]# vim load.conf upstream qq_com { ip_hash; server 111.161.64.48:80; server 111.161.64.40: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來指定多個web server,proxy_pass http://qq_com;這裏對應upstream qq_com
- ip_hash保證同一個用戶始終保持在同一臺服務器
- nginx不支持代理https(端口爲443),新版本支持tcp
- dig命令是域名解析工具,安裝包bind-utils
ssl原理
- HTTPS協議是由SSL+HTTP協議構建的可進行加密傳輸、身份認證的網絡協議要比http協議安全
- ssl的流程
- 瀏覽器發送一個https的請求給服務器;
- 服務器要有一套數字證書,可以自己製作(後面的操作就是阿銘自己製作的證書),也可以向組織申請,區別就是自己頒發的證書需要客戶端驗證通過,纔可以繼續訪問,而使用受信任的公司申請的證書則不會彈出>提示頁面,這套證書其實就是一對公鑰和私鑰;
- 服務器會把公鑰傳輸給客戶端;
- 客戶端(瀏覽器)收到公鑰後,會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串隨機數,並用收到的公鑰加密;
- 客戶端把加密後的隨機字符串傳輸給服務器;
- 服務器收到加密隨機字符串後,先用私鑰解密(公鑰加密,私鑰解密),獲取到這一串隨機數後,再用這串隨機字符串加密傳輸的數據(該加密爲對稱加密,所謂對稱加密,就是將數據和私鑰也就是這個隨機字符串>通過某種算法混合在一起,這樣除非知道私鑰,否則無法獲取數據內容);
- 服務器把加密後的數據傳輸給客戶端;
- 客戶端收到數據後,再用自己的私鑰也就是那個隨機字符串解密
生成ssl密鑰對
- yum install -y openssl安裝ssl工具
- 放在這個文件下/usr/local/nginx/conf
- openssl genrsa -des3 -out tmp.key 2048 //生成私鑰,key文件爲私鑰
- openssl rsa -in tmp.key -out aminglinux.key //轉換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爲公鑰文件Nginx配置SSL
- 配置文件
[root@akuilinux01 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 ssl_certificate aminglinux.crt; #配置公鑰 ssl_certificate_key aminglinux.key; #配置私鑰 ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #配置協議 } [root@akuilinux01 conf]# mkdir /data/wwwroot/aming.com
- 報錯
[root@akuilinux01 conf]# /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
- 未識別ssl配置,需要重新編譯Nginx
[root@akuilinux01 ~]# cd /usr/local/src/nginx-1.14.0 [root@akuilinux01 ~]# ./configure --prefix=/usr/local/nginx --with-http_ssl_modul [root@akuilinux01 ~]# make [root@akuilinux01 ~]# make install [root@akuilinux01 nginx-1.14.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 [root@akuilinux01 ~]# /etc/init.d/nginx restart Restarting nginx (via systemctl): [ 確定 ] [root@akuilinux01 ~]# 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:80 0.0.0.0:* LISTEN 5054/nginx: master tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 849/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1221/master tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 5054/nginx: master tcp6 0 0 :::22 :::* LISTEN 849/sshd tcp6 0 0 ::1:25 :::* LISTEN 1221/master tcp6 0 0 :::3306 :::* LISTEN 1179/mysqld nginx監聽了443端口,表示配置生效了
- 未識別ssl配置,需要重新編譯Nginx
- 測試
[root@akuilinux01 ~]# cd /data/wwwroot/aming.com/ [root@akuilinux01 aming.com]# vim index.html this is ssl. [root@akuilinux01 aming.com]# vim /etc/hosts 127.0.0.1 aming.com [root@akuilinux01 aming.com]# curl https://aming.com curl: (60) Peer's certificate issuer has been marked as not trusted by the user. More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
由於不是正式的證書,所以提示不信任
- 也可以更改Windows的hosts文件,使用瀏覽器測試
# 擴展
- [針對請求的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)