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

Regex to Identify an URL Parameter Not Knowing Which Parameter It Will Be

· 52CE 我爱测

So I've been working on building out an haproxy cluster (1.5.x) and hit a snag where for a particular webapp I'm wanting to pitch API GETs to a read-only app cluster and the PUTs into a much smaller write cluster. I've tried usin…

问题原文

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

So I've been working on building out an haproxy cluster (1.5.x) and hit a snag where for a particular webapp I'm wanting to pitch API GETs to a read-only app cluster and the PUTs into a much smaller write cluster.

I've tried using a path_reg, which doesn't seem to want to match anything beyond a filename. The urlp derivatives seem to be what I should be using, but their proper usage has eluded me. The one tricky nuance with the URL parameters I'm trying to capture and differentiate all begin with either a "get" or "set" something...but the parameters do not have values. From what I've understood or attempted to, the urlp derivatives generally expect there to be a value associated with a parameter, so it seems like I can't simply use something like:

acl api_gets urlp_reg(get) (get?){1}

from an URL with parameters like:

/api.foo?apikey=stuff&param1=foo&getsomething&param2=bar

Similarly, path_reg seems to not pick up anything beyond a filename, so rudimentary testing of trying to match the following as a proof of concept were also fruitless:

acl path_sanity_check path_reg ^\/api\.foo\?apikey.*

Whereas simply making the regex pattern ^/api.foo will pick up where the requests are being sent to.

Should I be using urlp_reg for this even if the parameter(s) don't have values? What alternatives are there if not using an urlp derivative?

已采纳解答

原帖链接 · Michael - sqlbot · CC BY-SA 4.0

Path fetches the path. The query isn't part of the path. capture.req.uri fetches the full URI.

Although sloppy, you could match that with something like this:

acl foo capture.req.uri -m reg [&\?]get

The character class escaping might require adjustment, but the idea is to find either ?get or &get in the request URI.

If you are building a new cluster, use HAProxy 1.6, which has a number of new, useful fetches and converters, plus optional Lua integration for more complicated logic.

urlp_reg is the wrong solution, because the regex match is for the value of the parameter, not its name... and you don't have values.

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

资料来源与适用范围

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

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

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