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

Regular expression to restrict URL in HAProxy

· 52CE 我爱测

I had successfully restricted an URL in my web app to certain trusted IP, using below-mentioned config in haproxy config acl trusted-ip src -f /etc/haproxy/whitelist.lst acl protected-page url /abc /abc/ acl allowed-page url /abc…

问题原文

原帖链接 · Arjun K R · CC BY-SA 4.0

I had successfully restricted an URL in my web app to certain trusted IP, using below-mentioned config in haproxy config

acl trusted-ip src -f /etc/haproxy/whitelist.lst    
acl protected-page url /abc /abc/    
acl allowed-page url /abc/api/
http-request deny if protected-page !allowed-page !trusted-ip

I want all users to access "/abc/api" URL, while "/abc" will be accessible only to Trusted IP. The problem here is if anyone from non-trusted IP enters "/abc?something" the URL "/abc" opens, in order to avoid this, I modified the config to

acl trusted-ip src -f /etc/haproxy/whitelist.lst
acl protected-page url_reg ^(?!\/abc\/api).*$
http-request deny if protected-page !trusted-ip

Now I was able to solve the above-mentioned problem, but the "/abc/api" is not accessible to anyone. Any help will be appreciated.

已采纳解答

原帖链接 · nuster cache server · CC BY-SA 4.0

Use path instead of url

This extracts the request's URL path, which starts at the first slash and ends before the question mark (without the host part). A typical use is with prefetch-capable caches, and with portals which need to aggregate multiple information from databases and keep them in caches. Note that with outgoing caches, it would be wiser to use "url" instead. With ACLs, it's typically used to match exact file names (eg: "/login.php"), or directory parts using the derivative forms. See also the "url" and "base" fetch methods.

ACL derivatives :
  path     : exact string match
  path_beg : prefix match
  path_dir : subdir match
  path_dom : domain match
  path_end : suffix match
  path_len : length match
  path_reg : regex match
  path_sub : substring match

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

资料来源与适用范围

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

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

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