haproxy 反向代理 tomcat (https、负载均衡)

背景:

    情况是这样的,我们要支撑高并发业务,需要多个web服务器来支持,如果一台机器只部署一个tomcat的话,那资源没有办法充分利用,所以我们的办法是在一台物理机部署数十个tomcat,前端使用haproxy做负载均衡,并且网站需要https访问,所以证书需要在haproxy中配置。


部署:

1、haproxy的配置

#---------------------------------------------------------------------
# Example configuration for a possible web application.  See the
# full configuration options online.
#
#   http://haproxy.1wt.eu/download/1.4/doc/configuration.txt
#
#---------------------------------------------------------------------
#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
    # to have these messages end up in /var/log/haproxy.log you will
    # need to:
    #
    # 1) configure syslog to accept network log events.  This is done
    #    by adding the '-r' option to the SYSLOGD_OPTIONS in
    #    /etc/sysconfig/syslog
    #
    # 2) configure local2 events to go to the /var/log/haproxy.log
    #   file. A line like the following can be added to
    #   /etc/sysconfig/syslog
    #
    #    local2.*                       /var/log/haproxy.log
    #
    log         127.0.0.1 local2
    chroot      /var/lib/haproxy
    pidfile     /var/run/haproxy.pid
    maxconn     400000
    user        haproxy
    group       haproxy
    daemon
    tune.ssl.default-dh-param  2048
    # turn on stats unix socket
    stats socket /var/lib/haproxy/stats
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
    mode                    http
    log                     global
    option                  httplog
    option                  dontlognull
    option http-server-close
    option forwardfor       except 127.0.0.0/8
    option                  redispatch
    retries                 3
    option                  httpclose
    timeout http-request    10s
    timeout queue           1m
    timeout connect         10s
    timeout client          1m
    timeout server          1m
    timeout http-keep-alive 10s
    timeout check           10s
    stats enable
    stats hide-version
    stats uri     /haproxy?status
    stats realm   Haproxy\ Statistics
    stats auth    admin:admin123
#---------------------------------------------------------------------
# main frontend which proxys to the backends
#---------------------------------------------------------------------
frontend  wzlinux_ssl
      bind *:80
      bind *:443 ssl crt /etc/haproxy/wzlinux.pem
      mode http
      default_backend  wzlinuxs
backend wzlinuxs
    mode http
    balance     roundrobin
    option forwardfor
#    option httpchk HEAD / HTTP/1.1\r\nHost:localhost
    server      tomcat01  127.0.0.1:8080 check inter 15000 rise 2 fall 4 weight 1
    server      tomcat02  127.0.0.1:8081 check inter 15000 rise 2 fall 4 weight 1
    server      tomcat03  127.0.0.1:8082 check inter 15000 rise 2 fall 4 weight 1
    server      tomcat04  127.0.0.1:8083 check inter 15000 rise 2 fall 4 weight 1
    server      tomcat05  127.0.0.1:8084 check inter 15000 rise 2 fall 4 weight 1
    server      tomcat06  127.0.0.1:8085 check inter 15000 rise 2 fall 4 weight 1
    server      tomcat07  127.0.0.1:8086 check inter 15000 rise 2 fall 4 weight 1
#    http-request set-header X-Forwarded-Port %[dst_port]
#    http-request add-header X-Forwarded-Proto https if { ssl_fc }


2、tomcat的配置设定

    因为tomcat日志需要知道真正的来源IP是什么,所以默认的是不满足要求的,我们需要修改日志格式的内容如下。

<Host name="localhost"  appBase="/home/webapps"
            unpackWARs="true" autoDeploy="true">
            
     <Valve className="org.apache.catalina.valves.AccessLogValve" 
            directory="/var/log/tomcat"
            prefix="wzlinux." suffix=".txt"
            pattern="%{X-Forwarded-For}i %l %u %t &quot;%r&quot; %s %b" />
</Host>



                              wKioL1mSXbfwFstqAACmb4CVfN4502.jpg


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值