12.17 Nginx负载均衡12.18 ssl原理12.19 生成ssl密钥对12.20 Nginx配置ssl

12.17 Nginx负载均衡

什么是负载均衡

代理一台机器为代理,两台机器,就叫负载均衡
代理服务器后面可以是多台web服务器,多个web服务器提供服务的时候,就可以实现一个负载均衡
正常情况下,用户访问web服务器,是一台一台去请求;要么就是指定一个IP,把这域名解析到多台服务器上

例:
用户1 --> web1服务器
用户2 --> web2服务器

用户1 --> web1服务器(宕机)
用户1因为解析到了web1,但web1宕机了。没法访问

这时候如果使用了nginx负载均衡,web1宕机,代理服务器就不会继续把请求发送到web1

配置负载均衡

通过dig可以查看到域名的解析IP地址
包“ bind-utils ”

[root@aminglinux-02 vhost]# yum install -y bind-util
[root@aminglinux-02 vhost]# dig qq.com

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

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

;; ANSWER SECTION:
qq.com.            353    IN    A    14.17.32.211

;; Query time: 35 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: 三 8月 16 00:14:17 CST 2017
;; MSG SIZE  rcvd: 51

新增一个配置文件load.conf

upstream qq_com       //这个名字可以自定义
{
    ip_hash;      //目的是为了让同一个用户始终保持在同一个机器上
    server 14.17.32.211:80;  //如果域名解析端口是80,这段配置上的指定端口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;
    }
}

upstream来指定多个web server
当有多个服务器同时对一个域名提供服务的时候,长时间访问一个域名,在一定的时效内,会出现需要重新登录或者是说跳转到另外一个地址的服务器上;ip_hash,就是使通过这个代理访问的同一个域名的多个IP的服务器是,始终保持在一个IP上对这个域名进行访问

测试

没有使用负载均衡配置的时候,curl -x 127.0.0.1 默认访问的是虚拟主机的

[root@aminglinux-02 vhost]# curl -x127.0.0.1:80 www.qq.com
This is the default site.

配置完成后检查语法和重新加载服务

-t && -s reload

测试

[root@aminglinux-02 vhost]# curl -x127.0.0.1:80 www.qq.com
    var _mtac = {};
    (function() {
        var mta = document.createElement("script");
        mta.src = "http://pingjs.qq.com/h5/stats.js?v2.0.2";
        mta.setAttribute("name", "MTAH5");
        mta.setAttribute("sid", "500460529");
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(mta, s);
    })();
    </script>
</body>
</html><!--[if !IE]>|xGv00|66aaf676da3c9edb56f9fd489826d8e6<![endif]-->

这时curl到的是qq的主页,反馈回来的是网页的源码

知识点:
nginx不支持去代理https ,支持http、tcp
解决办法,nginx监听443端口,但web服务必须是80端口

12.18 ssl原理

http和https的区别

https通信是加密的,如果不加密,中间传输数据包的时候会被截到,就会导致信息泄露,https就是对这个通信的数据包进行加密

SSL工作流程

  1. 浏览器发送一个https的请求给服务器;
  2. 服务器要有一套数字证书,可以自己制作(后面的操作就是阿铭自己制作的证书),也可以向组织申请,区别就是自己颁发的证书需要客户端验证通过,才可以继续访问,而使用受信任的公司申请的证书则不会弹出>提示页面,这套证书其实就是一对公钥和私钥;
  3. 服务器会把公钥传输给客户端;
  4. 客户端(浏览器)收到公钥后,会验证其是否合法有效,无效会有警告提醒,有效则会生成一串随机数,并用收到的公钥加密;
  5. 客户端把加密后的随机字符串传输给服务器;
  6. 服务器收到加密随机字符串后,先用私钥解密(公钥加密,私钥解密),获取到这一串随机数后,再用这串随机字符串加密传输的数据(该加密为对称加密,所谓对称加密,就是将数据和私钥也就是这个随机字符串>通过某种算法混合在一起,这样除非知道私钥,否则无法获取数据内容);
  7. 服务器把加密后的数据传输给客户端;
  8. 客户端收到数据后,再用自己的私钥也就是那个随机字符串解密;

image

12.19 生成ssl密钥对

在自己的虚拟机生成ssl
需要用到openssl工具
如果没有安装就安装

[root@aminglinux-02 conf]# rpm -qf `which openssl`
openssl-1.0.1e-60.el7_3.1.x86_64

生成密钥

[root@aminglinux-02 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:                       //再次输入密码

命令解释

openssl genrsa -des3 -out tmp.key 2048
genrsa 生成rsa的密码
2048  2048长度
名字为 tmp.key

因为这个加密,之后在nginx访问的时候还会提示输入密码,这样会很麻烦,所以就需要去掉密码

转换key,取消密码

[root@aminglinux-02 conf]# openssl rsa -in tmp.key -out aminglinux.key
Enter pass phrase for tmp.key:
unable to load Private Key
139930989189024:error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt:evp_enc.c:604:
139930989189024:error:0906A065:PEM routines:PEM_do_header:bad decrypt:pem_lib.c:483:

输入错误密码会提示

[root@aminglinux-02 conf]# openssl rsa -in tmp.key -out aminglinux.key
Enter pass phrase for tmp.key:
writing RSA key

完成取消密码

命令解释

-in tmp.key 输入tmp.key
-out aminglinux.key 输出 aminglinux.key

生成公钥

生成证书请求文件,需要拿这个文件和私钥一起生产公钥文件

[root@aminglinux-02 conf]# openssl req -new -key aminglinux.key -out aminglinux.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) []:GuangXi      //省或州
Locality Name (eg, city) [Default City]:LiuZhou   //城市
Organization Name (eg, company) [Default Company Ltd]:aming  //公司
Organizational Unit Name (eg, section) []:aming   //组织
Common Name (eg, your name or your server's hostname) []:aminglinux  //您的主机名
Email Address []:amin@adminlinux.com   //邮箱

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:1231512315   //设置密码
An optional company name []:  //一个可选的公司名称

用请求证书文件和私钥文件,生成一个公钥

[root@aminglinux-02 conf]# openssl x509 -req -days 365 -in aminglinux.csr -signkey aminglinux.key -out aminglinux.crt
Signature ok
subject=/C=cn/ST=GuangXi/L=LiuZhou/O=aming/OU=aming/CN=aminglinux/emailAddress=amin@adminlinux.com
Getting Private key

12.20 Nginx配置ssl

生成一个新的配置文件

[root@aminglinux-02 conf]# cd vhost/
[root@aminglinux-02 vhost]# pwd
/usr/local/nginx/conf/vhost

server
{
    listen 443;
    server_name aming.com;   //主机名
    index index.html index.php;
    root /data/wwwroot/aming.com;   //root 目录
    ssl on;                                            //打开ssl
    ssl_certificate aminglinux.crt;      //指定公钥
    ssl_certificate_key aminglinux.key;   //指定私钥
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;   //ssl 的协议
}

因为是新的主机,所以需要去创建对应的目录

配置完成后检查语法

[root@aminglinux-02 vhost]# /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

报错,因为安装nginx的时候是最简单的配置,不支持SSL

添加SSL依赖模块

查看配置

[root@aminglinux-02 vhost]# /usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.12.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
configure arguments: --prefix=/usr/local/nginx

需要重新编译,加上一条支持SSL

进入nginx目录重新编译

[root@aminglinux-02 src]# cd nginx-1.12.1
[root@aminglinux-02 nginx-1.12.1]# pwd
/usr/local/src/nginx-1.12.1

查看所需的依赖配置

[root@aminglinux-02 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

所需“ --with-http_ssl_module ”

[root@aminglinux-02 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
checking for OS
 + Linux 3.10.0-514.el7.x86_64 x86_64
checking for C compiler ... found
 + using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
checking for gcc -pipe switch ... found
...
..
.
  nginx error log file: "/usr/local/nginx/logs/error.log"
  nginx http access log file: "/usr/local/nginx/logs/access.log"
  nginx http client request body temporary files: "client_body_temp"
  nginx http proxy temporary files: "proxy_temp"
  nginx http fastcgi temporary files: "fastcgi_temp"
  nginx http uwsgi temporary files: "uwsgi_temp"
  nginx http scgi temporary files: "scgi_temp"

[root@aminglinux-02 nginx-1.12.1]# make && make install
...
..
.
test -d '/usr/local/nginx/logs' \
    || mkdir -p '/usr/local/nginx/logs'
make[1]: 离开目录“/usr/local/src/nginx-1.12.1”
[root@aminglinux-02 nginx-1.12.1]# echo $?
0

检查配置

[root@aminglinux-02 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-11) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module

重新检查语法

[root@aminglinux-02 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@aminglinux-02 nginx-1.12.1]# /etc/init.d/nginx restart
Restarting nginx (via systemctl):                          [  确定  ]

检查监听端口

[root@aminglinux-02 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 0.0.0.0:80              0.0.0.0:*               LISTEN      5761/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      995/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2181/master
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      5761/nginx: master
tcp6       0      0 :::3306                 :::*                    LISTEN      1773/mysqld
tcp6       0      0 :::22                   :::*                    LISTEN      995/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      2181/master

多了一个443端口的监听

测试

创建测试文件

[root@aminglinux-02 nginx-1.12.1]# cd /data/wwwroot/aming.com/
[root@aminglinux-02 aming.com]# pwd
/data/wwwroot/aming.com
[root@aminglinux-02 aming.com]# vim index.html
This is SSL

不能继续使用curl -x测试

[root@aminglinux-02 aming.com]# curl -x127.0.0.1:443 https://aming.com
curl: (56) Received HTTP code 400 from proxy after CONNECT

会报错

改hosts,直接访问

[root@aminglinux-02 aming.com]# vi /etc/hosts
[root@aminglinux-02 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.

报错: curl:(60)Peer的证书发行者被标记为不受用户信任。
这是ssl已经是安装上了

可以尝试用浏览器访问
改动本机Windows主机hosts
如果还是访问不到,就需要加上443端口放行,或者清空默认规则

[root@aminglinux-02 aming.com]# iptables -I INPUT -p tcp --dport 443 -j ACCEPT

转载于:https://my.oschina.net/nova12315/blog/1784798

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值