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

短命女 2022-05-22 02:15 230阅读 0赞

Nginx负载均衡

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

负载均衡配置

配置参数:

  1. [root@123 ~]# vim /usr/local/nginx/conf/vhost/load.conf
  2. upstream qq.com
  3. #自定义域名
  4. {
  5. ip_hash;
  6. #保证同一个用户始终保持在同一台机器上
  7. #即当域名指向多个IP时,保证每个用户始终解析到同一IP
  8. server 61.135.157.156:80;
  9. server 125.39.240.113:80;
  10. #指定web服务器的IP
  11. }
  12. server
  13. {
  14. listen 80;
  15. server_name www.qq.com;
  16. location /
  17. {
  18. proxy_pass http://qq.com;
  19. proxy_set_header Host $host;
  20. proxy_set_header X-Real-IP $remote_addr;
  21. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  22. }
  23. }

检测

代理前

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

使用代理前,会直接解析到默认虚拟主机。

代理后

  1. [root@123 ~]# /usr/local/nginx/sbin/nginx -t
  2. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  4. [root@adailinux ~]# /usr/local/nginx/sbin/nginx -s reload
  5. [root@adailinux ~]# curl -x127.0.0.1:80 www.qq.com

使用代理后会解析到代理服务器所指向的IP

  1. [root@123 ~]# dig www.qq.com
  2. ;; ANSWER SECTION:
  3. www.qq.com. 138 IN A 61.135.157.156
  4. www.qq.com. 138 IN A 125.39.240.113
  5. ;; Query time: 13 msec
  6. ;; SERVER: 119.29.29.29#53(119.29.29.29)
  7. ;; WHEN: 8 15 16:41:11 CST 2017
  8. ;; MSG SIZE rcvd: 71

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

dig命令

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

如果服务器中没有该命令,手动安装:

  1. [root@123 ~]# yum install -y bind-utils

语法: dig [域名]

http、https、tcp

HTTP超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议。
HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版。HTTPS协议是由SSL+HTTP协议构建的可进行加密传输、身份认证的网络协议要比http协议安全。
HTTP默认的端口号为80,HTTPS的端口号为443。
TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的、可靠的、基于字节流的传输层通信协议,由IETF的RFC 793定义。默认监听80端口。

12.18 SSL原理

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

SSL工作流程

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

  1. [root@123 ~]# yum install -y openssl

SSL工作流程

2018061307001535

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

12.19 生成SSL密钥对

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

创建私钥

  1. [root@123 ~]# cd /usr/local/nginx/conf/
  2. [root@123 conf]# openssl genrsa -des3 -out tmp.key 2048
  3. #生成SSL密钥
  4. Generating RSA private key, 2048 bit long modulus
  5. ....................................................................................+++
  6. ...............................................................+++
  7. e is 65537 (0x10001)
  8. Enter pass phrase for tmp.key:
  9. Verifying - Enter pass phrase for tmp.key:

说明: 在此指定密码!

转换key,取消密码:

  1. [root@123 conf]# openssl rsa -in tmp.key -out linux.key
  2. Enter pass phrase for tmp.key:
  3. writing RSA key

删除密钥文件:

  1. [root@123 conf]# rm -f tmp.key

生成证书请求文件

需要拿这个文件和私钥一起生产公钥文件:

  1. [root@123 conf]# openssl req -new -key linux.key -out linux.csr
  2. You are about to be asked to enter information that will be incorporated
  3. into your certificate request.
  4. What you are about to enter is what is called a Distinguished Name or a DN.
  5. There are quite a few fields but you can leave some blank
  6. For some fields there will be a default value,
  7. If you enter '.', the field will be left blank.
  8. -----
  9. Country Name (2 letter code) [XX]:CN
  10. State or Province Name (full name) []:aaa
  11. Locality Name (eg, city) [Default City]:Beijing
  12. Organization Name (eg, company) [Default Company Ltd]:Beijing
  13. Organizational Unit Name (eg, section) []:Beijing
  14. Common Name (eg, your name or your server's hostname) []:linux Email Address []:aaa@linux.com Please enter the following 'extra' attributes to be sent with your certificate request A challenge password []:123456 An optional company name []:123456

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

创建公钥:

  1. [root@123 conf]# openssl x509 -req -days 365 -in linux.csr -signkey linux.key -out linux.crt
  2. Signature ok
  3. subject=/C=CN/ST=adai/L=Beijing/O=Beijing/OU=Beijing/CN=adailinux/emailAddress=adai@adailinux.com
  4. Getting Private key

Nginx配置SSL

  1. [root@123 conf]# cd vhost/
  2. [root@123 vhost]# vim ssl.conf
  3. server
  4. {
  5. listen 443;
  6. server_name aming.com;
  7. index index.html index.php;
  8. root /data/wwwroot/aming.com;
  9. ssl on;
  10. #开启ssl
  11. ssl_certificate linux.crt;
  12. #配置公钥
  13. ssl_certificate_key linux.key;
  14. #配置私钥
  15. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  16. #配置协议
  17. }
  18. [root@123 vhost]# mkdir /data/wwwroot/aming.com

检测

报错:

  1. [root@123 conf]# /usr/local/nginx/sbin/nginx -t
  2. nginx: [emerg] unknown directive "ssl" in /usr/local/nginx/conf/vhost/ssl.conf:7
  3. nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

未识别ssl配置,需要重新编译Nginx:

  1. [root@123 conf]# cd /usr/local/src/nginx-1.12.1/
  2. [root@123 nginx-1.12.1]# ./configure --prefix=/usr/local/nginx --with-http_ssl_module
  3. [root@123 conf]# make
  4. [root@123 conf]# make install
  5. [root@123 nginx-1.12.1]# /usr/local/nginx/sbin/nginx -t
  6. nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
  7. nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
  8. [root@123 nginx-1.12.1]# /etc/init.d/nginx restart
  9. Restarting nginx (via systemctl): [ 确定 ]
  10. [root@123 nginx-1.12.1]# netstat -lntp
  11. Active Internet connections (only servers)
  12. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  13. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5991/nginx: master
  14. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1735/sshd
  15. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2040/master
  16. tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN 5991/nginx: master
  17. tcp6 0 0 :::3306 :::* LISTEN 1990/mysqld
  18. tcp6 0 0 :::22 :::* LISTEN 1735/sshd
  19. tcp6 0 0 ::1:25 :::* LISTEN 2040/master

nginx监听80和443端口。

测试

  1. [root@123 nginx-1.12.1]# cd /data/wwwroot/aming.com/
  2. [root@123 adai.com]# vim index.html
  3. This is ssl.

添加本地域名:

  1. [root@123 adai.com]# vim /etc/hosts
  2. 127.0.0.1 aming.com
  3. [root@123 vhost]# curl https://aming.com/
  4. 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
  5. using the --cacert option.
  6. If this HTTPS server uses a certificate signed by a CA represented in
  7. the bundle, the certificate verification probably failed due to a
  8. problem with the certificate (it might be expired, or the name might
  9. not match the domain name in the URL).
  10. If you'd like to turn off curl's verification of the certificate, use
  11. the -k (or --insecure) option.

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

使用浏览器检测:

20180613070204522

注: 进行该测试之前需要更改Windows的hosts文件。

扩展
针对请求的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

发表评论

表情:
评论列表 (有 0 条评论,230人围观)

还没有评论,来说两句吧...

相关阅读

    相关 nginx 配置 ssl

    1.1 Nginx如果未开启SSL模块,配置Https时提示错误 原因也很简单,nginx缺少http\_ssl\_module模块,编译安装的时候带上--wit

    相关 nginx 配置ssl

    前言 服务器安全一直是近些年来热门的话题,一个正常的请求发送到服务端之后,服务端将响应结果返回客户端,可以理解此次交互建立了一个请求响应的通道; 如果这个通道不够安全,

    相关 nginx配置ssl

    先生成网关证书 ,仿照CA模式  1 生成私钥,需要密码的  openssl genrsa -des3 -out server.key 1024  2 生成证书请求

    相关 Nginx 配置 ssl 证书

    > 在部署线下测试环境时浏览器会自动将http替换成https,又因为测试环境没有证书经常会导致跳转错误,所以自己配置一个ssl证书就可以避免这个问题。 > 下面记录我在M