本文由ilanniweb提供友情赞助,首发于烂泥行天下

想要获得更多的文章,可以关注我的微信ilanniweb。

在前一段时间,我写了几篇有关学习haproxy的文章。今天我们再来介绍下haproxy的https配置,https协议的好处在此,我们就不就作介绍了。

我们只介绍如何配置https,以及https在实际生产环境中的应用。

PS:本实验全部在haproxy1.5.4版本进行测试通过。haproxy1.3版本以下haproxy配置参数可能不能使用,需要注意版本号。

以下haproxy配置是线上生产环境直接使用的。

一、业务要求

现在根据业务的实际需要,有以下几种不同的需求。如下:

1.1 http跳转https

把所有请求http://http.ilanni.com的地址全部跳转为https//:http.ilanni.com这个地址。

1.2 http与https并存

服务器同时开放http://http.ilanni.com和https://http.ilanni.com的访问形式。

1.3同台服务器不同域名之间的https与http

同一台服务器对http.ilanni.com域名访问的全部跳转为https://http.ilanni.com,而对haproxy.ilanni.com访问走http协议,也就是跳转到http://haproxy.ilanni.com这个地址。

1.4同台服务器多域名均使用https

同一台服务器对http.ilanni.com和haproxy.ilanni.com访问走http是协议。

二、配置haproxy并测试业务需求

现在我们根据业务的需求,我们来配置haproxy一一达到其需求。

2.1 http跳转https配置

说实话haproxy的https配置要比nginx配置简单的多了,我们只需要加入几行代码即可实现https的功能。

http跳转https的haproxy配置文件内容,如下:

global

log 127.0.0.1 local0

log 127.0.0.1 local1 notice

maxconn 4096

uid 188

gid 188

daemon

tune.ssl.default-dh-param 2048

defaults

log global

mode http

option httplog

option dontlognull

option http-server-close

option forwardfor except 127.0.0.1

option redispatch

retries 3

option redispatch

maxconn 2000

timeout http-request 10s

timeout queue 1m

timeout connect 10s

timeout client 1m

timeout server 1m

timeout http-keep-alive 10s

timeout check 10s

maxconn 3000

listen admin_stats

bind 0.0.0.0:1080

mode http

option httplog

maxconn 10

stats refresh 30s

stats uri /stats

stats auth admin:admin

stats hide-version

frontend weblb

bind *:80

acl is_http hdr_beg(host) http.ilanni.com

redirect scheme https if !{ ssl_fc }

bind *:443 ssl crt /etc/haproxy/ilanni.com.pem

use_backend httpserver if is_http

backend httpserver

balance source

server web1 127.0.0.1:7070 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

在以上配置文件中,需要注意的选项如下:

tune.ssl.default-dh-param 2048因为我们的SSL密钥使用的是2048bit加密,所以在此进行声明。

acl is_http hdr_beg(host) http.ilanni.com

redirect scheme https if !{ ssl_fc }

bind *:443 ssl crt /etc/haproxy/ilanni.com.pem

这三行表示把所有访问http.ilanni.com这个域名的请求,全部转发到https://http.ilanni.com这个连接。

2.2测试http跳转https

http跳转https配置完毕后,我们选择来测试其跳转。如下:

你会发现在浏览器中,无论你输入的是http.ilanni.com,还是http://http.ilanni.com亦或是https://http.ilanni.com,都会自动跳转到https://http.ilanni.com。

这样就达到了,把所有的http请求跳转到https的目的。

2.3 http与https并存配置

haproxy要实现http和https并存的话,配置也很简单,只需要把haproxy分别监控不同的端口就行,配置文件如下:

global

log 127.0.0.1 local0

log 127.0.0.1 local1 notice

maxconn 4096

user haproxy

group haproxy

daemon

tune.ssl.default-dh-param 2048

defaults

log global

mode http

option httplog

option dontlognull

retries 3

option redispatch

maxconn 2000

timeout connect 5000ms

timeout client 50000ms

timeout server 50000ms

listen admin_stats

bind 0.0.0.0:1080

mode http

option httplog

maxconn 10

stats refresh 30s

stats uri /stats

stats auth admin:admin

stats hide-version

frontend weblb

bind *:80

acl is_http hdr_beg(host) http.ilanni.com

use_backend httpserver if is_http

backend httpserver

balance source

server web1 127.0.0.1:7070 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

frontend weblb443

bind *:443 ssl crt /etc/haproxy/ilanni.com.pem

acl is_443 hdr_beg(host) http.ilanni.com

use_backend httpserver443 if is_443

backend httpserver443

balance source

server web1 127.0.0.1:7070 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

在以上配置文件中,我们定义了两个前端,一个前端用于监听80端口,也就是http协议。另外一个前端监听443端口,也就是https协议。

此时haproxy会根据客户端请求的协议进行分发,如果发现客户端请求的是http协议,则把该请求分发到监听80端口的前端。如果发现客户端请求的是https协议,则把该请求分发到监听443端口的前端。如此就达到了haproxy让http和https并存的要求。

2.4测试http与https并存

http与https并存配置完毕后,我们选择来测试其跳转。如下:

通过测试你会发现,在浏览器中如果你输入的是http://http.ilanni.com或者是http.ilanni.com都会直接跳转到http://http.ilanni.com,而输入的是https://http.ilanni.com,则只会跳转到https://http.ilanni.com。

如此就到达了,我们业务的要求实现http和https并存。

2.5同台服务器不同域名之间的https与http配置

同台服务器不同域名之间的http和https配置比较复杂,第一需要监听两个端口,第二还要根据不同的域名进行分发。

haproxy配置文件如下:

global

log 127.0.0.1 local0

log 127.0.0.1 local1 notice

maxconn 4096

uid 188

gid 188

daemon

tune.ssl.default-dh-param 2048

defaults

log global

mode http

option httplog

option dontlognull

option http-server-close

option forwardfor except 127.0.0.1

option redispatch

retries 3

option redispatch

maxconn 2000

timeout http-request 10s

timeout queue 1m

timeout connect 10s

timeout client 1m

timeout server 1m

timeout http-keep-alive 10s

timeout check 10s

maxconn 3000

listen admin_stats

bind 0.0.0.0:1080

mode http

option httplog

maxconn 10

stats refresh 30s

stats uri /stats

stats auth admin:admin

stats hide-version

frontend weblb

bind *:80

acl is_haproxy hdr_beg(host) haproxy.ilanni.com

acl is_http hdr_beg(host) http.ilanni.com

redirect prefix https://http.ilanni.com if is_http

use_backend haproxyserver if is_haproxy

backend haproxyserver

balance source

server web1 127.0.0.1:9090 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

frontend weblb443

bind *:443 ssl crt /etc/haproxy/ilanni.com.pem

acl is_443 hdr_beg(host) http.ilanni.com

use_backend httpserver443 if is_443

backend httpserver443

balance source

server web1 127.0.0.1:7070 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

同台服务器不同域名之间的https与http配置,我们配置了两个前端一个用于监听80端口,并且根据不同的域名进行跳转。在80端口的规则中,如果客户端请求的是http.ilanni.com,这个域名的话,则haproxy会把该请求直接跳转到https://http.ilanni.com。如果是haproxy.ilanni.com,这个域名的话,则分发到后端的服务器。

另外一个前端用于监听443端口,用于分发客户端https://http.ilanni.com的请求。

2.6测试同台服务器不同域名之间的https与http配置

同台服务器不同域名之间的https与http配置配置完毕后,我们现在来进行测试。如下:

通过上图,我们可以发现在浏览器中输入haproxy.ilanni.com会跳转到http://haproxy.ilanni.com这个地址,而如果输入的是http.ilanni.com或者是http://http.ilanni.com,亦或是https://http.ilanni.com的话,都会跳转到https://http.ilanni.com。

如此就达到了我们的业务要求,同台服务器上访问haproxy.ilanni.com直接跳转到80端口,如果访问的是http.ilanni.com域名的话则跳转到https://http.ilanni.com这个地址。

2.7同台服务器多域名均使用https配置

要使同台服务器的两个设置多个域名都使用https协议的话,配置很简单。只需要在haproxy中启用各自的https配置即可。

haproxy配置文件,如下:

global

log 127.0.0.1 local0

log 127.0.0.1 local1 notice

maxconn 4096

uid 108

gid 116

daemon

tune.ssl.default-dh-param 2048

defaults

log global

mode http

option httplog

option dontlognull

option http-server-close

option forwardfor except 127.0.0.1

option redispatch

retries 3

option redispatch

timeout http-request 10s

timeout queue 1m

timeout connect 10s

timeout client 1m

timeout server 1m

timeout http-keep-alive 10s

timeout check 10s

maxconn 3000

listen admin_stats

bind 0.0.0.0:1080

mode http

option httplog

maxconn 10

stats refresh 30s

stats uri /stats

stats auth admin:admin

stats hide-version

frontend web80

bind *:80

acl is_http hdr_beg(host) http.ilanni.com

redirect scheme https if !{ ssl_fc }

bind *:443 ssl crt /etc/haproxy/ilanni.com.pem

acl is_haproxy hdr_beg(host) haproxy.ilanni.com

redirect scheme https if !{ ssl_fc }

bind *:443 ssl crt /etc/haproxy/ilanni.com.pem

use_backend httpserver if is_http

use_backend haproxyserver if is_haproxy

backend httpserver

balance source

server web1 127.0.0.1:6060 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

backend haproxyserver

balance source

server web1 127.0.0.1:9090 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

配置文件比较简单,在此就不做进一步的讲解了。

2.8测试同台服务器多域名均使用https

同台服务器多域名均使用https,配置完毕后,现在我们来测试下。

通过上图,我们可以看到在浏览中无论是输入http.ilanni.com、http://http.ilanni.com,还是haproxy.ilanni.com、http://haproxy.ilanni.com,都会跳转到相应的https地址。

这也达到了我们业务的要求。

ha 配置ssl_烂泥:haproxy学习之https配置相关推荐

  1. oracle11g中用asmlib配置磁盘组,ASM学习笔记_配置ASMLIB磁盘组

    ASM学习笔记_配置ASMLIB磁盘组 目录 1 ASMLIB Introduction 2 虚拟机添加一个共享磁盘(块设备) 3 下载,安装ASMLIB 4 配置,使用ASMLib 磁盘组 #### ...

  2. 3d建模电脑配置要求_3D建模学习对于电脑配置要求高不高?

    点击上方蓝字,关注我们了解更多建模知识 一 电脑配置需要高配还是低配? 这个必须是高配,低配你电脑卡的就直接就跑不动了,你平时用的那种一般的电脑,顶多算个小皮卡车,拉点一般的东西,还能跑起来,整点大吨 ...

  3. linux中apache配置、虚拟主机设置及https配置

    apache:企业常用的web服务.用来提供http://(超文本传输协议) Apache HTTP Server(简称Apache),是一个开放源码的网页服务器,可以在大多数计算机操作系统中运行,由 ...

  4. haproxy代理https配置方法【转】

    haproxy代理https配置方法[转] 记得在之前的一篇文章中介绍了nginx反向代理https的方法,今天这里介绍下haproxy代理https的方法: haproxy代理https有两种方式: ...

  5. 烂泥:高负载均衡学习haproxy之安装与配置

    本文首发于烂泥行天下 有关高负载均衡的软件,目前使用比较多的是haproxy.nginx和lvs.下面我们就开始学习haprxoy这款软件. 一.haproxy介绍 以下开始介绍有关haproxy的原 ...

  6. 烂泥:学习Nagios(三): NRPE安装及配置

    本文首发于烂泥行天下 在前两篇文章中,我们介绍了有关nagios的安装与配置,文章为<烂泥:学习Nagios(一):Nagios安装>.<烂泥:学习Nagios(二):Nagios配 ...

  7. 烂泥:学习tomcat之通过shell批量管理多个tomcat

    2019独角兽企业重金招聘Python工程师标准>>> 本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 公司的业务是使 ...

  8. Ubuntu18.04+CUDA10.2 深度学习开发环境配置指南

    深度学习 Author:louwill Machine Learning Lab 搞深度学习环境永远是第一步.笔者之前也写过配置的两篇文章,但时间久远,目前来看版本已经过旧了.之前两篇参考: 深度学习 ...

  9. 网络工程师学习参考资料路由器配置案例分析

    网络工程师学习参考资料路由器配置案例分析 2006-08-30 23:01:00 标签:配置 路由器 案例 网络工程师 [推送到技术圈] INTERNET共享资源的方式越来越多,就大多数而言,DDN专 ...

最新文章

  1. 进入Python世界——Python基础知识
  2. linux 内存越界判断_虚拟内存 和 page fault 的解释
  3. java instanceof运算符_java的instanceof运算符
  4. virtual box 针对Unable to load R3 module 解决方案
  5. Hbase CallQueueTooBigException 异常处理
  6. python基础之应用场景
  7. 毕业设计——基于STM32的智能家具系统(语音识别控制、步进电机、舵机)
  8. Windows Mobile系统PDA进行GPS导航的入门知识
  9. chmod 赋权所有_linux 命令 赋权 chmod
  10. D3.js用动画渲染数据集的显示
  11. Windows 8实用窍门系列:6.Windows 8中的Popup使用方式
  12. 云币网及KYC【区块链生存训练】
  13. 任天堂游戏服务器系统,买个服务器当电脑主机如何确保买到新版任天堂Switch游戏主机?...
  14. java如何利用JNative调用dll文件
  15. android卸载保留数据,谷歌Android 10新特性:应用卸载时可保留数据
  16. 在windows和ubuntu下安装Syncthing
  17. nodejs+IIS+WebMatrix
  18. 传奇服务器的爆率文件在哪里,传奇私服爆率调整
  19. Windows沙拉:为什么下载的文件打开时会有警告,而且会被“锁定”?
  20. signature=71c2363ad5776ff530a286dd0cdf792c,SUSY Multilepton Signatures at Tevatron

热门文章

  1. 2022.08.12 第三组 高小涵
  2. 【MOOC】华中科技大学计算机组成原理慕课答案-第六章-中央处理器(一)
  3. 现在捡芝麻都需要有见识吗?
  4. 网易企业邮箱:三道防御、七项措施,切实保障企业邮箱安全【企业邮箱申请】
  5. 数据结构之稀疏数组队列
  6. 莫尔斯码(Morse Code)
  7. 计算机应用好中吗小木虫,[计算机软件及应用]小木虫 Origin 使用问题集锦.doc
  8. Vero VISI 2021.0.2109
  9. U3D-3D MAX Biped导出.fbx到Unity的Humanoid的骨架不匹配(Disable Triangle Pelvis, Enable Triangle Neck)
  10. 检查是否存在满足条件的数字组合