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

Require a HTTP-authentication only for connections from outside my LAN in HAProxy

· 52CE 我爱测

I have HAProxy configured to require a HTTP-authentication for a single frontend. It looks like this userlist Users group G1 user XXX password YYY frontend public bind :::80 v4v6 bind :::443 v4v6 ssl crt /etc/ssl/snakeoil.pem # A…

问题原文

原帖链接 · speendo · CC BY-SA 4.0

I have HAProxy configured to require a HTTP-authentication for a single frontend. It looks like this

userlist Users
    group G1
    user XXX password YYY


frontend public
    bind :::80 v4v6
    bind :::443 v4v6 ssl crt /etc/ssl/snakeoil.pem

    # Authentication
    acl ValidUser http_auth(Users)
    http-request auth realm mysite if !ValidUser

    option forwardfor except 127.0.0.1
    use_backend othersite if { path_beg /othersite/ }
    default_backend thesite

backend thesite
    acl needs_scheme req.hdr_cnt(X-Scheme) eq 0

    reqrep ^([^\ :]*)\ /(.*) \1\ /\2
    reqadd X-Scheme:\ https if needs_scheme { ssl_fc }
    reqadd X-Scheme:\ http if needs_scheme !{ ssl_fc }
    option forwardfor
    server thesite1 127.0.0.1:5000
    errorfile 503 /etc/haproxy/errors/503-no-thesite.http

backend othersite
    reqrep ^([^\ :]*)\ /othersite/(.*)     \1\ /\2
    server othersite1  127.0.0.1:8080
    errorfile 503 /etc/haproxy/errors/503-no-othersite.http

Now I want to request a HTTP-authentication only for connections from outside my LAN.

I tried to replace the line

http-request auth realm mysite if !ValidUser

with

http-request auth realm mysite unless ValidUser || { hdr_beg(host) -i 192.168.0. }

but that didn't work.

How can this task be accomplished?

[edit]
Thanks to this question (HAProxy basic auth except from specific IP) I got my configuration working for one specific IP:

# Authentication
acl ValidOctoPrintUser http_auth(Users)
# Exclude one IP from Authentication
acl InternalIP src 192.168.0.XXX
http-request auth realm octoprint if !InternalIP !ValidOctoPrintUser

However, I don't know yet how to exclude all IPs starting with 192.168.0. from the authentication

已采纳解答

原帖链接 · XANi · CC BY-SA 4.0

hdr_beg(host) -i 192.168.0. is matching Header, not client's IP.

You want to use src, like

acl allowed_ip src 192.168.0.0/24

src matches by network's CIDR so you can match whole networks if needed

also note that you can OR rules by just defining rule named same, so say acl allowed_ip src 192.168.1.0/24 acl allowed_ip src 192.168.5.0/24

will match IPs from both 192.168.1.0/24 and 192.168.1.0/24 networks.

Only time when you want to do that on header is when you have another proxy in front and want to match IP in X-Forwarded-For

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

资料来源与适用范围

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

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

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