[toc]php
12.17 Nginx負載均衡
- 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;//寫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;
}
}
- upstream來指定多個web server
- dig 能夠查看域名解析的是哪一個IP
- yum install -y
- ngnix不支持代理https,server後邊不能寫443端口
12.18 ssl原理
- 瀏覽器發送一個https的請求給服務器;
- 服務器要有一套數字證書,能夠本身製做(後面的操做就是阿銘本身製做的證書),也能夠向組織申請,區別就是本身頒發的證書須要客戶端驗證經過,才能夠繼續訪問,而使用受信任的公司申請的證書則不會彈出>提示頁面,這套證書其實就是一對公鑰和私鑰;
- 服務器會把公鑰傳輸給客戶端;
- 客戶端(瀏覽器)收到公鑰後,會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串隨機數,並用收到的公鑰加密;
- 客戶端把加密後的隨機字符串傳輸給服務器;
- 服務器收到加密隨機字符串後,先用私鑰解密(公鑰加密,私鑰解密),獲取到這一串隨機數後,再用這串隨機字符串加密傳輸的數據(該加密爲對稱加密,所謂對稱加密,就是將數據和私鑰也就是這個隨機字符串>經過某種算法混合在一塊兒,這樣除非知道私鑰,不然沒法獲取數據內容);
- 服務器把加密後的數據傳輸給客戶端;
- 客戶端收到數據後,再用本身的私鑰也就是那個隨機字符串解密;
- SSL工做流程

12.19 生成ssl密鑰對
- cd /usr/local/ngnix/conf/
- rpm -qt 'which openssl' //查看命令由哪一個包安裝的
- 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爲公鑰
12.20 Nginx配置ssl
- 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;
}
- -t && -s reload //若報錯unknown directive 「ssl」,須要從新編譯nginx,加上--with-http_ssl_module
- mkdir /data/wwwroot/aming.com
- echo 「ssl test page.」>/data/wwwroot/aming.com/index.html
- 編輯hosts,增長127.0.0.1 aming.com
- curl https://aming.com/
擴展
- 針對請求的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