大网数据广告活动
服务器运维问答

Multiple forwardfor commands in HAProxy config

· 52CE 我爱测

In HAProxy, I would like to forwardfor everything except a few networks like below (multiple networks) frontend main bind myip:5356-60000 mode http option http_proxy option forwardfor except 127.0.0.0/8 #1st network option forwar…

问题原文

原帖链接 · Evyatar Saias · CC BY-SA 4.0

In HAProxy, I would like to forwardfor everything except a few networks like below (multiple networks)

frontend  main
    bind         myip:5356-60000
    mode                 http
    option               http_proxy
    option forwardfor    except 127.0.0.0/8 #1st network
    option forwardfor    except 1.1.1.1/32 #2nd network
    option forwardfor    except 2.2.2.2/32 #3rd network
    option forwardfor    except 3.3.3.3/32 #4th network
    maxconn              950
    timeout              client  30s
    default_backend      mybackendserver

This doesn't work, instead of not forwarding ALL of the specified networks it only works on the last one (the 4th network).

Each option forwardfor except my-network-here command overwrites the previous one instead of appending them. How can I achieve forwarding everything except a whitelist of multiple networks?

已采纳解答

原帖链接 · Evyatar Saias · CC BY-SA 4.0

I ended up using a somewhat hacky solution, it was not my first choice but it works for my needs. In the haproxy config I am using an acl whitelist that contains all the ips that I do not wish to forwardfor. If the request comes from an ip that exists in the whitelist, haproxy will use a second backend that is identical to the first one, except that it does not forwardfor. I basically moved the forwardfor option to the backend portion instead of the frontend.

so,

    frontend  main
        bind         myip:5356-60000
        mode                 http
        option               http_proxy
        maxconn              950
        timeout              client  30s
        acl white_list_noforward src 1.1.1.1 2.2.2.2 3.3.3.3 etc..
        #explanation: if the ip is not found in the whitelist, use the backend_that_forwards, else, and the ip is in the whitelist use the backend_that_DOESNT_forward 
        use_backend backend_that_forwards if !white_list_noforward
        use_backend backend_that_DOESNT_forward if white_list_noforward  
        #default to the backend that forwards just in case something goes wrong
        default_backend      use_backend backend_that_forwards

   backend_that_forwards #forwards client ip
        mode        http
        option forwardfor    except 127.0.0.0/8 # <-- THIS forwards the real client ip except 127.0.0.0/8
        balance     roundrobin
        timeout     connect 5s
        timeout     server  5s
        server      static 127.0.0.1:80 # same server for both backends

  backend_that_DOESNT_forward #DOES NOT forward the client-ip (No option forwardfor is used here), used to handle all requests coming in from ips that I do not wish to forward for
       mode        http
       balance     roundrobin
       timeout     connect 5s
       timeout     server  5s
       server      static 127.0.0.1:80 # same server for both backends

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2021-04-06)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。