十三周一次课Nginx负载均衡、ssl原理、生成ssl密钥对、配置ssl

1. Nginx负载均衡

Nginx负载均衡就是指 当代理服务器将自定义的域名解析到多个指定IP时,通过upstream模块来保证用户可以通过代理服务器正常访问各个IP(反向代理多台服务器就是负载均衡)。

1.1 负载均衡配置参数

[root@host ~]# vim /usr/local/nginx/conf/vhost/load.conf
upstream qq
#自定义域名
{
    ip_hash;
#目的是为了保证同一个用户始终保持在同一台机器上
#还有就是为了当域名指向多个IP时,保证每个用户始终解析到同一IP,添加的两个IP是命令:dig www.qq.com。解析出来的
    server 61.135.157.156:80;
    server 125.39.240.113:80;
#指定web服务器的IP
}
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;
    }
}

 

因为该配置也使用location板块,所以本节可结合日志管理一起配置:

[root@localhost ~]# vim /usr/local/nginx/conf/vhost/test.com.conf
……
location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
{
    expires 7d;
    valid_referers none blocked server_names  *.test.com ;
    #定义referer白名单
    if ($invalid_referer) {
        return 403;
    #if函数的意思是:如果不是白名单内的域名,返回值:403
    }
    access_log off;
}
……

[root@localhos ~]# /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@localhos ~]# /usr/local/nginx/sbin/nginx -s reload

说明 “location ~* ^.+”在此0“ * ”的作用是后面匹配的内容不区分大小写。

1.2 检测

代理前

[root@host ~]# curl -x127.0.0.1:80 www.qq.com 
This is the default directory.

#没使用代理时,会直接解析到默认的虚拟主机。

代理后

[root@host ~]# /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@host ~]# /usr/local/nginx/sbin/nginx -s reload
[root@host ~]# curl -x127.0.0.1:80 www.qq.com
……
#使用代理后,会解析到代理服务器所指向的IP的网页代码
[root@localhost ~]# curl -e "http://www.baidu.com/1.txt" -x127.0.0.1:80 -I test.com/baidu.png
HTTP/1.1 403 Forbidden
Server: nginx/1.12.1
Date: Mon, 14 Aug 2017 06:22:36 GMT
Content-Type: text/html
Content-Length: 169
Connection: keep-alive

即,使用非白名单内的referer进行访问,被拒绝!!!

 

1.3 dig命令

dig命令是常用域名的解析工具,可以寻找域名的全部IP。

如果服务器中没有安装命令

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

解析qq网站的全部IP

[root@host ~]# dig www.qq.com

;; ANSWER SECTION:
www.qq.com.        138    IN    A    61.135.157.156
www.qq.com.        138    IN    A    125.39.240.113

;; Query time: 12 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: 二 9月 12 22:44:23 CST 2017
;; MSG SIZE  rcvd: 61

2. ssl原理

SSL(Secure Sockets Layer 安//全//套接层)协议,及其继任者TLS(Transport Layer Security传输层安全)协议,是为网络通信提供安全及数据完整性的一种安全协议。

2.1 http、https、tcp

  • HTTP超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。
  • HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),简单讲是HTTP的安全加密版。
  • HTTP默认的端口号为80,HTTPS的端口号为443。
  • TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的、可靠的、基于字节流的传输层通信协议。默认监听80端口。
  • http是应用层协议, tcp是传输层。 http使用tcp传输文本数据; http只是定义了tcp数据的解析方式

2.2 SSL工作流程

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

生产ssl的时候(生成证书请求文件那步)有一步是要输入 ssl 的域名,后期配置nginx的ssl的时候,nginx配置文件中ssl.conf 中的server_name 的域名和证书文件中的域名是要一致

申请证书的时候,是需要确定好域名的,也就是说颁发的证书是针对某个域名的。自然,nginx配置中要和证书绑定的域名一致。

3. 生成ssl密钥对

SSL证书就是一对公钥和私钥。

3.1 准备工具

如果虚拟机中没有此工具,手动安装:

[root@host ~]# yum install -y openssl

3.2 创建私钥

[root@host ~]# cd /usr/local/nginx/conf/

[root@host conf]# openssl genrsa -des3 -out tmp.key 2048  //生成SSL密钥
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:   //密钥需要我们设置密码,一般我们都不需要再设置密码,所以要转换一下key,取消密码

[root@host conf]# openssl rsa -in tmp.key -out host.key  //转换一下key,将tmp.key 转换为没密码的host.key

Enter pass phrase for tmp.key:
writing RSA key

[root@host conf]# rm -f tmp.key  //删除tmp.key

3.3 自己生成证书

[root@host conf]# openssl req -new -key host.key -out host.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]:11
State or Province Name (full name) []:BeiJing
Locality Name (eg, city) [Default City]:BeiJing
Organization Name (eg, company) [Default Company Ltd]:BeiJing
Organizational Unit Name (eg, section) []:BeiJing
Common Name (eg, your name or your server's hostname) []:host
Email Address []:zhouqunic@qq.com
#以上是配置证书信息,因为是自己颁发给自己的证书,就随意瞎填或者干脆Enter跳过,如果是正式应用在自己的网站上,最好规范填写。

Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:123456

3.4 创建公钥:

[root@host conf]# openssl x509 -req -days 365 -in host.csr -signkey host.key -out host.crt  //这里的host.crt为公钥,host.key位私钥

Signature ok
subject=/C=11/ST=BeiJing/L=BeiJing/O=BeiJing/OU=BeiJing/CN=host/emailAddress=zhouqunic@qq.com
Getting Private key

4. Nginx配置ssl

4.1 配置文件

[root@host conf]# cd vhost/

[root@host vhost]# vim ssl.conf
server
{
    listen 443;
    server_name zhouqun.com;
    index index.html index.php;
    root /data/wwwroot/zhouquncom;
    ssl on;      //开启ssl
    ssl_certificate host.crt;     //配置公钥
    ssl_certificate_key host.key;        //配置私钥
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;      ///配置协议
}

[root@host vhost]# mkdir /data/wwwroot/wzq.com

4.2 检测

[root@host 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@host conf]# /usr/local/nginx/sbin/nginx -V
nginx version:nginx/1.12.1
built by gcc 4.8.5 2018615 (Centos 7.4)(GCC)
configure arguments:--prefix=/usr/local/nginx       #没有指定ssl

4.3 报错 unknown directive "ssl" 未识别ssl配置,需要重新编译nginx,加上--with-http_ssl_module

[root@host conf]# cd /usr/local/src/nginx-1.12.1/

[root@host nginx-1.12.1]# ./configure --help | grep -i ssl
--with-http_ssl_module              enable ngx_http_ssl_module
...........                          .....
......                               .............
........                             ............


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

[root@host conf]# make
[root@host conf]# make install
[root@host conf]# /usr/local/nginx/sbin/nginx -V
nginx version:nginx/1.12.1
built by gcc 4.8.5 2018615 (Centos 7.4)(GCC)  
configure arguments:--prefix=/usr/local/nginx  --with-http_ssl_module       #增加的部分

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

[root@host 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      5991/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*              LISTEN      1735/sshd          
tcp        0      0 127.0.0.1:25            0.0.0.0:*              LISTEN      2040/master        
tcp        0      0 0.0.0.0:443代理端口          0.0.0.0:*              LISTEN      5991/nginx: master  
tcp6      0      0 :::3306                :::*                    LISTEN      1990/mysqld        
tcp6      0      0 :::22                  :::*                    LISTEN      1735/sshd          
tcp6      0      0 ::1:25                  :::*                    LISTEN      2040/master  

nginx监听80和443端口。

4.4 测试

[root@host nginx-1.12.1]# cd /data/wwwroot/wzq.com/

[root@host adai.com]# vim 1.txt

This is ssl.
[root@host adai.com]# mv 1.txt index.html

4.5 添加本地域名:

[root@host adai.com]# vim /etc/hosts
127.0.0.1  wzq.com #没有就添加

[root@host vhost]# curl https://wzq.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.

因为该证书是自己创建的,没有符合https组织的规范,不能被正确识别,如果换上正规的证书,就没问题了。

所以,如果要使用浏览器检测,那么进行该测试之前,需要更改Windows的hosts文件,不然就会证书出错的。

证书 不被信任的时候,就会在浏览器输入栏显示红色。

在浏览器里面输入:https://wzq.com,访问不到,查看防护墙有没有关闭,命令:iptables -nvl,或者iptables -F

b66f97b9a66910d636c753202b78b124649.jpg

668295eb003a3b9ccab04b98ed36da8c0e3.jpg

9d17a8539350997a4c045695be8018da022.jpg

 

 

常见问题:

1、nginx 怎么检测后端tomcat 服务健康状态?比如在实际生产中,要上传tomcat代码,要保证这台tomcat没有客户连接的时候才可以停掉服务上传代码,这种时候我怎么判断这台tomcat有没有客户端连接?

(nginx能不能对后端tomcat进行检测,怎么判断后端tomcat TCP链接是否断开,没有客户连接才能上传代码

答:

1、tomcat有没有连接,在于nginx,把nginx一关,或者让它跳转到一个“业务维护”的页面,自然就没有新的连接了。 

2、

nginx检测不了有没有连接。 

但nginx可以通过安装第三方模块来检测后端的tomcat是不是正常的,比如不能访问,出现403,404这样的错误状态码。 可以检测。

3、(生产中,上代码是怎么上的?)  把nginx一关,或者让它跳转到一个“业务维护”的页面 。

 

2、配置 nginx+tomcat+ssl 负载均衡群集时,https可以跳转访问后端的tomcat群集,但是用http访问的时候就报400,浏览器提示可疑重定向问题,这个是要当访问80时强制跳转到443吗?下面是nginx负载均衡的配置

1.jpg

2.jpg

答:看配置并没有问题。 在16.12和16.15上是否能用80端口访问www.test.com ? 

 

3、nginx 负载均衡默认的轮询和加权轮询是不是不支持后端tomcat集群的session 共享?

答:tomcat session共享问题,用redis存session。NoSQL章节有session 共享有相关的资料

tomcat怎么用redis存储会话?

 

4、nginx负载均衡配置文件中的server_name 是后台web 的域名吗?

答:

对  就是浏览器上输入的那个域名。 

要两边都保持一致。

 

5、nginx做负载均衡,下面做了两个tomcat负载,192.168.0.11  tomcat+jdk  www.test1.com    192.168.0.13 tomcat+jdkwww.test2.com      192.168.0.12  nginx 调度器   。访问两个tomcat都可以,如图: 访问192.168.0.12 时,报404呢?

image.png

image.png

 

image.png

答:

定义一个域名   你用0.12 去访问,在0.11和0.13上同样也是这个0.12的域名,肯定有问题啊

 

 

6、

客户端向服务器发送请求,并且带有请求参数。这个过程会加密吗? 流程是怎样的呢?

我猜是不是,第五步的时候,客户端把加密后的随机字符串(暂且叫sign)传给服务器的同时,也把要请求的参数(被sign加密过)一同传给服务器?

答:会加密。 这个网址和数据是一块传过来的,自然加密、解密的过程和咱们课上讲的是一样的过程。

 

7、nginx做负载均衡的时候,不支持https访问,现在不都是加证书支持443端口访问吗,用https访问nginx搭建的负载均衡,应该怎么做??

答:

前段代理配置https,后端的真实服务器只能是80,不能是443.

80 代理 443或者443代理443不行。

443代理80没问题。

 

8、重新编译,make&&make install 不会将之前的模块给覆盖掉吗

答:重新编译  需要保证编译参数和原来一样或者比原来还要多,否则原来的模块就不能用了。

 

9、监听端口没有443端口?

blob.png

答:你的虚拟主机配置文件 没有被识别。 

在试试,先stop 在 start   再查看netstat -nltp

解决本机测试 ssl 时访问不成功的问题。

只要将 openssl 生成的签名文件 aminglinux.crt  放到 /etc/pki/ca-trust/source/anchors/ 目录就可以了,如果要在其他机器做客户端访问,也只需要将 .crt 文件复制到客户机的相应目录即可。

具体步骤:

1.   cp /usr/local/nginx/conf/ssl/aminglinux.crt /etc/pki/ca-trust/source/anchors/

2.   /bin/update-ca-trust 

3.   curl https://aming.com/

    结果:

curl.png

 

 

参考连接:https://my.oschina.net/u/3497124/blog/1510043

转载于:https://my.oschina.net/u/3803405/blog/1811423

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值