haproxy代理https配置方法【转】
记得在之前的一篇文章中介绍了nginx反向代理https的方法,今天这里介绍下haproxy代理https的方法:
haproxy代理https有两种方式:
1)haproxy服务器本身提供ssl证书,后面的web服务器走正常的http
2)haproxy服务器本身只提供代理,后面的web服务器走https(配置ssl证书)
第一种方式:haproxy服务器本身提供ssl证书
注意:
需要编译haproxy的时候支持ssl
编译参数:
#make TARGET=linux26 USE_OPENSSL=1 ADDLIB=-lz
#ldd haproxy | grep ssl
libssl.so.10 => /usr/lib64/libssl.so.10 (0x00007fb0485e5000)
配置参数(修改haproxy.cfg文件)
frontend https_frontend
bind *:443 ssl crt /etc/ssl/certs/servername.pem
mode http
option httpclose
option forwardfor
reqadd X-Forwarded-Proto:\ https
default_backend web_server
backend web_server
mode http
balance roundrobin
cookie SERVERID insert indirect nocache
server s1 192.168.1.150:80 check cookie s1
server s2 192.168.1.151:80 check cookie s2
-----------------------------------------------------------
注意:这里的pem 文件是下面两个文件合并而成:
#cat servername.crt servername.key |tee servername.pem
-----------------------------------------------------------
第二种方式:haproxy服务器本身只提供代理,没有ssl证书 (一般我们常用的就是这种方式)
这种方式,haproxy不需要重新编译支持ssl,简单方便,只需要后面的web服务器配置好ssl即可。
配置参数(修改haproxy.cfg文件)
frontend https_frontend
bind *:443
mode tcp
default_backend web_server
backend web_server
mode tcp
balance roundrobin
stick-table type ip size 200k expire 30m
stick on src
server s1 192.168.1.150:443
server s2 192.168.1.151:443
---------------------------------------------------------
注意,这种模式下mode 必须是tcp 模式
---------------------------------------------------------
本文由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地址。
这也达到了我们业务的要求
转自
haproxy代理https配置方法 http://www.mamicode.com/info-detail-1539510.html
烂泥:haproxy学习之https配置 http://www.cnblogs.com/ilanni/p/4941056.html
haproxy代理https配置方法【转】的更多相关文章
- 【转】Nginx服务器的反向代理proxy_pass配置方法讲解
[转]Nginx服务器的反向代理proxy_pass配置方法讲解 转自:http://www.jb51.net/article/78746.htm 就普通的反向代理来讲Nginx的配置还是比较简单的, ...
- Moya https配置方法
准备 iOS做https适配时对服务器是有一定要求的,服务端必须要是一个符合ATS(App Transport Security)要求的HTTPS.简单说要满足以下几个要求: 1.Transpor ...
- Nginx服务器的反向代理proxy_pass配置方法讲解
Nginx的配置还是比较简单的,如: 1 2 3 4 location ~ /* { proxy_pass http://127.0.0.1:8008; } 或者可以 1 2 3 4 loca ...
- 企业网站的SSL签证生产测试以及https配置方法
这一次要做企业网站怎么获得安全的数字证书,没有数字证书的话,在浏览器访问网站的时候会跳出不安全界面,而且钓鱼网站也会让用户进去个假网站,一般企业可以去阿里云去买数字证书,买好之后浏览器便会加载这个数字 ...
- iis7 https配置方法并且http跳转https
操作场景 本文档指导您如何在 IIS 中安装 SSL 证书. 说明: 本文档以证书名称 www.domain.com 为例. 本文档以操作系统 Windows10 为例.由于操作系统的版本不同,详细操 ...
- 烂泥:haproxy学习之https配置
本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb. 在前一段时间,我写了几篇有关学习haproxy的文章.今天我们再来介绍下haproxy ...
- haproxy学习之https配置
haproxy学习之https配置 原文 http://www.cnblogs.com/ilanni/p/4941056.html 如何配置https,以及https在实际生产环境中的应用. ...
- 负载均衡服务之HAProxy https配置、四层负载均衡以及访问控制
前文我们聊了下haproxy的访问控制ACL的配置,回顾请参考https://www.cnblogs.com/qiuhom-1874/p/12817773.html:今天我们来聊一聊haproxy的h ...
- Nginx配置代理gRPC的方法
Nginx配置代理gRPC的方法_nginx_脚本之家 https://www.jb51.net/article/137330.htm
随机推荐
- Multiple Instance Learning
///////////////////////////////////////////推荐学习组////////////////////////////// http://www.robots.ox. ...
- python的==和is区别
Python中: is判断两个标识符是否引自同一个对象 ==判断两个标识符的值是否相等 区别于java: ==判断两个标识符是否引自同一个对象 .equals()判断是否相等 #如果是String ...
- linux grep --我最喜欢的命令~~
转:http://www.cnblogs.com/end/archive/2012/02/21/2360965.html Grep 命令 用法大全 1. 参数: -I :忽略大小写 -c :打印匹配的 ...
- Timing wheel心跳机制
在web服务中,断开空闲连接是一种减少资源浪费的一种手段,由此就有了心跳机制来判断一个连接是否空闲. 一种简单粗暴的方式: 1. 服务端每个连接保存一个最后一次操作的时间戳,每次这个连接对应fd可读时 ...
- BZOJ5058 期望逆序对 【矩乘 + 组合数学 + 树状数组】
题目链接 BZOJ5058 题解 可以发现任意两个位置\(A,B\)最终位置关系的概率是相等的 如果数列是这样: CCCCACCCCBCCCC 那么最终有\(7\)种位置关系 \((A,B)\) \( ...
- java多线程 -- 原子量 变量 CAS
多线程原子性问题的产生和解决 原子变量:在 java.util.concurrent.atomic 包下提供了一些原子变量. 1. volatile 保证内存可见性,可以查看atomic中变量是使用v ...
- Struts2的配置文件中, <package>的作用,<action><result>重名?
问:Struts2的配置文件中, <package>的作用是什么? 答:防止action重名啊,例如前台和后台,总会有很多地方起名重复的! 问:可是访问的时候,不也是访问action吗,能 ...
- NO.2day 操作系统基础
操作系统基础 1.为什么要有操作系统 操作系统为用户程序提供一个更好.更简单.更清晰的计算机模型,并管理刚才提到的所有设备(磁盘.内存.显示器.打印机等).程序员无法把所有的硬件操作细节都了解到,管理 ...
- Tomcat权威指南-读书摘要系列2
2. 配置Tomcat 2.1. 重定向Web应用程序的目录 将工程文件与Tomcat分离 复制conf和webapps文件夹到分离目录: 配置CATALINA_BASE环境变量,值为分离目录: 2. ...
- [Web] Web请求过程之一:HTTP
请求过程: 1.用户在浏览器输入 www.sdjtu.edu.cn 这个URL. 2.浏览器请求 DNS 服务器将这个 URL 解析成对应的 IP 地址. 3.浏览器向 IP 地址对应的这个服务器发起 ...