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.comwKioL1mSi-bBEjX7AABZgFx33DE213.png

spacer.gif3. 创建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;   

    }
}

wKioL1mSjAKCkEIoAABGEI66EQ0558.png

spacer.gif4. 检测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原理

wKioL1mSjCLie1xqAAaVHlLlxKQ563.png

spacer.gif

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

wKiom1mSjD_ixurBAAAjxDeXw1g394.png

spacer.gif4. 转换tmp.key私钥取消密码生成一个新的文件haosy.key私钥:

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

wKiom1mSjFnS3zOyAAAX-ExObDI159.png

spacer.gif5. 删除 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

wKiom1mSjHWi_2zkAACK_b1G9BM604.png

spacer.gif7. 生成公钥,hao.csr证书请求文件和haosy.key私钥一起生产出haogy.crt公钥:

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

wKiom1mSjI-xquPZAAAnRmHoVas417.png

spacer.gif12.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

添加内容:

server
{
    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;
}

wKioL1mSjKuAXaGhAAAkjAxL6CI674.png

spacer.gif3. 进入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配置文件是否有错?

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

8. 重启nginx服务:

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

9. 查看监听端口:

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

wKioL1mSjM6S_QrAAABFW4BYhNk507.png

spacer.gif10. 进入 .../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

wKiom1mSjOigqvCtAABmCrHOLTM703.png

spacer.gif14. 打开windows系统找到hosts文件

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

wKiom1mSjQLTi5gXAAAY7XXpDSs338.png

spacer.gif15. 编辑windows下的hosts文件:

添加一条内容:

192.168.211.128 hao1.com

16. 临时关闭防火墙(不想全部清空,把443端口添加一个规则)

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

17. windows下的游览器访问https://hao1.com

wKioL1mSjR6ScVl_AAAr-oY_-3s769.png










本文转自 主内安详 51CTO博客,原文链接:http://blog.51cto.com/zhuneianxiang/1956411,如需转载请自行联系原作者
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值