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

HAProxy 1.7 Redirect/Rewrite specific domains with Map Files

· 52CE 我爱测

I am new to HAProxy and I would like to redirect requests in map files defined domains. An short example: In my redirect.map file: abc.com xyz.com def.ru idk.com So if the user sends a request to domain abc.com or def.ru he will …

问题原文

原帖链接 · Rivest · CC BY-SA 3.0

I am new to HAProxy and I would like to redirect requests in map files defined domains. An short example: In my redirect.map file:

abc.com xyz.com
def.ru idk.com

So if the user sends a request to domain abc.com or def.ru he will be redirected to the defined domain. But if he requests asd.com, no redirect should be done. My aim is to define those redirects in a map file so it is easy to change.

Next i would like to do the same with uri rewrites. Something like:

http-request set-uri X if Y from rewrite.map

So my Map file will look like:

 (my X variable)    (my Y variable)
/abc/def/specific  www.mydomain.com/specific

Do I even have the opportunity to do this with HAProxy?

已采纳解答

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

http-request redirect location https://%[hdr(host),map(/etc/haproxy/rewrite.map)]%[capture.req.uri] code 301 if { hdr(host),map(/etc/haproxy/rewrite.map) -i -m found }

The hdr(x) fetch reads the request header x and returns the value found. (Technically, it reads the last occurrence of this header, but the Host header should only appear once.)

The map converter takes the value preceding it (connected with ,), looks it up in the map table, and returns the result.

If no match is found, no result is returned, so the -m found test returns no result and the rule is not matched. (-i makes the lookup case-insensitive).

If the rule is matched, we build a new URL for the Location response header, using the literal string https:// (change this to http if appropriate) + the rewritten hostname from the map file + the original request URI, which begins with the leading / and includes the path and query string, preserving the entire request URI.

If you don't want to preserve the original URI, but just send everything to the main page of the target site, then remove %[capture.req.uri] from the expression.

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

资料来源与适用范围

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

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

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