当前位置: 首页 > news >正文

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

nginx的负载均衡

  • nginx的负载均衡就是把代理服务器指向多个ip,让用户可以通过代理服务器访问到多个web服务器,当其中一个web服务器宕机时,不影响用户访问网站。
  • 配置如下
    [root@akuilinux01 vhost]# vim load.conf 
    upstream qq_com
    {
    ip_hash;
    server 111.161.64.48:80;
    server 111.161.64.40:80;
    }
    server
    {
    listen 80;
    server_name www.qq.com;
    location /
    {
        proxy_pass      http://qq_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来指定多个web server,proxy_pass http://qq_com;这里对应upstream qq_com
  • ip_hash保证同一个用户始终保持在同一台服务器
  • nginx不支持代理https(端口为443),新版本支持tcp
  • dig命令是域名解析工具,安装包bind-utils

    ssl原理

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

      生成ssl密钥对

  • yum install -y openssl安装ssl工具
  • 放在这个文件下/usr/local/nginx/conf
  • openssl genrsa -des3 -out tmp.key 2048 //生成私钥,key文件为私钥
  • openssl rsa -in tmp.key -out aminglinux.key //转换key,取消密码
  • rm -f tmp.key
  • openssl req -new -key aminglinux.key -out aminglinux.csr//生成证书请求文件,需要拿这个文件和私钥一起生产公钥文件
    • 该部分如果不购买证书可以自定义;如果是正式应用在网站上,需要规范填写对应信息(需要购买证书)
  • openssl x509 -req -days 365 -in aminglinux.csr -signkey aminglinux.key -out aminglinux.crt
    //生成公钥,aminglinux.crt为公钥文件

    Nginx配置SSL

  • 配置文件
    [root@akuilinux01 conf]# vim /usr/local/nginx/conf/vhost/ssl.conf
    server
    {
    listen 443;
    server_name aming.com;
    index index.html index.php;
    root /data/wwwroot/aming.com;
    ssl on;  #开启ssl
    ssl_certificate aminglinux.crt; #配置公钥
    ssl_certificate_key aminglinux.key;  #配置私钥
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #配置协议
    }
    [root@akuilinux01 conf]# mkdir /data/wwwroot/aming.com
  • 报错
    [root@akuilinux01 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
      [root@akuilinux01 ~]# cd /usr/local/src/nginx-1.14.0
      [root@akuilinux01 ~]# ./configure --prefix=/usr/local/nginx --with-http_ssl_modul
      [root@akuilinux01 ~]# make
      [root@akuilinux01 ~]# make install
      [root@akuilinux01 nginx-1.14.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@akuilinux01 ~]# /etc/init.d/nginx restart
      Restarting nginx (via systemctl):                          [  确定  ]
      [root@akuilinux01 ~]# 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      5054/nginx: master  
      tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      849/sshd            
      tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1221/master         
      tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      5054/nginx: master  
      tcp6       0      0 :::22                   :::*                    LISTEN      849/sshd            
      tcp6       0      0 ::1:25                  :::*                    LISTEN      1221/master         
      tcp6       0      0 :::3306                 :::*                    LISTEN      1179/mysqld    
      nginx监听了443端口,表示配置生效了
  • 测试
    
    [root@akuilinux01 ~]# cd /data/wwwroot/aming.com/
    [root@akuilinux01 aming.com]# vim index.html
    this is ssl.
    [root@akuilinux01 aming.com]# vim /etc/hosts
    127.0.0.1  aming.com
    [root@akuilinux01 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.
由于不是正式的证书,所以提示不信任


- 也可以更改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)

转载于:https://blog.51cto.com/akui2521/2130451

相关文章:

  • easyui-tree 修改图标
  • egret--一次性给多个对象添加点击事件
  • 《Python从小白到大牛》第3章 第一个Python程序
  • webpack4.X初学之配置VUE开发环境
  • val和var和Java
  • 银河证券互联网转型调研报告:数字化加速器助推银河战舰腾飞
  • 第十七节:易混淆的概念(静态和非静态、拆箱和装箱)
  • 从10亿到百亿规模大促,用云效玩转项目管理
  • for in遍历对象属性注意事项
  • nodejs:开发并发布一个nodejs包
  • android高仿小视频、应用锁、3种存储库、QQ小红点动画、仿支付宝图表等源码...
  • MySQL运维系列 之 如何快速定位IO瓶颈
  • aidl跨进程通讯
  • MySQL主从介绍 准备工作 配置主 配置从 测试主从同步
  • 定位多线程内存越界问题实践总结【转】
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • js ES6 求数组的交集,并集,还有差集
  • Lsb图片隐写
  • ReactNativeweexDeviceOne对比
  • SpriteKit 技巧之添加背景图片
  • Swift 中的尾递归和蹦床
  • Swoft 源码剖析 - 代码自动更新机制
  • 记录一下第一次使用npm
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 扑朔迷离的属性和特性【彻底弄清】
  • 前端之Sass/Scss实战笔记
  • 前端知识点整理(待续)
  • 让你的分享飞起来——极光推出社会化分享组件
  • 什么软件可以剪辑音乐?
  • 以太坊客户端Geth命令参数详解
  • 在GitHub多个账号上使用不同的SSH的配置方法
  • Redis4.x新特性 -- 萌萌的MEMORY DOCTOR
  • 不要一棍子打翻所有黑盒模型,其实可以让它们发挥作用 ...
  • 扩展资源服务器解决oauth2 性能瓶颈
  • ​2020 年大前端技术趋势解读
  • ​水经微图Web1.5.0版即将上线
  • ​虚拟化系列介绍(十)
  • !!Dom4j 学习笔记
  • #HarmonyOS:Web组件的使用
  • $(function(){})与(function($){....})(jQuery)的区别
  • (+3)1.3敏捷宣言与敏捷过程的特点
  • (4)Elastix图像配准:3D图像
  • (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY)讲解
  • (附源码)springboot炼糖厂地磅全自动控制系统 毕业设计 341357
  • (九)信息融合方式简介
  • (提供数据集下载)基于大语言模型LangChain与ChatGLM3-6B本地知识库调优:数据集优化、参数调整、Prompt提示词优化实战
  • .NET Core 2.1路线图
  • .NET3.5下用Lambda简化跨线程访问窗体控件,避免繁复的delegate,Invoke(转)
  • .net网站发布-允许更新此预编译站点
  • /*在DataTable中更新、删除数据*/
  • /bin、/sbin、/usr/bin、/usr/sbin
  • @Bean注解详解
  • @RequestParam,@RequestBody和@PathVariable 区别
  • [Angular 基础] - 表单:响应式表单
  • [Angular] 笔记 16:模板驱动表单 - 选择框与选项