Nginx负载均衡、ssl原理、生成ssl密钥对、Nginx配置ssl

简介:

Nginx负载均衡

Nginx负载均衡即为当代理服务器将自定义的域名解析到多个指定IP时,通过upstream来保证用户可以通过代理服务器正常访问各个IP。

代理一台机器叫做代理,代理两台及两台服务器就能叫做负载均衡。

  • 负载均衡配置

    创建一个配置文件/usr/local/nginx/conf/vhost/load.con

    [root@localhost ~]# vim /usr/local/nginx/conf/vhost/load.conf
    upstream qq.com
    #借助upstream模块,自定义域名
    {
    ip_hash;
    #保证同一个用户始终保持在同一台机器上
    #即当域名指向多个IP时,保证同一个用户始终解析到之前访问的IP
    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.com;
    #不支持再proxy_pass中写多个ip。代理中可以写成ip,但是再负载中不能写ip,要写入和upstream 对应
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    }

    测试并重载配置:
    [root@localhost ~]# /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@localhost ~]# /usr/local/nginx/sbin/nginx -s reload

  • 测试

    设置代理前:
    [root@localhost ~]# curl -x127.0.0.1:80 www.qq.com
    This is the default directory.

    设置代理后:
    [root@localhost ~]# curl -x127.0.0.1:80 www.qq.com
    ......
    #会显示网页的源码。

注意: Nginx不支持代理https,只能代理http,新版本的Nginx可以代理tcp。

  • dig命令

dig 命令是常用的域名解析工具。

安装dig 命令
[root@localhost ~]# yum install -y bind-utils

www.qq.com解析到了3个ip
[root@localhost ~]# dig www.qq.com

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

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

;; ANSWER SECTION:
www.qq.com.     97  IN  A   14.17.32.211
www.qq.com.     97  IN  A   14.17.42.40
www.qq.com.     97  IN  A   59.37.96.63

;; Query time: 60 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: 一 1月 08 21:
  • http、https、tcp

    • HTTP超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。
    • HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议要比http协议安全。如果不加密,中间传输数据包的有时候会被截到,就会导致信息泄露,https就是对这个通信的数据包进行加密。
    • HTTP默认的端口号为80,HTTPS的端口号为443。
    • TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的、可靠的、基于字节流的传输层通信协议,由IETF的RFC 793定义。默认监听80端口。

SSL原理

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

  • 安装ssl

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

  • ssl工作流程
    pnJ72F.jpg

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

生成ssl密钥对

ssl证书就是一对公钥和私钥

  • 创建私钥

    切换目录,密钥对会保存在该目录下:
    [root@localhost ~]# cd /usr/local/nginx/conf/

    [root@localhost conf]# openssl genrsa -des3 -out tmp.key 2048
    #生成rsa格式的密钥对,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,取消密码

    [root@localhost conf]# openssl rsa -in tmp.key -out huang.key
    #in指定哪个密钥要被转换,out指定输出密钥的 名称。
    Enter pass phrase for tmp.key:
    writing RSA key
    #需要输入上一个tmp.key 的密码。这时候,tmp.key和huang.key其实是一个,只是huang.key没密码。

  • 删除密钥文件

    [root@localhost conf]# rm -f tmp.key
    #删除有密码的key

  • 生成证书请求文件

生成请求文件目的是为了让请求文件和私钥一起去生成一个公钥。

[root@localhost conf]# openssl req -new -key huang.key -out huang.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 //国家
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) []:centos 03 //主机名
Email Address []:abc@abc.com //邮箱

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

说明: 该部分内容如果不购买证书可以自定义;如果是正式应用在网站上,需要规范填写对应信息。

  • 创建公钥

    [root@localhost conf]# openssl x509 -req -days 365 -in huang.csr -signkey huang.key -out huang.crt
    #365是密钥生效的天数。
    Signature ok
    subject=/C=CN/ST=Beijing/L=Beijing/O=Beijing/OU=Beijing/CN=centos 03/emailAddress=abc@abc.com
    Getting Private key
    [root@localhost conf]# ls /usr/local/nginx/conf/huang*
    /usr/local/nginx/conf/huang.crt /usr/local/nginx/conf/huang.csr /usr/local/nginx/conf/huang.key

    crt是公钥,key是私钥。

Nginx配置SSL

创建新的配置文件:
[root@localhost conf]# vim /usr/local/nginx/conf/vhost/ssl.conf
server
{
listen 443;
server_name abc.com;
index index.html index.php;
root /data/wwwroot/abc.com;
ssl on;
#开启ssl
ssl_certificate huang.crt;
#配置公钥
ssl_certificate_key huang.key;
#配置私钥
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#配置协议,一般情况三种都配置上。
}

检测配置:
[root@localhost 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
#检测报错,为时便ssl配置,需要重新编译Nginx。

重新编译Nginx:
[root@localhost conf]# cd /usr/local/src/nginx-1.8.0/
[root@localhost nginx-1.8.0]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
.....
[root@localhost nginx-1.8.0]# echo $?
0
[root@localhost nginx-1.8.0]# make
......
[root@localhost nginx-1.8.0]# echo $?
0
[root@localhost nginx-1.8.0]# make install
......
[root@localhost nginx-1.8.0]# echo $?
0

#./configure --hlep 查看可以安装的模块

测试配置文件,并重启nginx服务:
[root@localhost nginx-1.8.0]# /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@localhost nginx-1.8.0]# /etc/init.d/nginx restart
Restarting nginx (via systemctl):                          [  确定  ]
[root@localhost nginx-1.8.0]# 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      4905/nginx: master  
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1243/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      2121/master         
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      4905/nginx: master  
......
#nginx 监听了443和80端口
  • 测试

    添加本地域名
    root@localhost nginx-1.8.0]# vim /etc/hosts
    127.0.0.1 abc.com

    [root@localhost nginx-1.8.0]# curl https://abc.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.

    #因为该证书是自己创建的,所以提示证书不被信任!!!

使用浏览器访问

需要先在hosts文件中添加本地域名,并清空或添加防火墙规则。

192.168.159.132 abc.com

pn4nYV.md.png

购买正规证书:沃通等



本文转自 豆渣锅 51CTO博客,原文链接:http://blog.51cto.com/754599082/2058866

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
相关文章
|
7月前
|
缓存 负载均衡 监控
135_负载均衡:Redis缓存 - 提高缓存命中率的配置与最佳实践
在现代大型语言模型(LLM)部署架构中,缓存系统扮演着至关重要的角色。随着LLM应用规模的不断扩大和用户需求的持续增长,如何构建高效、可靠的缓存架构成为系统性能优化的核心挑战。Redis作为业界领先的内存数据库,因其高性能、丰富的数据结构和灵活的配置选项,已成为LLM部署中首选的缓存解决方案。
795 25
|
安全 算法 网络协议
解析:HTTPS通过SSL/TLS证书加密的原理与逻辑
HTTPS通过SSL/TLS证书加密,结合对称与非对称加密及数字证书验证实现安全通信。首先,服务器发送含公钥的数字证书,客户端验证其合法性后生成随机数并用公钥加密发送给服务器,双方据此生成相同的对称密钥。后续通信使用对称加密确保高效性和安全性。同时,数字证书验证服务器身份,防止中间人攻击;哈希算法和数字签名确保数据完整性,防止篡改。整个流程保障了身份认证、数据加密和完整性保护。
|
负载均衡 前端开发 应用服务中间件
Tomcat的负载均衡和动静分离(与nginx联动)
总的来说,负载均衡和动静分离是提高Web应用性能的两个重要手段。通过合理的配置和使用,我们可以让Web应用更好地服务于用户。
391 21
|
弹性计算 负载均衡 网络协议
配置SLB监听器
配置SLB监听器
854 63
|
域名解析 弹性计算 监控
slb测试基本配置检查
slb测试基本配置检查
394 60
|
弹性计算 负载均衡 监控
slb配置健康检查
slb配置健康检查
402 5
|
监控 负载均衡 容灾
slb测试配置
slb测试配置
443 5
|
负载均衡 前端开发 应用服务中间件
负载均衡指南:Nginx与HAProxy的配置与优化
负载均衡指南:Nginx与HAProxy的配置与优化
958 3
|
11月前
|
安全 数据建模 应用服务中间件
阿里云SSL证书价格、证书类型及免费版证书申请和证书部署教程参考
阿里云SSL证书有收费版也有免费版,收费版DV域名级SSL类型405元起,免费版证书为DV域名级SSL类型,每个实名个人和企业主体在一个自然年内可以一次性领取20张免费证书。本文为大家详细介绍阿里云SSL证书价格情况,包括不同域名类型、证书类型、证书等级和证书品牌的相关收费标准,以及免费版证书的申请和部署教程参考。
|
7月前
|
网络协议 应用服务中间件 网络安全
阿里云SSL证书申请具体操作流程,以申请免费SSL证书为例
阿里云免费SSL证书由Digicert提供,单域名可申请20张,有效期3个月。通过数字证书管理控制台,完成购买、域名验证(DNS或文件)、提交审核后下载,支持Nginx、Apache等多服务器格式。
1085 0