搜索了很多教程都没理清楚思路,还是官方讲的清楚。传送门:[https://www.haproxy.com/support/technical-notes/an-0007-en-rewriting-http-requests/](https://www.haproxy.com/support/technical-notes/an-0007-en-rewriting-http-requests/) ## 使用方法 为了重写请求,需要使用关键词reqrep和reqirep,语法如下: ``` reqrep [{if | unless} ] reqirep [{if | unless} ] (忽略大小写) ``` `` is the regular expression applied both to the HTTP headers and to the request. This is an extended regular expression. Grouped parentheses are supported, and the backslash character is not required. All spaces and known separators must be escaped using the backslash “\”. The template is then applied to the entire line. `` is the entire line to be added. All spaces and known separators must be escaped using the backslash “\”. You can refer to groups on corresponding patterns by using “\N”, where “N” is an integer between 0 and 9. `` is an optional corresponding condition produced from an ACL. Thus you can ignore this rule when the other conditions are not met. Any line with a correspondence extended by a regular expression in the “search” argument of a request (in both the request and the header) will be completely replaced by the “string” argument. This is most commonly used to rewrite URLs or domain names in the “host” field of headers, for instance. ### 重点: “reqrep”关键词严格区分大小写,而“repirep”是大小写不敏感的。 ``条件只适用于v3.5.x及以上版本。 ## 7层负载均衡配置示例 ``` ######## The first public address as seen by the clients frontend frt bind 10.0.32.10:80 # address:port to listen to mode http log global # use global log parameters option httplog # Enable HTTP logging # Replace the “jpg” folder with the “images” folder reqrep ^([^\ ]*)\ /jpg/(.*) \1\ /images/\2 # Replace any host name in the header with “www.mysite.com” reqirep ^Host:\ Host:\ www.mysite.com maxconn 4000 # max conn per instance timeout client 25s # maximum client idle time (ms) default_backend bck # send everything to this backend by default ####### This backend manages the servers and the load balancing algorithm backend bck balance roundrobin # roundrobin | source | uri | leastconn mode http log global # use global log parameters option httplog # Enable HTTP logging cookie SERVERID insert indirect nocache # provide persistence with cookie option httpchk HEAD / # how to check those servers option forwardfor except 127.0.0.1/8 # add X-Forwarded-For except local fullconn 4000 # dynamic limiting below timeout server 25s # max server’s response time (ms) server srv1 10.0.32.101:80 cookie s1 weight 10 maxconn 100 check inter 1000 fall 3 server srv2 10.0.32.102:80 cookie s2 weight 10 maxconn 100 check inter 1000 fall 3 ``` Loading... 搜索了很多教程都没理清楚思路,还是官方讲的清楚。传送门:[https://www.haproxy.com/support/technical-notes/an-0007-en-rewriting-http-requests/](https://www.haproxy.com/support/technical-notes/an-0007-en-rewriting-http-requests/) ## 使用方法 为了重写请求,需要使用关键词reqrep和reqirep,语法如下: ``` reqrep <search> <string> [{if | unless} <cond>] reqirep <search> <string> [{if | unless} <cond>] (忽略大小写) ``` `<search>` is the regular expression applied both to the HTTP headers and to the request. This is an extended regular expression. Grouped parentheses are supported, and the backslash character is not required. All spaces and known separators must be escaped using the backslash “\”. The template is then applied to the entire line. `<string>` is the entire line to be added. All spaces and known separators must be escaped using the backslash “\”. You can refer to groups on corresponding patterns by using “\N”, where “N” is an integer between 0 and 9. `<cond>` is an optional corresponding condition produced from an ACL. Thus you can ignore this rule when the other conditions are not met. Any line with a correspondence extended by a regular expression in the “search” argument of a request (in both the request and the header) will be completely replaced by the “string” argument. This is most commonly used to rewrite URLs or domain names in the “host” field of headers, for instance. ### 重点: “reqrep”关键词严格区分大小写,而“repirep”是大小写不敏感的。 `<cond>`条件只适用于v3.5.x及以上版本。 ## 7层负载均衡配置示例 ``` ######## The first public address as seen by the clients frontend frt bind 10.0.32.10:80 # address:port to listen to mode http log global # use global log parameters option httplog # Enable HTTP logging # Replace the “jpg” folder with the “images” folder reqrep ^([^\ ]*)\ /jpg/(.*) \1\ /images/\2 # Replace any host name in the header with “www.mysite.com” reqirep ^Host:\ Host:\ www.mysite.com maxconn 4000 # max conn per instance timeout client 25s # maximum client idle time (ms) default_backend bck # send everything to this backend by default ####### This backend manages the servers and the load balancing algorithm backend bck balance roundrobin # roundrobin | source | uri | leastconn mode http log global # use global log parameters option httplog # Enable HTTP logging cookie SERVERID insert indirect nocache # provide persistence with cookie option httpchk HEAD / # how to check those servers option forwardfor except 127.0.0.1/8 # add X-Forwarded-For except local fullconn 4000 # dynamic limiting below timeout server 25s # max server’s response time (ms) server srv1 10.0.32.101:80 cookie s1 weight 10 maxconn 100 check inter 1000 fall 3 server srv2 10.0.32.102:80 cookie s2 weight 10 maxconn 100 check inter 1000 fall 3 ``` 最后修改:2021 年 09 月 26 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 2 如果觉得我的文章对你有用,请随意赞赏