11月29日任務
12.17 Nginx負載均衡
12.18 ssl原理
12.19 生成ssl密鑰對
12.20 Nginx配置sslphp
一.Nginx負載均衡html
![](http://static.javashuo.com/static/loading.gif)
示例一:linux
- vim /usr/local/nginx/conf/vhost/load.conf // 寫入以下內容
upstream qq_comnginx
{ web
ip_hash; 算法
server 61.135.157.156:80; vim
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來指定多個web server
- yum install -y bind-utils 安裝該包
- 使用dig 查詢出域名解析了哪些ip
![](http://static.javashuo.com/static/loading.gif)
- Nginx不支持代理https,只支持代理http,新版本支持代理tcp
二.ssl原理
![](http://static.javashuo.com/static/loading.gif)
- https
- ssl通訊是加密的
- 瀏覽器發送一個https的請求給服務器;
- 服務器要有一套數字證書,能夠本身製做(後面的操做就是阿銘本身製做的證書),也能夠向組織申請,區別就是本身頒發的證書須要客戶端驗證經過,才能夠繼續訪問,而使用受信任的公司申請的證書則不會彈出>提示頁面,這套證書其實就是一對公鑰和私鑰;
- 服務器會把公鑰傳輸給客戶端;
- 客戶端(瀏覽器)收到公鑰後,會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串隨機數,並用收到的公鑰加密; 客戶端把加密後的隨機字符串傳輸給服務器; 服務器收到加密隨機字符串後,先用私鑰解密(公鑰加密,私鑰解密),獲取到這一串隨機數後,再用這串隨機字符串加密傳輸的數據(該加密爲對稱加密,所謂對稱加密,就是將數據和私鑰也就是這個隨機字符串>經過某種算法混合在一塊兒,這樣除非知道私鑰,不然沒法獲取數據內容);
- 服務器把加密後的數據傳輸給客戶端;
- 客戶端收到數據後,再用本身的私鑰也就是那個隨機字符串解密;
三.生產ssl密鑰對
![](http://static.javashuo.com/static/loading.gif)
示例一:
- yum install openssl 安裝包
- cd /usr/local/nginx/conf
- openssl genrsa -des3 -out tmp.key 2048//key ,生成rsa形式的密鑰,文件爲私鑰
![](http://static.javashuo.com/static/loading.gif)
- openssl rsa -in tmp.key -out aminglinux.key //轉換key,取消密碼
![](http://static.javashuo.com/static/loading.gif)
- rm -f tmp.key
- openssl req -new -key aminglinux.key -out aminglinux.csr//生成證書請求文件,須要拿這個請求文件和私鑰一塊兒生產公鑰文件
![](http://static.javashuo.com/static/loading.gif)
- openssl x509 -req -days 365 -in aminglinux.csr -signkey aminglinux.key -out aminglinux.crt 生成公鑰,用生成的請求文件和私鑰生成
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
四.Nginx配置ssl
![](http://static.javashuo.com/static/loading.gif)
示例一:
- 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; 協議,通常三種協議都配置上
}
- mkdir /data/wwwroot/aming.com 建立root定義的目錄
- -t && -s reload //若報錯unknown directive 「ssl」 ,須要從新編譯nginx,加上--with-http_ssl_module
![](http://static.javashuo.com/static/loading.gif)
- cd /usr/local/src/nginx-1.12.1/ 進去Nginx源碼包下
![](http://static.javashuo.com/static/loading.gif)
- ./configure --prefix=/usr/local/nginx --with-http_ssl_module 編譯該模塊
![](http://static.javashuo.com/static/loading.gif)
- make && make install
- 查看一下
![](http://static.javashuo.com/static/loading.gif)
- -t &&restare 檢測語法,而且重啓一下Nginx
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
- mkdir /data/wwwroot/aming.com 建立一個目錄
- echo 「ssl test page.」>/data/wwwroot/aming.com/index.html 建立一個測試文件
- vi /etc/hosts
- 編輯hosts,增長127.0.0.1 aming.com
![](http://static.javashuo.com/static/loading.gif)
- curl https://aming.com/ 測試一下
![](http://static.javashuo.com/static/loading.gif)