haproxy基础配置

global
	log /dev/log	local0
	log /dev/log	local1 notice
	chroot /var/lib/haproxy
    maxconn     10000
    maxsessrate 10000
	stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners
	stats timeout 30s
	user haproxy
	group haproxy
	daemon

	# Default SSL material locations
	ca-base /etc/ssl/certs
	crt-base /etc/ssl/private

	# See: https://ssl-config.mozilla.org/#server=haproxy&server-version=2.0.3&config=intermediate
        ssl-default-bind-ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384
        ssl-default-bind-ciphersuites TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256
        ssl-default-bind-options ssl-min-ver TLSv1.2 no-tls-tickets
    tune.ssl.default-dh-param 2048

defaults
	log	global
	mode	http
	option	httplog
	option	dontlognull
        timeout connect 5000
        timeout client  50000
        timeout server  50000
	errorfile 400 /etc/haproxy/errors/400.http
	errorfile 403 /etc/haproxy/errors/403.http
	errorfile 408 /etc/haproxy/errors/408.http
	errorfile 500 /etc/haproxy/errors/500.http
	errorfile 502 /etc/haproxy/errors/502.http
	errorfile 503 /etc/haproxy/errors/503.http
	errorfile 504 /etc/haproxy/errors/504.http

listen stats
    bind 0.0.0.0:1080
    mode http
    option  httplog
    log     global
    stats enable
    stats refresh 30s
    stats uri /admin?stats
    stats realm Private lands
    stats auth admin:jt-admin
    stats hide-version
    stats admin if TRUE

backend per-ip
    stick-table type ip size 50k expire 120m store gpc0,http_req_rate(1s)

frontend http_80_in
    #bind 0.0.0.0:80
    bind *:80
    mode http
    option httpclose
    option forwardfor   #将客户端真实ip加到HTTP Header中供��务器读取
    option forwardfor header X-Client-IP  # 将xff的ip转成获取客户端一手ip,只对代理有效
    option forwardfor header X-Real-IP  # 转发real_ip值
    option http-server-close
    # option redispatch
    # rate-limit sessions 10000

    # 禁止favicon图标请求
    acl favicon path_beg -i /favicon.ico
    http-request deny if favicon
    
    # 图片
    acl aws_img_host hdr(host) -i image.test.cn
    use_backend aws-img if aws_img_host

    # https
    acl app_letsencrypt path_beg /.well-known/acme-challenge/
    use_backend letsencrypt_backend if app_letsencrypt

    acl project_host hdr_end(host) -i api.test.com
    use_backend work if project_host
    
    default_backend work

frontend https
    bind *:443 ssl crt /etc/haproxy/ssl/api.test.com.pem

    mode http
    http-request add-header X-Forwarded-Proto https
    # http-request add-header:\ https
    option httpclose
    option forwardfor
    option http-server-close
    # capture request header Host len 255
    # capture request header User-Agent len 255
    # capture request header Referer len 255
    # capture request header Cookie len 255

    # websocket
    acl hdr_connection_upgrade hdr(Connection)  -i upgrade
    acl hdr_upgrade_websocket  hdr(Upgrade)     -i websocket
    use_backend websockets if hdr_connection_upgrade hdr_upgrade_websocket
    
    # 项目
    acl project_host_https hdr_end(host) -i api.test.com
    use_backend work if project_host_https
    
    default_backend work

backend work
    balance roundrobin
    cookie SERVERID
    server work-01 127.0.0.1:80 cookie 3 check inter 2000 rise 3 fall 3 weight 2
    server work-02 127.0.0.2:80 cookie 2 check inter 2000 rise 3 fall 3 weight 2

backend letsencrypt_backend
    http-request set-header Host letsencrypt.requests
    dispatch 127.0.0.1:8000

backend aws-img
    balance roundrobin
    cookie SERVERID
    http-request set-header Host s3.cn-north-1.amazonaws.com.cn
    http-request replace-path (.*) /image\1
    server aws s3.cn-north-1.amazonaws.com.cn

# 测试命令
sudo haproxy -c -f haproxy.cfg
# 重启命令
sudo service haproxy reload
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值