使用路径前缀规则时如何使用 Traefik 重写路径? [英] How to rewrite paths with Traefik when using path prefix rules?
问题描述
我的 WordPress 的 Traefik 配置包含以下 docker-labels:
My Traefik config for WordPress contains the following docker-labels:
- "traefik.backend=wordpress"
- "traefik.docker.network=web"
- "traefik.frontend.rule=Host:MyHostName.net;PathPrefix:/blog"
- "traefik.enable=true"
- "traefik.port=80"
现在请求网址https://MyHostName/blog";似乎到达了似乎返回重定向到https://MyHostName/wp-admin..."的服务.
Now requesting the url "https://MyHostName/blog" seems to reach the service which seems to return a redirect to "https://MyHostName/wp-admin...".
我不能使用子域.
我该如何解决这个问题?
How can I solve this?
更新 0
首先应该添加过滤器PathPrefixStrip:/blog";删除/blog"将请求转发到服务时的前缀.正确吗?
First thing to do should be adding the Filter "PathPrefixStrip:/blog" to remove the "/blog" prefix when forwarding the request to the service. Correct?
但是我如何修改(例如)重定向请求以添加前缀/blog"?到重定向网址?
But how do I modify (for example) a redirect request to add the prefix "/blog" to the redirect URL?
更新 1
在 https://github.com/containous/traefik/issues/985 我的问题是讨论过的";并且似乎合并了一个解决方案(https://github.com/containous/traefik/pull/1442).
At https://github.com/containous/traefik/issues/985 my question is "discussed" and a solution seems to be merged (https://github.com/containous/traefik/pull/1442).
简而言之:剥离的前缀将被添加为相应的标头(X-Forwarded-Prefix).
In short: Stripped prefixes will be added as the respective header (X-Forwarded-Prefix).
我会检查并在此处写下结果.
I will check that and write down the results here.
其他资源:
更新 2
现在我创建了一个如下所示的请求:
Now I created a request looking like this:
https://MYHOSTNAME/blog
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: de,en-US;q=0.7,en;q=0.3
Connection: keep-alive
Cookie: ocuvhr6ala6i=d2cd9020839889a752b4375a63dedad0; oc_sessionPassphrase=qJu13Q%2FlAoSsv5b0qC18Re%2BcrcML6o32c2XuDJEGViIMI4uERIf%2Bs77DvFbMSkEBkZs%2Bn%2FfnUjdB9APvk4zq2qlj6AiDXX2CGYf31MPVci8HkgcsXFcpL7cRLBbRGRWS; __Host-nc_sameSiteCookielax=true; __Host-nc_sameSiteCookiestrict=true
Host: MYHOSTNAME
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0
PathPrefixStrip"似乎朝着 CLIENT>>SERVICE 的方向工作.这是我的 traefik 日志包含的内容:
The "PathPrefixStrip" seems to work in the direction CLIENT>>SERVICE. This is what my traefik log contains:
traefik | time="2018-04-04T18:12:54Z" level=debug msg="vulcand/oxy/roundrobin/rr: competed ServeHttp on request" Request="
{
"Method":"GET",
"URL":{
"Scheme":"",
"Opaque":"",
"User":null,
"Host":"",
"Path":"/",
"RawPath":"",
"ForceQuery":false,
"RawQuery":"",
"Fragment":""
},
"Proto":"HTTP/2.0",
"ProtoMajor":2,
"ProtoMinor":0,
"Header":{
"Accept":[
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
],
"Accept-Encoding":[
"gzip, deflate, br"
],
"Accept-Language":[
"de,en-US;q=0.7,en;q=0.3"
],
"Cookie":[
"ocuvhr6ala6i=d2cd9020839889a752b4375a63dedad0; oc_sessionPassphrase=qJu13Q%2FlAoSsv5b0qC18Re%2BcrcML6o32c2XuDJEGViIMI4uERIf%2Bs77DvFbMSkEBkZs%2Bn%2FfnUjdB9APvk4zq2qlj6AiDXX2CGYf31MPVci8HkgcsXFcpL7cRLBbRGRWS; __Host-nc_sameSiteCookielax=true; __Host-nc_sameSiteCookiestrict=true"
],
"Upgrade-Insecure-Requests":[
"1"
],
"User-Agent":[
"Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:59.0) Gecko/20100101 Firefox/59.0"
],
"X-Forwarded-Prefix":[
"/blog"
]
},
"ContentLength":0,
"TransferEncoding":null,
"Host":"MYHOSTNAME",
"Form":null,
"PostForm":null,
"MultipartForm":null,
"Trailer":null,
"RemoteAddr":"81.128.35.176:33468",
"RequestURI":"/",
"TLS":null
}
"
但重定向答案在我的浏览器中如下所示:
But the redirection answer looks as follows in my browser:
HTTP/2.0 302 Found
cache-control: no-cache, must-revalidate, max-age=0
content-length: 0
content-type: text/html; charset=UTF-8
date: Wed, 04 Apr 2018 18:44:18 GMT
expires: Wed, 11 Jan 1984 05:00:00 GMT
location: https://MYHOSTNAME/wp-admin/install.php
server: Apache/2.4.25 (Debian)
X-Firefox-Spdy: h2
x-powered-by: PHP/7.2.2
因此重定向响应不包含任何有关剥离路径前缀/blog"的信息.
So the redirect-response does not contain any information about the stripped path prefix "/blog".
更新 3
最后看起来是容器内服务的软件不处理标头的问题.
At the end it looks like a problem of the served software inside the container that does not handle the header.
其他资源:
有什么想法吗?
推荐答案
也许您应该在 PathPrefixStrip:/blog
规则中添加所有可能的值,例如.
Maybe you should add all possible values in your PathPrefixStrip: / blog
rule eg.
PathPrefixStrip: /blog,/wp-admin,/abc,/xyz
在许多情况下,它适用于标准路线.最大的问题是当您的后端服务不侦听根目录 /
中的请求,而是在某些子目录 /something/index.html
中并且该子目录占用资源时从根 /
开始.
In many cases, it works for standard routes. The biggest problem is when your backend service does not listen to requests in the root /
but in some sub-dir /something/index.html
and that sub-dir takes resources from the root /
.
这篇关于使用路径前缀规则时如何使用 Traefik 重写路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!