Linux基础(day52)

时间:2022-04-27
本文章向大家介绍Linux基础(day52),主要内容包括12.17 Nginx负载均衡、Nginx负载均衡、12.18 ssl原理、12.19 生成ssl密钥对、生成ssl密钥对、12.20 Nginx配置ssl、Nginx配置ssl、扩展、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

12.17 Nginx负载均衡

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;
        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

Nginx负载均衡

  • 代理一台机器称为代理 ,代理两台机器就可以称为负载均衡
  • 代理服务器后面可以有多个web服务器,多个web服务器去提供服务的时候,就可以实现一个负载均衡的功能
  • 正常情况下,用户访问web服务器,是一台一台去请求;要么就是指定一个IP,把这域名解析到多台服务器上
  • 案例
    • 用户1 –> web1服务器
    • 用户2 –> web2服务器
      • 假设这时web1服务器挂掉了(宕机),用户1因为解析到了web1,但web1宕机了,所以就无法正常访问
      • 这时候若是用nginx的负载均衡,在web1宕机后,代理服务器就不会把请求发送给web1,这就是代理的一个优点,负载均衡的优点
  1. 配置负载均衡,负载均衡的配置借助了upstream 模块
  2. 这里将qq.com作为演示对象
  • dig命令查看解析的IP——>yum install -y bind-utils
[root@hanfeng ~]# yum install -y bind-utils


[root@hanfeng ~]# dig qq.com

; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7_4.1 <<>> qq.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 24485
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;qq.com.				IN	A

;; ANSWER SECTION:
qq.com.			273	IN	A	125.39.240.113
qq.com.			273	IN	A	61.135.157.156

;; Query time: 18 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: 一 1月 08 22:46:59 CST 2018
;; MSG SIZE  rcvd: 67

[root@hanfeng ~]# 
  1. 会看到返回出两个IP,这个就是域名解析,也就是qq.com解析到了两个IP上
  2. 这时候就可以用这两个125.39.240.113IP和61.135.157.156IP,去 做负载均衡
  3. 写一个配置文件vim /usr/local/nginx/conf/vhost/load.conf
[root@hanfeng ~]# vim /usr/local/nginx/conf/vhost/load.conf

写入以下内容
upstream qq_com        //upstream后的名称自定义
{
    ip_hash;        //目的是为了让同一个用户始终保持在同一个机器上
    server 61.135.157.156:80;    //如果域名解析端口是80,这段配置上的指定端口80是可以省略的
    server 125.39.240.113:80;
}
server
{
    listen 80;    //定义监听端口
    server_name www.qq.com;     //域名

    location /
    {
        proxy_pass      http://qq_com;         //这里填写的是upstream 的名字
即“http://upstream”,因为作为一个模块,代理访问的是通过解析后的IP访问;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}
保存退出
  1. upstream来指定多个web server
  • 当有多个服务器同时对一个域名提供服务的时候,长时间访问一个域名,在一定的时效内,会出现需要重新登录或者是说跳转到另外一个地址的服务器上;ip_hash,就是使通过这个代理访问的同一个域名的多个IP的服务器是,始终保持在一个IP上对这个域名进行访问
  1. 在未加载配置的时候,本机去访问qq.com,回去访问默认虚拟主机
[root@hanfeng ~]# curl -x127.0.0.1:80 www.qq.com
This is the default site.
[root@hanfeng ~]# 
  1. 测试访问qq.com
  2. 检查配置文件语法,并重新加载
[root@hanfeng ~]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@hanfeng ~]# /usr/local/nginx/sbin/nginx -s reload
[root@hanfeng ~]# 
  1. 这时再来访问qq.com,会看到的是qq.com的主页,反馈回来的是网页的源码
[root@hanfeng ~]# curl -x127.0.0.1:80 www.qq.com
  1. 这个就是负载均衡

nginx代理和负载均衡的知识点

  • nginx不支持去代理https,也就是在配置文件中的server 后不能写443,是不支持的,只能代理http、tcp
  • 若想要实现代理https,nginx监听443端口,但web服务必须是80端口

12.18 ssl原理

ssl原理

  • https的相关知识点
  • 要配置nginx和https,就需要首先去了解https是什么?
  • 在访问一些网站的时候,会自动加上了https前标
  • http和https的区别
    • https通信是加密的,如果不加密,中间传输数据包的有时候会被截到,就会导致信息泄露,https就是对这个通信的数据包进行加密
  • SSL工作流程
    • 浏览器发送一个https的请求给服务器;
    • 服务器要有一套数字证书,可以自己制作(后面的操作就是阿铭自己制作的证书),也可以向组织申请,区别就是自己颁发的证书需要客户端验证通过,才可以继续访问,而使用受信任的公司申请的证书则不会弹出>提示页面,这套证书其实就是一对公钥(加密)和私钥(解密);
    • 服务器会把公钥传输给客户端;
    • 客户端(浏览器)收到公钥后,(这个过程是浏览器判断的)会验证其是否合法有效,无效会有警告提醒,有效则会生成一串随机数,并用收到的公钥加密;
    • 客户端把加密后的随机字符串传输给服务器;
    • 服务器收到加密随机字符串后,先用私钥解密(公钥加密,私钥解密),获取到这一串随机数后,再用这串随机字符串加密传输的数据(该加密为对称加密,所谓对称加密,就是将数据和私钥也就是这个随机字符串>通过某种算法混合在一起,这样除非知道私钥,否则无法获取数据内容);
    • 服务器把加密后的数据传输给客户端;
    • 客户端收到数据后,再用自己的私钥也就是那个随机字符串解密;

12.19 生成ssl密钥对

生成ssl密钥对目录概要

  • cd /usr/local/nginx/conf
  • 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为公钥

生成ssl密钥对

在自己的虚拟机生成ssl 需要用到openssl工具

  • 在虚拟上颁发一套证书,生成ssl
  1. 首先得有一个openssl工具
  2. 切换到/usr/local/nginx/conf/目录下
[root@hf-01 ~]# cd /usr/local/nginx/conf/
[root@hf-01 conf]# 
  1. 若是没有openssl工具,可以安装下
  2. 查看openssl工具是由哪个安装包安装的
[root@hf-01 conf]# rpm -qf `which openssl`
openssl-1.0.2k-8.el7.x86_64
[root@hf-01 conf]# 
  1. 生成一个私钥,命令openssl genrsa -des3 -out tmp.key 2048
[root@hf-01 conf]# openssl genrsa -des3 -out tmp.key 2048
Generating RSA private key, 2048 bit long modulus
.......+++
......................................................................+++
e is 65537 (0x10001)
Enter pass phrase for tmp.key:        //输入密码
Verifying - Enter pass phrase for tmp.key:        //再次输入密码
[root@hf-01 conf]# 
  • openssl genrsa -des3 -out tmp.key 2048
    • genrsa ,表示生成rsa的私钥
    • 2048 ,2048长度
    • 名字为 tmp.key
  • 生成这个秘钥必须要有密码
  1. 在生成这个秘钥后比较麻烦,在nginx的配置文件里指定密码,每次访问浏览器,在https这个网址输入这个密码会很不方便,所以还需要去除这个密码
  2. 转换key,取消密码,命令 openssl rsa -in tmp.key -out gurui.key
  • -in 表示指定哪一个秘钥要被转换
  • -out 表示指定输出的
[root@hf-01 conf]# openssl rsa -in tmp.key -out gurui.key
Enter pass phrase for tmp.key:    //输入tmp.key的密码
writing RSA key
[root@hf-01 conf]# 
  1. 这时候tmp.key和gurui.key是属于同一个
  • tmp.key,有密码
  • gurui.key,没有密码
  1. 删除tmp.key
[root@hf-01 conf]# rm -f tmp.key
[root@hf-01 conf]# 
  1. 生成证书请求文件,需要拿这个请求文件和私钥一起生产公钥文件
[root@hf-01 conf]# openssl req -new -key gurui.key -out gurui.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn              //国家,2个字母
State or Province Name (full name) []:JiangSu      //省或州
Locality Name (eg, city) [Default City]:YanCheng   //城市
Organization Name (eg, company) [Default Company Ltd]:han  //公司
Organizational Unit Name (eg, section) []:han   //组织
Common Name (eg, your name or your server’s hostname) []:hanfeng  //您的主机名
Email Address []:han1118feng@163.com   //邮箱
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []:hanfeng  //设置密码
An optional company name []:  //一个可选的公司名称
用请求证书文件和私钥文件,生成一个公钥
[root@hf-01 conf]# 
  • 这里的信息可以不用填写,直接回车也行
  1. 因为这是自己给自己颁发的证书,可以随意填写,若是购买那些正式的证书,那证书的信息就需要填写相对应的信息
  2. 生成公钥,命令openssl x509 -req -days 365 -in gurui.csr -signkey gurui.key -out gurui.crt
[root@hf-01 conf]# openssl x509 -req -days 365 -in gurui.csr -signkey gurui.key -out gurui.crt
Signature ok
subject=/C=11/ST=BEIJING/L=BeiJing/O=hanfeng/OU=hanfeng/CN=hanfeng/emailAddress=han1118femx08
Getting Private key
[root@hf-01 conf]# 
  • -days 365 证书的日期是一年
  1. gui.crt是公钥,gurui.key是私钥

12.20 Nginx配置ssl

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/

Nginx配置ssl

  • 在有了公钥和私钥之后,配置nginx
  1. 生成新的配置文件 vim /usr/local/nginx/conf/vhost/ssl.conf
[root@hf-01 conf]# 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;   //root 目录
    ssl on;                                            //开启ssl
    ssl_certificate gurui.crt;      //指定公钥
    ssl_certificate_key gurui.key;   //指定私钥
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   //ssl 的协议
}
保存退出
  • ssl 的协议,一般情况下,三种协议都配置上
  1. 创建/data/wwwroot/aming.com目录
[root@hf-01 conf]# mkdir /data/wwwroot/aming.com
[root@hf-01 conf]# 
  1. 检查配置文件语法
[root@hf-01 conf]# /usr/local/nginx/sbin/nginx -t
nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/vhost/ssl.conf:7
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@hf-01 conf]# 
  • 报错:
    • 因为不知道这个 ssl 配置,在编译nginx的时候,并没有指定支持ssl
[root@hf-01 conf]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
configure arguments: --prefix=/usr/local/nginx
[root@hf-01 conf]# 
  • 解决办法
    • 重新编译nginx
  1. 重新编译nginx
[root@hf-01 conf]# cd /usr/local/src/nginx-1.12.1/
[root@hf-01 nginx-1.12.1]# ./configure --help |grep -i ssl
  --with-http_ssl_module             enable ngx_http_ssl_module
  --with-mail_ssl_module             enable ngx_mail_ssl_module
  --with-stream_ssl_module           enable ngx_stream_ssl_module
  --with-stream_ssl_preread_module   enable ngx_stream_ssl_preread_module
  --with-openssl=DIR                 set path to OpenSSL library sources
  --with-openssl-opt=OPTIONS         set additional build options for OpenSSL
[root@hf-01 nginx-1.12.1]# 
  • 编译的时候需要加上--with-http_ssl_module
  1. 初始化./configure --prefix=/usr/local/nginx --with-http_ssl_module
[root@hf-01 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
  1. 编译make
[root@hf-01 nginx-1.12.1]# make
  1. 然后make install
[root@hf-01 nginx-1.12.1]# make install
  1. 查看nginx的编译参数,会看到增加了--with-http_ssl_module
[root@hf-01 nginx-1.12.1]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-16) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module
[root@hf-01 nginx-1.12.1]# 
  1. 检查配置文件语法错误
[root@hf-01 nginx-1.12.1]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@hf-01 nginx-1.12.1]# 
  1. 重启nginx
[root@hf-01 nginx-1.12.1]# /etc/init.d/nginx restart
Restarting nginx (via systemctl):                          [  确定  ]
[root@hf-01 nginx-1.12.1]# 
  1. 查看监听端口,会看到多出一个443端口
[root@hf-01 nginx-1.12.1]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1533/master         
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      5716/nginx: master  
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      5716/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1205/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      1533/master         
tcp6       0      0 :::3306                 :::*                    LISTEN      1576/mysqld         
tcp6       0      0 :::22                   :::*                    LISTEN      1205/sshd           
[root@hf-01 nginx-1.12.1]# 
  1. 切换目录路径,并创建一个测试文件
[root@hf-01 nginx-1.12.1]# cd /data/wwwroot/aming.com/
[root@hf-01 aming.com]# ls
[root@hf-01 aming.com]# vim index.html

This is ssl.
保存退出
  1. 测试,若是直接访问会报400
[root@hf-01 aming.com]# curl -x127.0.0.1:443 https://aming.com/
curl: (56) Received HTTP code 400 from proxy after CONNECT
[root@hf-01 aming.com]# 
  1. 在虚拟机中 /etc/写hosts
[root@hf-01 aming.com]# vim /etc/hosts

加入以下内容
127.0.0.1 aming.com
  1. 测试,不指定-x访问
[root@hf-01 aming.com]# curl https://aming.com/
curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.
[root@hf-01 aming.com]# 
  • 就是说你这个证书被标记为不可信任了,因为这个证书是自己颁发的,实际上是已经配置成功了
  1. 在windows中的host文件添加,并保存
192.168.74.129  aming.com
  1. 浏览器访问aming.com,会看到加载超时
  2. 这时查看虚拟机防火墙iptables -nvL,若是防火墙存在,可以直接ipbables -F清空所有规则,若不想清空所有规则可以增加443端口的规则
[root@hf-01 aming.com]# iptables -nvL

[root@hf-01 aming.com]# iptables -F
[root@hf-01 aming.com]# 
  1. 这时再来访问https://aming.com,会提示是否信任证书,选择 是 ,会访问成功
  1. 这个就是自己颁发证书,浏览器不被信任的时候,会显示红色 不安全 ,而不是绿色
  2. 以后若想正常的访问https,可以去沃通买证书

扩展

  1. 针对请求的uri来代理
  2. 根据访问的目录来区分后端的web
  3. nginx长连接
  4. nginx算法分析