LNMP(Nginx负载均衡,SSL原理,Nginx配置SSL,生产SSL密钥对)

一、Nginx负载均衡

负载均衡:单从字面上的意思来理解就可以解释N台服务器平均分担负载,不会因为某台服务器负载高宕机而某台服务器闲置的情况。那么负载均衡的前提就是要有多台服务器才能实现,也就是两台以上即可。

在开始部署负载均衡之前,我们先来介绍一个命令,dig命令需要yum安装一下

[[email protected] ~]# yum install bind-utils

[[email protected] ~]# dig qq.com            (dig后加域名,他可以返回2个ip.实则域名解析,我们就用这两个ip测试负载均衡)

; <<>> DiG 9.9.4-RedHat-9.9.4-51.el7_4.1 <<>> qq.com

;; global options: +cmd

;; Got answer:

;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 57513

;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:

;qq.com. IN A

;; ANSWER SECTION:

qq.com. 601 IN A 125.39.240.113

qq.com. 601 IN A 61.135.157.156

[[email protected] ~]# vim /usr/local/nginx/conf/vhost/load.conf            (再来编写一个配置文件,需要用到upstream模块,upstream:数据转发功能,为nginx提供了跨越单机的横向处理能力,使nginx摆脱只能为终端节点提供单一功能的限制,而使它具备了网路应用级别的拆分、封装和整合的战略功能。)

upstream qq

{

ip_hash;           (负载均衡有多个web服务器,我们需要一个长连接来保持于一个服务器的链接,这里需要用到hash)

server 61.135.157.156:80;

server 125.39.240.113:80;

}

server

{

listen 80;

server_name www.qq.com;

location /

{

proxy_pass      http://qq;   (这里写的要与upstream一致,因为域名是虚拟的,下面的2个ip才是重要的)

proxy_set_header Host   $host;

proxy_set_header X-Real-IP      $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

}

检查语法错误并且重新加载配置文件。

[[email protected] ~]# /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

[[email protected] ~]# /usr/local/nginx/sbin/nginx -s reload

[[email protected] ~]# curl -x127.0.0.1:80 www.qq.com                (发现返回的是qq页面的源代码)

nginx不支持代理Https服务。也就是说不支持访问web服务器的443端口。

二、SSL原理

https和http相比,https的通信是加密的。如果不加密,比如你访问一个很重要的网站,数据包还是会到达,但是可能会用人从中间复制一份。https会把数据包加密,就算从中间复制也无法解码。

https的工作流程:

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

三、Nginx配置ssl

Nginx配置ssl

[[email protected] nginx-1.8.0]# vim /usr/local/nginx/conf/vhost/ssl.conf           (编写ssl的配置文件)

server

{

listen 443;                                        (监听443端口)

server_name lty.com;                                 (编写server_name)

index index.html index.php;

root /data/wwwroot/lty.com;

ssl on;                                       (开启ssl服务)

ssl_certificate lty.crt;                           (指定公钥)

ssl_certificate_key lty.key;                         (指定私钥)

ssl_protocols TLSv1 TLSv1.1 TLSv1.2;                   (指定三种模式)

}

[[email protected] nginx-1.8.0]# /usr/local/nginx/sbin/nginx -t (如果nginx编译的时候没有加上ssl,这里会报错需要重新编译)

重新编译:

[[email protected] nginx-1.8.0]# cd /usr/local/src/nginx-1.8.0

[[email protected] nginx-1.8.0]# ./configure --help |grep -i ssl

--with-http_ssl_module             enable ngx_http_ssl_module

--with-mail_ssl_module             enable ngx_mail_ssl_module

--with-openssl=DIR                 set path to OpenSSL library sources

--with-openssl-opt=OPTIONS         set additional build options for OpenSSL

[[email protected] nginx-1.8.0]# ./configure --prefix=/usr/local/nginx/ --with-http_ssl_module

[[email protected] nginx-1.8.0]# make && make install

编译完成后就可以检查语法错误了,然后重新启动

[[email protected] 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

[[email protected] nginx-1.8.0]# /etc/init.d/nginx restart

Restarting nginx (via systemctl):                          [  确定  ]

创建测试页面:

[[email protected] nginx-1.8.0]# mkdir /data/wwwroot/lty.com

[[email protected] nginx-1.8.0]# vim /data/wwwroot/lty.com/index.html

因为是我们自己办法的证书,直接修改/etc/hosts,用Curl测试并看不出效果,提示证书已经失去信任。

[[email protected] nginx-1.8.0]# vim /etc/hosts

127.0.0.1   lty.com

[[email protected] nginx-1.8.0]# curl https://lty.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。

192.168.52.101 lty.com

用浏览器打开

https://lty.com

如果访问不到,查看防火墙信息简单的办法直接-F

12306网站是自己颁发的证书:(在中国的政府有些网站,认为只有自己的颁发的安全,所以用自己颁发的证书)

如果想要买证书,可以搜索 沃通,

原文地址:http://blog.51cto.com/13407306/2059168

时间: 2024-07-30 13:18:36

LNMP(Nginx负载均衡,SSL原理,Nginx配置SSL,生产SSL密钥对)的相关文章

12.17 Nginx负载均衡;12.18 ssl原理;12.19 生产ssl密钥对;12.20 N

12.17 Nginx负载均衡:12.18 ssl原理:12.19 生产ssl密钥对:12.20 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:/

(2)LVS+Keepalived高可用负载均衡架构原理及配置

1.keepalived 介绍2.keepalived 优缺点3.keepalived 应用场景4.keepalived 安装配置5.keepalived+lvs 高可用6.keepalived+nginx 高可用7.keepalived 切换原理8.性能优化9.常见故障 一.keepalived 介绍 1.keepalived 定义keepalived是一个基于VRRP(virtual route redundent protocol)协议来实现的LVS服务高可用方案,可以利用其来避免单点故障

12.17 Nginx负载均衡;12.18 ssl原理;12.19 生产ssl密钥对;12.20 Nginx配置ssl

扩展: 针对请求的uri来代理 http://ask.apelearn.com/question/1049 根据访问的目录来区分后端web http://ask.apelearn.com/question/920 12.17 Nginx负载均衡 1. 安装dig命令: [[email protected] ~]# yum install -y bind-utils 2. 用dig获取qq.com的ip地址: [[email protected] ~]# dig qq.com 3. 创建ld.co

FastDFS分布式文件系统&amp;Nginx负载均衡最小环境安装配置[超级详解]

1.背景 FastDFS 是一款开源的.分布式文件系统(Distributed File System),由淘宝开发平台部资深架构师余庆开发.该开源项目的主页是 http://code.google.com/p/fastdfs .可以通过 fastdfs.sourceforge.net 下载.FastDFS论坛是 http://www.csource.org ,目前是指向 ChinaUnix 开源项目孵化平台的一个板块 FastDFS,网址为 bbs.chinaunix.net/forum-24

nginx负载均衡以及反向代理配置

记录一下方便以后自己查看 1.环境准备 lb-01:192.168.33.135 nginx-lb centos7 rs-01:192.168.33.131 apache-web centos6.x rs-02:192.168.33.132 nginx-web centos6.x 2.环境安装 lb-01 安装nginx,配置nginx源 # cat /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.

学习下nginx负载均衡--深入理解nginx

作为代理服务器,一般都需要向上游服务器转发请求.这里的负载均衡是指通过一种策略尽量把请求平均的分发都上游服务器 1.upstream 语法 upstream name {} 配置快: http 栗子(实验通过,每次请求均匀的分布在两台机器上) upstream backend { server 192.168.1.100:8080; server 192.168.1.101:8080; } server { location / { proxy_pass  http://backend; } }

Centos配置Nginx负载均衡详解

在日常网络数据开发中,我们对服务器的处理能力要求很高,但是在服务器有限的情况下,怎么才能更好的利用服务器资源,使得我们的服务器最大限度发挥自己的作用呢?负载均衡是一种很好的办法.     哪什么是Nginx负载均衡呢? Nginx是一个轻量级的.高性能的WebServer,他主要可以干下面两件事: (1).作为http服务器(和apache的效果一样) (2).作为反向代理服务器实现负载均衡 现在Nginx到处都可以见到,经常会看到宕机后的网页会显示nginx的字样,这也说明Nginx由于高性能

nginx负载均衡

nginx负载均衡 Nginx负载均衡的理解 Nginx是一个轻量级的.高性能的WebServer,他主要可以干下面两件事: 作为http服务器(和apache的效果一样) 作为反向代理服务器实现负载均衡 现在Nginx到处都可以见到,经常会看到宕机后的网页会显示nginx的字样,这也说明Nginx由于高性能.使用配置简.开源单这些特点被越来越多的用户所接受,所使用. 其中第一种作为http服务器,结合php-fpm进程,对发来的请求进行处理,nginx本身并不会解析php,他只是作为一个服务器

Nginx负载均衡及反向代理

Nginx 负载均衡 什么是nginx负载均衡? Nginx作为一个强大的web服务器管理软件,自身带有负载均衡和反向代理的功能,那么他和lvs之间有什么区别呢? LVS负载:是基于4层的负载均衡, 优点: 1抗负载能力强 2配置性低 3工作稳定 4无流量 5基本支持所有应用负载均衡,如WEB,数据库 Nginx负载:基于7层的负载均衡 特点: 1nginx工作在网络7层,他可以针对http本身做分发策略,如域名,目录结构等 2nginx对网络依赖小 3配置简单,测试方便 4nginx同样能承受

tomcat单机多应用部署,Nginx负载均衡

一.Windows 1. 安装两个tomcat服务器以上 2. 配置环境变量 CATALINA_BASE:D:\servers\Tomcat8 CATALINA_HOME:D:\servers\Tomcat8 TOMCAT_HOME:D:\servers\Tomcat8 CATALINA_2_BASE:D:\servers\Tomcat8 - 2 CATALINA_2_HOME:D:\servers\Tomcat8 - 2 TOMCAT_2_HOME:D:\servers\Tomcat8 - 2