码迷,mamicode.com
首页 > 其他好文 > 详细

2018.3.16 12周5次课

时间:2018-03-16 17:15:47      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:Linux学习

十二周五次课(3月16日)

12.17 Nginx负载均衡

12.18 ssl原理

12.19 生成ssl密钥对

12.20 Nginx配置ssl

12.17 Nginx负载均衡

后端web服务器可以有多台,就可以实现负载均衡

upstream来指定多个web server

查看解析域名的ip命令:dig

安装dig命令:yum install -y bind-utils

vim /usr/local/nginx/conf/vhost/load.conf //写入如下内容

upstream qq_com // 在这里定义后端的web server ,通过upstream来指定多个web服务器,upstream后面的名字qq_com可以更改,如可以更改为qq

{

   ip_hash; //它表示根据ip地址把请求分到不同的服务器上。目的是同一个用户始终保持在同一个机器

   server 61.135.157.156:80; //也可以写成http:// 220.181.57.216:80; ,对应的server块内的proxy_pass内直接写qq_com即可,不需要写https://

   server 125.39.240.113:80;

}

server

{

   listen 80;

   server_name www.qq.com;

   location /

   {

       proxy_pass      http://qq_com; //这里使用的是upstream名即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;

   }

}

测试:

curl -x127.0.0.1:80 www.qq.com //通常情况下会访问我们的默认虚拟主机

技术分享图片

-t && -s reload

再次执行,就会变成了qq.com的主页,显示的是网页的源码

说明:nginx不支持代理https,即server语句内的端口无法使用443。

12.18 ssl原理

目前chrome、Firefox各大浏览器 也开始针对HTTPS做调整。你有没有发现,使用百度搜问题时,百度的网址前面是HTTPS ,包括阿铭的猿课官网(ask.apelearn.com)同样也支持了HTTPS访问

这说明HTTPS已经成为一种趋势,相信过不了10年,整个互联网都会强制使用HTTPS通信,而废弃目前主流的HTTP。那到底什么是HTTPS呢?简单讲,它就是一种加密的HTTP协议,如果HTTP通信的数据包在传输过程中被截获,我们可以破译这些数据包里面的信息,这里面不乏一些用户名、密码、手机号等敏感的信息。 而如果使用HTTPS通信,即使数据包被截获,我们也无法破译里面的内容图

技术分享图片

技术分享图片

12.19 生成ssl密钥对

cd /usr/local/nginx/conf

查询某个命名由哪个包安装的:rpm -qf  `which 命令` (命令不能有别名)

需要用到openssl命令:rpm -qf `which openssl`

技术分享图片

openssl genrsa -des3 -out tmp.key 2048 //生成rsa形式的私钥文件,文件名tmp.key,长度2048

技术分享图片

openssl rsa -in tmp.key -out aminglinux.key //转换key,取消密码。-in:指定哪个密钥要转换;-out:指定输出的

技术分享图片

tmp.key和aminglinux.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为公钥,

技术分享图片

crt是私钥,key是私钥,csr是证书请求文件

技术分享图片

12.20 Nginx配置ssl

vim /usr/local/nginx/conf/vhost/ssl.conf //加入如下内容

server

{

   listen 443; //监听443端口

   server_name aming.com;

   index index.html index.php;

   root /data/wwwroot/aming.com;

   ssl on; //开启ssl,支持https

   ssl_certificate aminglinux.crt; //指定公钥文件

   ssl_certificate_key aminglinux.key; //指定私钥文件

   ssl_protocols TLSv1 TLSv1.1 TLSv1.2; //指定ssl的协议

}

-t && -s reload //若报错unknown directive “ssl” ,需要重新编译nginx,加上--with-http_ssl_module

技术分享图片

在前面编译nginx时,没有指定支持ssl

命令查看:/usr/local/nginx/sbin/nginx -V

技术分享图片

configure arguments: --prefix=/usr/local/nginx

重新编译nginx

cd /usr/local/src/ nginx-1.13.9

./configure --help |grep -i ssl //用help查看要加载哪个ssl模块

技术分享图片

要加--with-http_ssl_module

编译:

./configure --prefix=/usr/local/nginx --with-http_ssl_module

make && make install

技术分享图片

-t && -s reload //没有报错

技术分享图片

重启:/etc/init.d/nginx restart

技术分享图片

查看监听端口:netstat –lntp //多了443端口

技术分享图片

echo “ssl test page.”>/data/wwwroot/aming.com/index.html

测试:

不能用curl -x127.0.0.1:443 https://aming.com会报400错误

技术分享图片

编辑host:vi /etc/hosts

增加一行192.168.37.101  aming.com

curl https://aming.com

技术分享图片

curl: (60) Peer's certificate issuer has been marked as not trusted by the user.

此种情况多发生在自签名的证书,报错含义是签发证书机构未经认证,无法识别。解决办法是将签发该证书的私有CA公钥cacert.pem文件内容,追加到/etc/pki/tls/certs/ca-bundle.crt。

用主机网页测试:https://aming.com,如访问不到,就要看看虚拟机有没有设置防火墙

Iptables -nvL //查看防火

Iptables -F    //清空规则,如果不想清空就加一个443端口的规则

firewall-cmd --add-port=443/tcp

firewall-cmd --list-port

firewall-cmd --query-port=443/tcp

技术分享图片


2018.3.16 12周5次课

标签:Linux学习

原文地址:http://blog.51cto.com/415326/2087671

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!