烂泥:haproxy学习之https配置

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:

在前一段时间,我写了几篇有关学习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

clip_image001[4]

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

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配置完毕后,我们选择来测试其跳转。如下:

clip_image002[10]

你会发现在浏览器中,无论你输入的是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并存配置完毕后,我们选择来测试其跳转。如下:

clip_image003[4]

clip_image002[11]

通过测试你会发现,在浏览器中如果你输入的是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

clip_image004[4]

同台服务器不同域名之间的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配置配置完毕后,我们现在来进行测试。如下:

clip_image005[4]

clip_image002[12]

通过上图,我们可以发现在浏览器中输入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

clip_image006[4]

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

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

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

clip_image002[13]

clip_image007[4]

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

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


本文转自 烂泥行天下 51CTO博客,原文链接:http://blog.51cto.com/ilanni/1710201

相关实践学习
基于函数计算快速搭建Hexo博客系统
本场景介绍如何使用阿里云函数计算服务命令行工具快速搭建一个Hexo博客。
相关文章
|
2月前
|
网络协议 Java 应用服务中间件
Springboot+ubuntu+Let‘s Encrypt配置https
Springboot+ubuntu+Let‘s Encrypt配置https
35 0
|
2月前
|
弹性计算 应用服务中间件 Apache
ECS配置问题之输入ip无法访问如何解决?
ECS配置指的是对阿里云Elastic Compute Service(弹性计算服务)实例的硬件和软件资源进行设置的过程;本合集将详述如何选择合适的ECS配置、调整资源配比以及优化实例性能,以满足不同应用场景的需求。
|
2月前
|
安全 应用服务中间件 网络安全
HTTPS 基础原理和配置 -2
HTTPS 基础原理和配置 -2
|
2月前
|
安全 应用服务中间件 网络安全
HTTPS 基础原理和配置 -3
HTTPS 基础原理和配置 -3
|
3月前
|
JavaScript
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)(上)
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
29 0
|
3月前
|
JavaScript
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)(下)
Node.js【GET/POST请求、http模块、路由、创建客户端、作为中间层、文件系统模块】(二)-全面详解(学习总结---从入门到深化)
25 0
|
9天前
|
运维 Java 应用服务中间件
Tomcat详解(七)——Tomcat使用https配置实战
Tomcat详解(七)——Tomcat使用https配置实战
21 4
|
12天前
|
域名解析 网络协议 应用服务中间件
阿里云服务器配置免费https服务
阿里云服务器配置免费https服务
|
16天前
|
域名解析 网络协议 应用服务中间件
阿里云SSL证书配置(HTTPS证书配置)
该内容是一个关于如何在阿里云上准备和购买SSL证书,以及如何为网站启用HTTPS的步骤指南。首先,需要注册并实名认证阿里云账号,然后在SSL证书控制台选择证书类型、品牌和时长进行购买。申请证书时填写域名信息,并进行DNS验证,这包括在阿里云域名管理板块添加解析记录。完成验证后提交审核,等待证书审核通过并下载Nginx格式的证书文件。最后,将证书配置到网站服务器以启用HTTPS。整个过程涉及账户注册、实名认证、证书购买、DNS设置和证书下载及安装。
81 0
|
17天前
|
应用服务中间件 网络安全 nginx
nginx配置https访问
nginx配置https访问
27 0