LNMP架構(Nginx負載均衡、ssl原理、生成ssl密鑰對、Nginx配置ssl)

Nginx負載均衡

網站的訪問量愈來愈大,服務器的服務模式也得進行相應的升級,好比分離出數據庫服務器、分離出圖片做爲單獨服務,這些是簡單的數據的負載均衡,將壓力分散到不一樣的機器上。有時候來自web前端的壓力,也能讓人十分頭痛。怎樣將同一個域名的訪問分散到兩臺或更多的機器上呢?這其實就是另外一種負載均衡了,nginx自身就能夠作到,只須要作個簡單的配置就行。
  nginx不單能夠做爲強大的web服務器,也能夠做爲一個反向代理服務器,並且nginx還能夠按照調度規則實現動態、靜態頁面的分離,能夠按照輪詢、ip哈希、URL哈希、權重等多種方式對後端服務器作負載均衡,同時還支持後端服務器的健康檢查。php

1.Nginx負載均衡一些基礎知識:html

  • nginx 的 upstream目前支持 4 種方式的分配
    • 輪詢(默認): 每一個請求按時間順序逐一分配到不一樣的後端服務器,若是後端服務器down掉,能自動剔除。
    • weight : 指定輪詢概率,weight和訪問比率成正比,用於後端服務器性能不均的狀況。
    • ip_hash : 每一個請求按訪問ip的hash結果分配,這樣每一個訪客固定訪問一個後端服務器,能夠解決session的問題。
    • fair(第三方): 按後端服務器的響應時間來分配請求,響應時間短的優先分配。
    • url_hash(第三方)

2.nginx負載均衡配置,主要是proxy_pass,upstream的使用前端

1)dig命令:是經常使用的域名查詢工具,能夠用來測試域名系統工做是否正常。linux

yum install -y bind-utils

執行效果:nginx

[root@yolks2 vhost]# dig qq.com

; <<>> DiG 9.9.4-RedHat-9.9.4-61.el7 <<>> qq.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 29871
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;qq.com.				IN	A

;; ANSWER SECTION:
qq.com.			177	IN	A	111.161.64.40
qq.com.			177	IN	A	111.161.64.48

;; Query time: 13 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: 六 8月 18 22:16:12 CST 2018
;; MSG SIZE  rcvd: 67

2)新編輯配置文件,目錄爲**/usr/local/nginx/conf/vhost/load.conf**,文件內容以下所示:web

upstream qq_com #此處名稱可自定義
{
    ip_hash; #保持訪問時始終在一臺機器上,而不會是1頁面一個ip,2頁面1個ip.
    server 61.135.157.156:80; #ip:sort,若是是域名是端口能夠省略掉
    server 125.39.240.113:80; #server能夠配置多個
}
server
{
    listen 80;#監聽端口
    server_name www.qq.com; #監聽域名
    location /
    {
        proxy_pass      http://qq_com; #此處須要填寫upstream名稱
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

ip_hash是容易理解的,可是由於僅僅能用ip這個因子來分配後端,所以ip_hash是有缺陷的,不能在一些狀況下使用:算法

  • **nginx不是最前端的服務器。**ip_hash要求nginx必定是最前端的服務器,不然nginx得不到正確ip,就不能根據ip做hash。譬如使用的是squid爲最前端,那麼nginx取ip時只能獲得squid的服務器ip地址,用這個地址來做分流是確定錯亂的。
  • **nginx的後端還有其它方式的負載均衡。**假如nginx後端又有其它負載均衡,將請求又經過另外的方式分流了,那麼某個客戶端的請求確定不能定位到同一臺session應用服務器上。這麼算起來,nginx後端只能直接指向應用服務器,或者再搭一個squid,而後指向應用服務器。最好的辦法是用location做一次分流,將須要session的部分請求經過ip_hash分流,剩下的走其它後端去。

3)先不從新加載配置curl騰訊主頁 www.qq.com, 提示訪問默認頁數據庫

[root@yolks2 vhost]# curl -x127.0.0.1:80 www.qq.com
This is a default site.

4)從新加載新配製的load.conf文件再看效果:反饋回了qq.com主頁的html頁面源碼vim

ssl原理

即https加密訪問後端

SSL工做原理

生成ssl密鑰對

在當前虛擬機模擬生成ssl密鑰對

1.進入/usr/local/nginx/conf目錄

cd /usr/local/nginx/conf

2.查詢openssl包

[root@yolks2 conf]# rpm -qf `which openssl `
openssl-1.0.2k-12.el7.x86_64

3.生成類型爲rsa格式的私鑰

關鍵代碼:

openssl genrsa -des3 -out tmp.key 2048 #key文件爲私鑰

實踐,確認輸入密碼123456,由於用戶每次訪問不可能都去輸入密碼,因此咱們下面轉換key去掉密碼;

[root@yolks2 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:

4.轉換key,取消密碼

openssl rsa -in tmp.key -out yolkslinux.key

輸入和tmp.key文件相同的密碼確認

[root@yolks2 conf]# openssl rsa -in tmp.key -out yolkslinux.key
Enter pass phrase for tmp.key:
writing RSA key

5.刪除tmp.key文件

rm -f tmp.key

6.生成證書請求文件,須要拿文件和和私鑰一塊兒生產公鑰文件

openssl req -new -key yolkslinux.key -out yolkslinux.csr

過程當中須要填寫一些信息,由於是本地模擬,咱們根據本身狀況填寫便可。

[root@yolks2 conf]# openssl req -new -key yolkslinux.key -out yolkslinux.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]:cn
State or Province Name (full name) []:BeiJing
Locality Name (eg, city) [Default City]:ChaoYang
Organization Name (eg, company) [Default Company Ltd]:Yolks
Organizational Unit Name (eg, section) []:yolks
Common Name (eg, your name or your server's hostname) []:yolks    
Email Address []:superyolks@vip.qq.com

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:yolks
An optional company name []:yolks

7.設置證書有效期等信息:1年有效期

crt文件爲公鑰,key文件爲私鑰

openssl x509 -req -days 365 -in yolkslinux.csr -signkey yolkslinux.key -out yolkslinux.crt

執行效果以下:

[root@yolks2 conf]# openssl x509 -req -days 365 -in yolkslinux.csr -signkey yolkslinux.key -out yolkslinux.crt
Signature ok
subject=/C=cn/ST=BeiJing/L=ChaoYang/O=Yolks/OU=yolks/CN=yolks/emailAddress=superyolks@vip.qq.com
Getting Private key

Nginx配置ssl

1.虛擬主機下建立新配置文件/usr/local/nginx/conf/vhost/ssl.conf

server
{
    listen 443;
    server_name aming.com;
    index index.html index.php;
    root /data/wwwroot/yolks.com;
    ssl on; #開啓ssl即支持https
    ssl_certificate yolkslinux.crt; #指定公鑰
    ssl_certificate_key yolkslinux.key; #指定私鑰
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #協議
}

2.建立/data/wwwroot/yolks.com目錄

mkdir /data/wwwroot/yolks.com

3.檢查測試配置文件

/usr/local/nginx/sbin/nginx -t

錯誤提示 :nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/vhost/ssl.conf:7 錯誤緣由:沒有安裝相對應的ssl配置 解決方法:從新編譯,添加ssl參數

1.添加編譯參數

./configure --prefix=/usr/local/nginx --with-http_ssl_module #添加對應模塊參數編譯

2.從新編譯安裝

make && make install

4.從新啓動Nginx,查看端口號是否有ssl.conf中配置的443端口

[root@yolks2 vhost]# netstat -lntp |grep 443
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      3824/nginx: master

5.在/data/wwwroot/yolks.com建立測試文件

[root@yolks2 yolks.com]# vim index.html 
[root@yolks2 yolks.com]# cat !$
cat index.html
this is the ssl test  page!

6.修改虛擬機/etc/hosts文件

[root@yolks2 yolks.com]# cat !$
cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
127.0.0.1 yolks.com #添加網址dns

7.curl測試

[root@yolks2 yolks.com]# curl -x127.0.1:443 https://yolks.com/
curl: (56) Received HTTP code 400 from proxy after CONNECT #

Windows上瀏覽器訪問,須要暫時iptables -F清空規則,再次嘗試

提示不安全,則點擊繼續訪問

由於是本身頒發的ssl證書,因此提示紅色不安全標識,例如12306網站本身頒發

ssl證書申請推薦:沃通、阿里雲(我的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

相關文章
相關標籤/搜索