刘伟 发表于 2018-11-10 10:54:41

15.Nginx负载均衡&SSL密钥对&Nginx配置SSL

  


  扩展
  针对请求的uri来代理 http://ask.apelearn.com/question/1049
  根据访问的目录来区分后端的web http://ask.apelearn.com/question/920
  nginx长连接http://www.apelearn.com/bbs/thread-6545-1-1.html
  nginx算法分析   http://blog.sina.com.cn/s/blog_72995dcc01016msi.html

Nginx负载均衡

  负载均衡在服务端开发中算是一个比较重要的特性。因为Nginx除了作为常规的Web服务器外,还会被大规模的用于反向代理前端,因为Nginx的异步框架可以处理很大的并发请求,把这些并发请求hold住之后就可以分发给后台服务端(backend servers,也叫做服务池, 后面简称backend)来做复杂的计算、处理和响应,这种模式的好处是相当多的:隐藏业务主机更安全,节约了公网IP地址,并且在业务量增加的时候可以方便地扩容后台服务器。
  负载均衡可以分为硬件负载均衡和软件负载均衡,前者一般是专用的软件和硬件相结合的设备,设备商会提供完整成熟的解决方案,通常也会更加昂贵。软件的复杂均衡以Nginx占据绝大多数.


1.修改虚拟主机配置文件(以baidu.com为例)
  

# yum install -y bind-utils  
# dig baidu.com
  

  
;DiG 9.9.4-RedHat-9.9.4-51.el7_4.2baidu.com
  
;; global options: +cmd
  
;; Got answer:

  
;; ->>HEADER  

  

  看到有两个IP,可以看到两个IP,有两个IP就可以走负载均衡了

2.编辑配置文件
  

# pwd  
/usr/local/nginx/conf/vhost
  
# vim load.conf
  

  
uptream baidu_com
  
{
  ip_hash;
  server 111.13.101.208:80;
  server 220.181.57.216:80;
  
}
  
server
  
{
  listen 80;
  server_name www.baidu.com;
  location /
  {
  proxy_pass      http://baidu_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 baidu_com
  
#名字自定义
  
{
  ip_hash;
  
#   目的:同一个用户保持在同一个服务器上
  
#   即当域名指向多个IP时,保证每个用户始终解析到同一IP
  server 111.13.101.208:80;
  server 220.181.57.216:80;
  
#指定web服务器的IP
  
}
  

  

3.测试
  由于指向dig qq.com时候也是有问题,只能获得一个IP,百度有两个IP打算拒绝访问.
  

# curl -x127.0.0.1 baidu.com -I  
curl: (7) Failed connect to 127.0.0.1:1080; 拒绝连接
  

二、ssl原理

SSL工作流程


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


三、生成ssl密钥对

1.生成key即“私钥”:openssl genrsa
  

# 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:
  

  //这一步操作是生成key即“私钥”,2048为加密字符长度,会让我们输入密码,不能太短,否者不成功。

2.把上一步生成的tmp.key在转换成xavilinux.key
  

# openssl rsa -in tmp.key -out xavilinux.key  
Enter pass phrase for tmp.key:
  
writing RSA key
  

  把tmp.key转化成zlinux.key,目的是删除刚才设置的密码,如果不清除密码,后续nginx加载它的时候要输入它的密码,很不方便.

3.生成证书请求文件
  生成证书请求文件,key文件和csr文件生成最终的公钥文件。Common Name为后面配置Nginx配置文件server_name
  

# rm -f tmp.key  
# openssl req -new -key xavilinux.key -out xavilinux.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) :86
  
State or Province Name (full name) []:jiangsu
  
Locality Name (eg, city) :suzhou
  
Organization Name (eg, company) :dsfss
  
Organizational Unit Name (eg, section) []:dsf
  
Common Name (eg, your name or your server's hostname) []:xavi.com
  
Email Address []:dsf1626@163.com
  

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

4.最终生成CRT证书文件
  

# openssl x509 -req -days 365 -in xavilinux.csr -signkey xavilinux.key -out xavilinux.crt  
Signature ok
  
subject=/C=86/ST=jiangsu/L=suzhou/O=dsfss/OU=dsf/CN=xavi.com/emailAddress=dsf1626@163.com
  
Getting Private key
  

  以上操作最终目的是生成xavilinux.key和xavilinux.crt两个文件,其实购买SSL证书主要得到这两个文件,有了这两个文件就可以配置nginx了
  

#ls |grep xavilinux  
xavilinux.crt
  
xavilinux.csr
  
xavilinux.key
  

四、Nginx配置ssl

1.编辑配置文件
  

# cd /usr/local/nginx/conf/vhost  
# vim /usr/local/nginx/conf/vhost/ssl.conf
  

  
server
  
{
  listen 443;
  server_name xavi.com;
  index index.html index.php;
  root /data/wwwroot/xavi.com;
  ssl on;
  ssl_certificate xavilinux.crt;
  ssl_certificate_key xavilinux.key;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  
}
  

2.检查配置文件有无问题,结果说明当前的Nginx不支持SSL
  

# /usr/local/nginx/sbin/nginx -t  
nginx: 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,因为之前Nginx编译时并没有配置支持SSL的参数.

3.重新编译一次,加上SSL参数:
  

# /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
  
#cd /usr/local/src/nginx-1.12.1
  

  

  确定版本号,找到配置文件中那部分是支持ssl服务的
  

# ./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
  

4.增加支持SSL的参数./configure --prefix=/usr/local/nginx --with-http_ssl_module
  

# ./configure --prefix=/usr/local/nginx --with-http_ssl_module  
# make
  
# make install
  

5. 编译完成,再来检验一次,启动nginx服务
  

#/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
  
# /etc/init.d/nginx start
  
Starting nginx (via systemctl):                            [确定]
  

  

查看443端口
  

# 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:111             0.0.0.0:*               LISTEN      1/systemd
  
tcp      0      0 0.0.0.0:80            0.0.0.0:*               LISTEN      6156/nginx: master
  
tcp      0      0 192.168.122.1:53      0.0.0.0:*               LISTEN      1779/dnsmasq
  
tcp      0      0 0.0.0.0:22            0.0.0.0:*               LISTEN      1047/sshd
  
tcp      0      0 127.0.0.1:631         0.0.0.0:*               LISTEN      1044/cupsd
  
tcp      0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1589/master
  
tcp      0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      6156/nginx: master
  
tcp6       0      0 :::111                  :::*                  LISTEN      1/systemd
  
tcp6       0      0 :::22                   :::*                  LISTEN      1047/sshd
  
tcp6       0      0 ::1:631               :::*                  LISTEN      1044/cupsd
  
tcp6       0      0 ::1:25
  

6.没有问题,然后创建对应的目录和测试文件:
  

# mkdir /data/wwwroot/xavi.com  
mkdir: 无法创建目录"/data/wwwroot/xavi.com": 文件已存在
  
# cd /data/wwwroot/xavi.com
  
# vi index.html
  

7.curl https://xavi.com报错,编辑/etc/hosts文件,加上xavi.com




再编辑hosts文件,写入一行(hosts路径为C:\Windows\System32\drivers\etc),用浏览器查看


查看防火墙:iptables -nvL


关闭防火墙: iptables -F

  高级网站:12306



页: [1]
查看完整版本: 15.Nginx负载均衡&SSL密钥对&Nginx配置SSL