Haproxy route and rewrite based on URI path

风流意气都作罢 提交于 2019-11-28 08:28:50
Rhim

If I understand you correctly, replace the example below:

reqrep ^([^\ ]\*)\ /([-.0-9A-Za-z]\*)/([a-zA-Z]\*)/(.\*)  \1\ /\3-\2/\4

http://cbonte.github.io/haproxy-dconv/configuration-1.5.html#reqrep

reqrep search string [{if | unless} cond]

You have no spaces.

The answer above is a single fix that doesn't explain the root of the problem. The problem is that many assume we're parsing a complete URL or only the PATH component, when we're actually parsing/rewriting HTTP headers.

For example:

curl -iv https://stackoverflow.com/questions/24784517/haproxy-route-and-rewrite-based-on-uri-path
*   Trying 151.101.65.69...
> GET /questions/24784517/haproxy-route-and-rewrite-based-on-uri-path HTTP/1.1
> Host: stackoverflow.com
> User-Agent: curl/7.54.0
> Accept: */*

The goal is to rewrite GET /some_path HTTP/1.1 to GET /some_other_path HTTP/1.1

I want to clarify that the solution above works because it takes into account the HTTP verb in the match and replace. ^([^\ ]\*)\ (.*) to capture the verb and use it in the replacement pattern. \1 \2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!