本实验全部在haproxy1.5.19版本进行测试通过,经过测试1.7.X及haproxy1.3版本以下haproxy配置参数可能不适用,需要注意版本号。

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

1.1 http跳转https

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

1.2 http与https并存

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

1.3 服务器环境准备

node1即haproxy所在服务器的处理

安装依赖
yum install -y openssl openssl-devel readline-devel pcre-devel libssl-dev libpcre3

# 下载安装包,
tar zxf haproxy-1.5.19.tar.gz
cd haproxy-1.5.19

useradd -u 188 -r -d /var/lib/haproxy -s /sbin/nologin haproxy
# 加入支持ssl的编译参数

  1. make TARGET=linux2628 USE_PCRE= USE_OPENSSL= USE_ZLIB= USE_CRYPT_H= USE_LIBCRYPT=
  2. make install PREFIX=/usr/local/haproxy
  3. cp /usr/local/haproxy/sbin/haproxy /usr/sbin/
  4. cp examples/haproxy.init /etc/init.d/haproxy

chmod +x /etc/init.d/haproxy

mkdir /etc/haproxy

mkdir /var/lib/haproxy

# 修改启动脚本(可能会报错)为如下
vim /etc/init.d/haproxy
26 [[ ${NETWORKING} = "no" ]] && exit 0

后端web01(192.168.3.200)服务器apache配置,需要配置虚拟主机域名为:www.chinasoft.com否则无法正常处理

  1. [root@node2 ~]# egrep -v '#|^$' /etc/httpd/conf/httpd.conf
  2. ServerRoot "/etc/httpd"
  3. Listen
  4. Include conf.modules.d/*.conf
  5. User apache
  6. Group apache
  7. ServerAdmin root@localhost
  8. <Directory />
  9. Options FollowSymLinks
  10. AllowOverride none
  11. Allow from all
  12. </Directory>
  13. DocumentRoot "/var/www/html/chinasoft"
  14. <Directory "/var/www">
  15. AllowOverride None
  16. Require all granted
  17. </Directory>
  18. <Directory "/var/www/html/chinasoft">
  19. Options Indexes FollowSymLinks
  20. AllowOverride None
  21. Require all granted
  22. </Directory>
  23. <IfModule dir_module>
  24. DirectoryIndex index.php index.html
  25. </IfModule>
  26. <Files ".ht*">
  27. Require all denied
  28. </Files>
  29. ErrorLog "logs/error_log"
  30. LogLevel warn
  31. <IfModule log_config_module>
  32. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
  33. LogFormat "%h %l %u %t \"%r\" %>s %b" common
  34. <IfModule logio_module>
  35. LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
  36. </IfModule>
  37. CustomLog "logs/access_log" combined
  38. </IfModule>
  39. <IfModule alias_module>
  40. ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
  41. </IfModule>
  42. <Directory "/var/www/cgi-bin">
  43. AllowOverride None
  44. Options None
  45. Require all granted
  46. </Directory>
  47. <IfModule mime_module>
  48. TypesConfig /etc/mime.types
  49. AddType application/x-compress .Z
  50. AddType application/x-gzip .gz .tgz
  51. AddType text/html .shtml
  52. AddOutputFilter INCLUDES .shtml
  53. </IfModule>
  54. AddDefaultCharset UTF-8
  55. <IfModule mime_magic_module>
  56. MIMEMagicFile conf/magic
  57. </IfModule>
  58. EnableSendfile on
  59. IncludeOptional conf.d/*.conf
  60.  
  61. [root@node2 ~]# cat /etc/httpd/conf.d/vhost.conf
  62. NameVirtualHost *:8080
  63. <VirtualHost *:8080>
  64. DocumentRoot /var/www/html/
  65. ServerName 192.168.3.200:8080
  66. </VirtualHost>
  67.  
  68. <Directory "/var/www/html/chinasoft/">
  69. php_admin_value open_basedir "/var/www/html/chinasoft/:/tmp/"
  70. Options Includes ExecCGI FollowSymLinks
  71. AllowOverride All
  72. Order allow,deny
  73. Allow from all
  74. </Directory>
  75. <VirtualHost *:8080>
  76. DocumentRoot /var/www/html/chinasoft/
  77. ServerName www.chinasoft.com:8080
  78. </VirtualHost>

1.4 证书的处理,需要将网站的根证书和key简单的合并在一起:

  1. cat chinasoft.com.pem chinasoft.com.key | tee chinasoft.pem

否则会报错
'bind *:443' : unable to load SSL private key from PEM file

1.5 域名的指向及处理
将www.chinasoft.com指向haproxy负载均衡器所在的服务器IP地址,此处是192.168.3.198

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

2.1 http跳转https配置

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

  1. [root@node1 haproxy]# cat /etc/haproxy/haproxy.cfg
  2. global
  3. log 127.0.0.1 local3 info
  4. chroot /var/lib/haproxy
  5. maxconn 4096
  6. user haproxy
  7. group haproxy
  8. daemon
  9. stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
  10. stats timeout 2m
  11. tune.ssl.default-dh-param 2048
  12.  
  13. defaults
  14. log global
  15. mode http
  16. option httplog
  17. option dontlognull
  18. option http-server-close
  19. option forwardfor except 127.0.0.1
  20. option redispatch
  21. retries 3
  22. option redispatch
  23. maxconn 2000
  24. timeout http-request 10s
  25. timeout queue 1m
  26. timeout connect 10s
  27. timeout client 1m
  28. timeout server 1m
  29. timeout http-keep-alive 10s
  30. timeout check 10s
  31.  
  32. listen admin_stats
  33. bind 0.0.0.0:1080
  34. mode http
  35. option httplog
  36. maxconn 10
  37. stats refresh 30s
  38. stats uri /haproxy?stats
  39. stats auth admin:admin
  40. stats hide-version
  41.  
  42. frontend weblb
  43. bind *:80
  44. acl is_http hdr_beg(host) www.chinasoft.com
  45. redirect scheme https if !{ ssl_fc }
  46. bind *:443 ssl crt /etc/haproxy/chinasoft.pem
  47. use_backend httpserver if is_http
  48.  
  49. backend httpserver
  50. balance source
  51. server web1 192.168.3.200:8080 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3

# 配置好之后先检查语法是否正确
[root@node1 haproxy]# /etc/init.d/haproxy check
Configuration file is valid

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

tune.ssl.default-dh-param 2048因为我们的SSL密钥使用的是2048bit加密,所以在此进行声明。
acl is_http hdr_beg(host) www.chinasoft.com
redirect scheme https if !{ ssl_fc }
bind *:443 ssl crt /etc/haproxy/chinasoft.pem

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

管理页面

2.2 测试http跳转https

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

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

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

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

  1. [root@node1 haproxy]# cat haproxy.cfg
  2. global
  3. log 127.0.0.1 local3 info
  4. chroot /var/lib/haproxy
  5. maxconn 4096
  6. user haproxy
  7. group haproxy
  8. daemon
  9. stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
  10. stats timeout 2m
  11. tune.ssl.default-dh-param 2048
  12.  
  13. defaults
  14. log global
  15. mode http
  16. option httplog
  17. option dontlognull
  18. option redispatch
  19. retries 3
  20. option redispatch
  21. maxconn 2000
  22. timeout connect 10s
  23. timeout client 1m
  24. timeout server 1m
  25. timeout check 10s
  26.  
  27. listen admin_stats
  28. bind 0.0.0.0:1080
  29. mode http
  30. option httplog
  31. maxconn 10
  32. stats refresh 30s
  33. stats uri /haproxy?stats
  34. stats auth admin:admin
  35. stats hide-version
  36.  
  37. frontend weblb
  38. bind *:80
  39. acl is_http hdr_beg(host) www.chinasoft.com
  40. use_backend httpserver if is_http
  41.  
  42. backend httpserver
  43. balance source
  44. server web1 192.168.3.200:8080 maxconn 1024 weight 3 check inter 2000 rise 2 fall 3
  45.  
  46. frontend weblb443
  47. bind *:443 ssl crt /etc/haproxy/chinasoft.pem
  48. acl is_443 hdr_beg(host) www.chinasoft.com
  49. use_backend httpserver443 if is_443
  50.  
  51. backend httpserver443
  52. balance source
  53. server web1 192.168.3.200:8080 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://www.chinasoft.com或者是www.chinasoft.com都会直接跳转到http://www.chinasoft.com,而输入的是https://www.chinasoft.com,则只会跳转到https://www.chinasoft.com。

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

生产环境配置实例:

  1. [root@u05mix05 ~]# cat /etc/haproxy/haproxy.cfg
  2. global
  3. log 127.0.0.1 local3 info
  4. chroot /var/lib/haproxy
  5. maxconn
  6. user haproxy
  7. group haproxy
  8. daemon
  9. stats socket /var/lib/haproxy/haproxy.sock mode level admin
  10. stats timeout 2m
  11.  
  12. defaults
  13. log global
  14. mode http
  15. option httplog
  16. option dontlognull
  17. option http-server-close
  18. option forwardfor except 127.0.0.1
  19. option redispatch
  20. retries
  21. option redispatch
  22. maxconn
  23. timeout http-request 10s
  24. timeout queue 1m
  25. timeout connect 10s
  26. timeout client 1m
  27. timeout server 1m
  28. timeout http-keep-alive 10s
  29. timeout check 10s
  30.  
  31. listen admin_stats
  32. bind 0.0.0.0:
  33. mode http
  34. option httplog
  35. maxconn
  36. stats refresh 30s
  37. stats uri /haproxy?stats
  38. stats auth admin:admin
  39. stats hide-version
  40.  
  41. frontend hs_chinasoft_com
  42. mode http
  43. bind *:
  44. stats uri /haproxy?stats
  45. default_backend hs_chinasoft_com_backend
  46.  
  47. backend hs_chinasoft_com_backend
  48. option forwardfor header X-REAL-IP
  49. option httpchk GET /check
  50. balance roundrobin
  51. server node1 1.1.1.1: check inter rise fall weight
  52.  
  53. frontend hs_chinasoft_info
  54. mode http
  55. bind *:
  56. stats uri /haproxy?stats
  57. default_backend hs_chinasoft_info_backend
  58.  
  59. backend hs_chinasoft_info_backend
  60. option forwardfor header X-REAL-IP
  61. option httpchk GET /check
  62. balance roundrobin
  63. server node1 1.1.1.1: check inter rise fall weight
  64. server node2 1.1.1.2: check inter rise fall weight

haproxy配置基于ssl证书的https负载均衡的更多相关文章

  1. 阿里云域名ssl证书导入aws负载均衡使用

    一 .原因 由于公司战略需求,需要将阿里云的服务器迁移到aws,在迁移过程中,我们需要使用的是aws的负载均衡,可以在EC2的控制台 负载平衡位找到负载均衡.根据业务需求我们使用的是应用程序负载均衡器 ...

  2. SSL证书部署HTTPS站点Apache/Nginx配置

    SSL证书及HTTPS协议 SSL 证书是一种数字证书,它使用 Secure Socket Layer 协议在浏览器和 Web 服务器之间建立一条安全通道,从而实现:1.数据信息在客户端和服务器之间的 ...

  3. Linux下Nginx配置阿里云 SSL证书实现HTTPS访问

    这篇文章主要介绍了nginx配置ssl证书实现https访问的示例 1.服务器系统:Centos 2. 阿里云申请SSL证书 选择“免费版DV SSL”,点击立即购买: 下载证书 列表中找到已签发的证 ...

  4. Nginx配置SSL证书实现https访问「浏览器未认证」

    http 和 https 介绍 http:应用最广泛的一种网络协议,是一个B/S的request和response的标准,用于从www服务器传输超文本到本地浏览器的传输协议. https:以安全为目标 ...

  5. 使用nginx+docker配置https负载均衡

    了解Docker Docker是一个golang编写的开源轻量级的.可移植的.自给自足的容器,Docker主要应用在以下场景: web应用的自动化打包和发布: 自动化测试和持续集成.发布: 在服务型环 ...

  6. office web apps安装部署,配置https,负载均衡(五)配置服务器场

    前提条件:您已经完成了域控制器的配置,拥有域账号,并且已经安装了OWA启动所需要的必要软件: 具体步骤可以参考: office web apps安装部署,配置https,负载均衡(一)背景介绍 off ...

  7. spring boot / cloud (五) 自签SSL证书以及HTTPS

    spring boot / cloud (五) 自签SSL证书以及HTTPS 前言 什么是HTTPS? HTTPS(全称:Hyper Text Transfer Protocol over Secur ...

  8. 在IIS下部署SSL证书实现HTTPS

    在IIS下部署SSL证书实现HTTPS   HTTPS是以安全为目标的HTTP通道,简单讲是HTTP的安全版.谷歌已经制定了一项长远的计划,它的最终目标是将所有通过HTTP协议呈现的网页标为“不安全” ...

  9. 在nginx环境下搭建基于ssl证书的websocket服务转发,wss

    1.证书准备 本地调试,可以安装自签名证书,安装方法参考https本地自签名证书添加到信任证书访问 2.修改配置文件 将上面的配置文件拷贝到conf目录,添加或者修改节点如下 # HTTPS serv ...

随机推荐

  1. JavaScript -- throw、try 和 catch

    try 语句测试代码块的错误. catch 语句处理错误. throw 语句创建自定义错误. 很想java哦. <!DOCTYPE html> <html> <head& ...

  2. bzoj1345 序列问题

    题意: 给你一个序列,长度为n.你需要进行n - 1次操作,每次合并两个相邻的数,代价是max,合并后成为max,求最小代价. n <= 1e6 解: 有个显然的做法是nlogn的,显然不行.. ...

  3. 【模板】kmp

    引理:当计算第 \(i\) 位的失配指针时,若 \(j_0\) 是一个候选条件,那么小于 \(j_0\) 的最大候选条件是 \(fail[j_0]\). 证明:反证法.假设存在 \(j_1\),使得\ ...

  4. centos7 上配置Javaweb---MySQL的安装与配置、乱码解决

    上一篇谢了关于jdk和tomcat的安装.今天先更新一下有用的. 1. 不用关闭防火墙,把80端口添加进防火墙的开放端口. firewall-cmd --zone=public --add-port= ...

  5. 洛谷 P1083 借教室

    传送门:Probem 1083 https://www.cnblogs.com/violet-acmer/p/9721160.html 一.暴力简述 首先我们不难看出,这道题--并不是一道多难的题,因 ...

  6. CentOS6.7下Ansible部署

    Ansible是一种集成IT系统的配置管理, 应用部署, 执行特定任务的开源平台. 它基于Python语言实现, 部署只需在主控端部署Ansible环境, 被控端无需安装代理工具, 只需打开SSH, ...

  7. Tensorflow-gpu版本安装

    目录 服务器选型 NVIDIA GPU驱动安装 cuda和cudnn的安装 cuda安装 cudnn的安装 tensorflow-gpu安装 最近给公司部署一套深度学习相关的环境,以tensorflo ...

  8. python 3字符编码

    python 3字符编码 官方链接:http://legacy.python.org/dev/peps/pep-0263/ 在Python2中默认是ascii编码,Python3是utf-8编码 在p ...

  9. NFS无法启动解决方式

    今天一台挂载nfs磁盘的服务器出现异常,数据不能写入,执行 df -h 卡住不动. 登录nfs server查看发现nfs为启动. [root@server10-13 web]# exportfs [ ...

  10. Spark记录-SparkSQL相关学习

    $spark-sql  --help  查看帮助命令 $设置任务个数,在这里修改为20个 spark-sql>SET spark.sql.shuffle.partitions=20; $选择数据 ...