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

[toc]

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

[[email protected] vhost]# yum install -y bind-utils
[[email protected] vhost]# dig baidu.com

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

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

;; ANSWER SECTION:
baidu.com.      174 IN  A   111.13.101.208
baidu.com.      174 IN  A   220.181.57.216

;; Query time: 41 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: 日 3月 18 17:28:34 CST 2018
;; MSG SIZE  rcvd: 70

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

2.编辑配置文件

[[email protected] vhost]# pwd
/usr/local/nginx/conf/vhost
[[email protected] 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打算拒绝访问.

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

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

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

[[email protected] conf]# rm -f tmp.key
[[email protected] conf]# 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) [XX]:86
State or Province Name (full name) []:jiangsu
Locality Name (eg, city) [Default City]:suzhou
Organization Name (eg, company) [Default Company Ltd]:dsfss
Organizational Unit Name (eg, section) []:dsf
Common Name (eg, your name or your server‘s hostname) []:xavi.com
Email Address []:[email protected]

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

4.最终生成CRT证书文件

[[email protected] conf]# 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/[email protected]
Getting Private key

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

[[email protected] conf]#  ls |grep xavilinux
xavilinux.crt
xavilinux.csr
xavilinux.key

四、Nginx配置ssl

1.编辑配置文件

[[email protected] ~]# cd /usr/local/nginx/conf/vhost
[[email protected] 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

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

这说明当前Nginx并不支持SSL,因为之前Nginx编译时并没有配置支持SSL的参数.

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

[[email protected] vhost]# /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
[[email protected] vhost]#  cd /usr/local/src/nginx-1.12.1

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

[[email protected] nginx-1.12.1]# ./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

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

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

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

查看443端口

[[email protected] nginx-1.12.1]# 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.没有问题,然后创建对应的目录和测试文件:

[[email protected] nginx-1.12.1]# mkdir /data/wwwroot/xavi.com
mkdir: 无法创建目录"/data/wwwroot/xavi.com": 文件已存在
[[email protected] nginx-1.12.1]# cd /data/wwwroot/xavi.com
[[email protected] 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

原文地址:http://blog.51cto.com/12995218/2088297

时间: 2024-07-30 22:54:04

15.Nginx负载均衡&SSL密钥对&Nginx配置SSL的相关文章

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

12.17Nginx负载均衡12.18ssl原理12.19生成ssl密钥对 20Nginx配置ssl

12.17Nginx负载均衡查看域名的IP,用dig命令首先安装 一下dig命令yum install -y bind-utils这时候就可以查看到qq.com的ipb 没有更改配置文件前只能访问默认页,更改配置文件之后,加载配置文件再访问curl x127.0.0.1:80 www.qq.com 就不一样了,进入主页了.只不过反馈回来的是网页的原码nginx不支持代理https,新版本 的也只支持http tcp12.18 ssl原理12.19 生成ssl密钥对需要安装 一个包,查看一个命令需

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

(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服务高可用方案,可以利用其来避免单点故障