天天看點

12.17 Nginx負載均衡;12.18 ssl原理;12.19 生産ssl密鑰對;12.20 Nginx配置ssl

擴充:

針對請求的uri來代理 http://ask.apelearn.com/question/1049

根據通路的目錄來區分後端web http://ask.apelearn.com/question/920

12.17 Nginx負載均衡

1. 安裝dig指令:

[root@hao-01 ~]# yum install -y bind-utils

2. 用dig擷取qq.com的ip位址:

[root@hao-01 ~]# dig qq.com

3. 建立ld.conf檔案,并添加如下内容:

[root@hao-01 ~]# vi /usr/local/nginx/conf/vhost/ld.conf

添加内容(upstream指定多個web server ip):

upstream qq

{

    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;

        proxy_set_header Host   $host;

        proxy_set_header X-Real-IP      $remote_addr;

        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;   

    }

4. 檢測nginx配置檔案是否有錯?

[root@hao-01 ~]# /usr/local/nginx/sbin/nginx -t

5. 重新加載nginx配置檔案(非重新開機!):

[root@hao-01 ~]# /usr/local/nginx/sbin/nginx -s reload

6. curl 通過本地ip127.0.0.1通路www.qq.com,通路到了qq網站首頁:

[root@hao-01 ~]# curl -x127.0.0.1:80 www.qq.com

12.18 ssl原理

12.19 生産ssl密鑰對

1. 進入...conf目錄下:

[root@hao-01 ~]# cd /usr/local/nginx/conf

2. 安裝 openssl指令:

[root@hao-01 ~]# yum install -y which openssl

3. 目前目錄下生成tmp.key私鑰:

[root@hao-01 conf]# openssl genrsa -des3 -out tmp.key 2048

4. 轉換tmp.key私鑰并取消密碼,生成一個新的檔案haosy.key私鑰:

[root@hao-01 conf]# openssl rsa -in tmp.key -out haosy.key

5. 删除 tmp.key私鑰:

[root@hao-01 conf]# rm -f tmp.key

6. 生成證書請求檔案hao.csr:

[root@hao-01 conf]# openssl req -new -key haosy.key -out hao.csr

7. 生成公鑰,hao.csr證書請求檔案和haosy.key私鑰一起生産出haogy.crt公鑰:

[root@hao-01 conf]# openssl x509 -req -days 365 -in hao.csr -signkey haosy.key -out haogy.crt

12.20 Nginx配置ssl

1. 建立 .../hao1.com網站目錄:

[root@hao-01 ~]# mkdir /data/wwwroot/hao1.com

2. 建立ssl.conf檔案,并添加如下内容:

[root@hao-01 ~]# vim /usr/local/nginx/conf/vhost/ssl.conf

添加内容:

    listen 443;

    server_name hao1.com;

    index index.html index.php;

    root /data/wwwroot/hao1.com;

    ssl on;

    ssl_certificate haogy.crt;

    ssl_certificate_key haosy.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

3. 進入nginx-1.12.1源碼包目錄下:

[root@hao-01 ~]# cd /usr/local/src/nginx-1.12.1/

4. 編譯with-http_ssl_module :

[root@hao-01 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module

5. make :

[root@hao-01 nginx-1.12.1]# make

6. make install :

[root@hao-01 nginx-1.12.1]# make install

7. 檢測nginx配置檔案是否有錯?

8. 重新開機nginx服務:

[root@hao-01 nginx-1.12.1]# /etc/init.d/nginx restart

9. 檢視監聽端口:

[root@hao-01 nginx-1.12.1]# netstat -lntp

10. 進入 .../hao1.com網站目錄下:

[root@hao-01 nginx-1.12.1]# cd /data/wwwroot/hao1.com/

11. 在 .../hao1.com網站目錄下,建立index.html測試檔案并添加内容:

[root@hao-01 hao1.com]# vim index.html

This is ssl.

12. 在host檔案中,另起一行,添加本地ip 跟指定的域名:

[root@hao-01 hao1.com]# vim /etc/hosts

127.0.0.1 hao1.com

13. curl 通路hao1.com設定的域名網站:

[root@hao-01 hao1.com]# curl https://hao1.com

14. 打開windows系統找到hosts檔案:

路徑:C:\Windows\System32\drivers\etc

15. 編輯windows下的hosts檔案:

添加一條内容:

192.168.211.128 hao1.com

16. 臨時關閉防火牆(不想全部清空,把443端口添加一個規則)

[root@hao-01 hao1.com]# iptables -F

17. windows下的遊覽器通路https://hao1.com:

本文轉自 主内安詳 51CTO部落格,原文連結:http://blog.51cto.com/zhuneianxiang/1956411,如需轉載請自行聯系原作者

繼續閱讀