设为首页 收藏本站
查看: 1994|回复: 0

[经验分享] haproxy redirect 重定向

[复制链接]
发表于 2015-11-20 11:05:15 | 显示全部楼层 |阅读模式
redirect location <loc> [code <code>] <option> [{if | unless} <condition>]
redirect prefix   <pfx> [code <code>] <option> [{if | unless} <condition>]
redirect scheme   <sch> [code <code>] <option> [{if | unless} <condition>]
Return an HTTP redirection if/unless a condition is matched
返回一个HTTP redirection  if/unless 一个条件匹配,用于下面章节。
May be used in sections :   defaults | frontend | listen | backend
no    |    yes   |   yes  |   yes
If/unless the condition is matched, the HTTP request will lead to a redirect
response. If no condition is specified, the redirect applies unconditionally.
If/unless 一个条件被匹配, HTTP 请求会指向一个重定向的响应,如果条件被指定,重定向无条件地适用
Arguments :
<loc>  location     With &quot;redirect location&quot;, the exact value in <loc> is placed into
the HTTP &quot;Location&quot; header. When used in an &quot;http-request&quot; rule,
<loc> value follows the log-format rules and can include some
dynamic values (see Custom Log Format in section 8.2.4).
重定向的文职, 准确的值 在<loc> 被放置到HTTP &quot;Location&quot; header.
当用在一个 &quot;http-request&quot; 规则,<loc> 值跟着一个log-format 规则可以包含一些动态的值
<pfx> 前缀     With &quot;redirect prefix&quot;, the &quot;Location&quot; header is built from the
concatenation of <pfx> and the complete URI path, including the
query string, unless the &quot;drop-query&quot; option is specified (see
below). As a special case, if <pfx> equals exactly &quot;/&quot;, then
nothing is inserted before the original URI. It allows one to
redirect to the same URL (for instance, to insert a cookie). When
used in an &quot;http-request&quot; rule, <pfx> value follows the log-format
rules and can include some dynamic values (see Custom Log Format
in section 8.2.4).
重定向前缀, “Location&quot; header 被构造从 <pfx>关联的事务和完整的URI路径,
包括查询字符串,除非  &quot;drop-query&quot; 选项被指定。 作为一个特别的例子,
如果<pfx> = '/',那么没有什么会被插入在原始的URI.允许重定向到同样的URL


<sch>     With &quot;redirect scheme&quot;, then the &quot;Location&quot; header is built by
concatenating <sch> with &quot;://&quot; then the first occurrence of the
&quot;Host&quot; header, and then the URI path, including the query string
unless the &quot;drop-query&quot; option is specified (see below). If no
path is found or if the path is &quot;*&quot;, then &quot;/&quot; is used instead. If
no &quot;Host&quot; header is found, then an empty host component will be
returned, which most recent browsers interpret as redirecting to
the same host. This directive is mostly used to redirect HTTP to
HTTPS. When used in an &quot;http-request&quot; rule, <sch> value follows
the log-format rules and can include some dynamic values (see
Custom Log Format in section 8.2.4).
<code>    The code is optional. It indicates which type of HTTP redirection
is desired. Only codes 301, 302, 303, 307 and 308 are supported,
with 302 used by default if no code is specified. 301 means
&quot;Moved permanently&quot;, and a browser may cache the Location. 302
means &quot;Moved permanently&quot; and means that the browser should not
cache the redirection. 303 is equivalent to 302 except that the
browser will fetch the location with a GET method. 307 is just
like 302 but makes it clear that the same method must be reused.
Likewise, 308 replaces 301 if the same method must be used.
<option>  There are several options which can be specified to adjust the
expected behaviour of a redirection :
- &quot;drop-query&quot;
When this keyword is used in a prefix-based redirection, then the
location will be set without any possible query-string, which is useful
for directing users to a non-secure page for instance. It has no effect
with a location-type redirect.
- &quot;append-slash&quot;
This keyword may be used in conjunction with &quot;drop-query&quot; to redirect
users who use a URL not ending with a '/' to the same one with the '/'.
It can be useful to ensure that search engines will only see one URL.
For this, a return code 301 is preferred.
- &quot;set-cookie NAME[=value]&quot;
A &quot;Set-Cookie&quot; header will be added with NAME (and optionally &quot;=value&quot;)
to the response. This is sometimes used to indicate that a user has
been seen, for instance to protect against some types of DoS. No other
cookie option is added, so the cookie will be a session cookie. Note
that for a browser, a sole cookie name without an equal sign is
different from a cookie with an equal sign.
- &quot;clear-cookie NAME[=]&quot;
A &quot;Set-Cookie&quot; header will be added with NAME (and optionally &quot;=&quot;), but
with the &quot;Max-Age&quot; attribute set to zero. This will tell the browser to
delete this cookie. It is useful for instance on logout pages. It is
important to note that clearing the cookie &quot;NAME&quot; will not remove a
cookie set with &quot;NAME=value&quot;. You have to clear the cookie &quot;NAME=&quot; for
that, because the browser makes the difference.
Example: move the login URL only to HTTPS.
acl clear      dst_port  80
acl secure     dst_port  8080
acl login_page url_beg   /login
acl logout     url_beg   /logout
acl uid_given  url_reg   /login?userid=[^&]+
acl cookie_set hdr_sub(cookie) SEEN=1
redirect prefix   https://mysite.com set-cookie SEEN=1 if !cookie_set
redirect prefix   https://mysite.com           if login_page !secure
redirect prefix   http://mysite.com drop-query if login_page !uid_given
redirect location http://mysite.com/           if !login_page secure
redirect location / clear-cookie USERID=       if logout
Example: send redirects for request for articles without a '/'.
acl missing_slash path_reg ^/article/[^/]*$
redirect code 301 prefix / drop-query append-slash if missing_slash
Example: redirect all HTTP traffic to HTTPS when SSL is handled by haproxy.
redirect scheme https if !{ ssl_fc }
重定向所有的HTTP 到HTTPS
Example: append 'www.' prefix in front of all hosts not having it
http-request redirect code 301 location www.%[hdr(host)]%[req.uri] \
unless { hdr_beg(host) -i www }
追加www. 前缀在所有的主机名前 没有http-request 重定向代码301 location www.%[hdr(host)]%[req.uri]
除非 { hdr_beg(host) -i www }
See section 7 about ACL usage.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-141465-1-1.html 上篇帖子: haproxy hdr_beg(host) 只匹配开头 下篇帖子: HAproxy 虚拟主机
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表