-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: Caddy v2.4.2 with specified rewrite rules causes unexpected 308 redirection #4205
Comments
This seems to happen when there's a rewrite (correct?). I think we need to revert f9b5445. @diamondburned Could you help take a look at this? @inoblue Can you distill your config down to the minimum required to reproduce the issue, and a |
This reverts commit f9b5445. /cc @diamondburned (see #4205)
For now, @inoblue, I've reverted what I believe to be the regression -- if you can build from source can you verify? @diamondburned, maybe we can come up with a better solution to #4179 that doesn't cause a regression. |
The issue was solved after building from source. Thanks for your help. |
Are there any debug logs available with the broken commit? Edit: I think it has to do with the changes in I think I can make a commit with just the fix for |
FWIW, what you were fixing was a bug for both |
The more I look at this and fiddle with it, the more I'm convinced that canonicalizing based on the original request path is not the correct behavior (i.e. I was wrong in the forum thread where I suggested that; or at least, wrong in the implementation). |
@inoblue Can you please share your full config and curl commands and logs? I need to know your exact use case. I can tell your config is not real because it has lines like Ideally, we need to be able to reproduce the bug in the most minimal way possible. This allows us to write regression tests to verify the fix is working. If we can't reproduce it, then you'll have to test our changes for us until it's fixed -- and then we can't add test cases, either. I've attached a template below that will help make this easier and faster! This will require some effort on your part -- please understand that we will be dedicating time to fix the bug you are reporting if you can just help us understand it and reproduce it easily. This template will ask for some information you've already provided; that's OK, just fill it out the best you can. 👍 I've also included some helpful tips below the template. Feel free to let me know if you have any questions! Thank you again for your report, we look forward to resolving it! Template
Instructions -- please heed otherwise we cannot help you (help us help you!)
Example of a tutorial: Create a config file: I will close the issue since we reverted the commit, and tag this as need more info. We can reopen it once we have that information. |
I am experiencing the same issue and downgrading to 1. Environment1a. Operating system and version
1b. Caddy version (run
|
Thanks for the repro instructions. That exhibits it for me. The commit I linked earlier is definitely a regression, because that shouldn't yield a redirect loop (edit: actually, maybe it should, see below). (I still want to know @inoblue's exact use case & config though, since how to solve both problems without a regression is still an open question.) |
@cam-perry Actually, it could be argued that your config should have a redirect. Requests to If you change your config to use I agree its unintuitive, since it's an internal rewrite it's not clear whether canonicalization should be happening. At a glance, no, but it makes sense if you think about it. Few real URLs actually show index.html in them... |
Point taken. I agree the redirect is not necessarily wrong, but it is not obvious. I wouldn't expect that being more explicit with Perhaps worth noting, my solution was guided by this community forum and your comment where the recommended solution is |
1. Environment1a. Operating system and version
(This is the output of 1b. Caddy version (run
|
Alright, thanks. Yeah, this happens when rewriting from an index/directory to a non-index/file, since the original path has a trailing slash but the rewritten path doesn't. Hmm hmm hmm. |
Okay so I'm starting to understand this bug better, and to be honest, I'm torn on this one. Here's what I know:
One could reasonably say that changing configs that rewrite from However, one could also argue that rewriting to an index file explicitly ( I am guilty of this in at least 2 site configs of my own so far, and it's not really something I've thought about. But now I wonder if these kinds of configs really are misconfigurations, and should be fixed; or whether we should try to be clever to not break existing sites. For right now, f9b5445 has been reverted in 8848df9, but I'm hoping we can come to a satisfying resolution before releasing v2.4.3... |
For the record, I tried using a solution that Francis suggested where we don't canonicalize (redirect) if the rewritten path is explicitly an index file. This worked for one of the use cases I tried, but not the Caddy website for example (the docs part of the site), in part due to how templates use the rewritten path (it expects the index page to have "index" in the rewritten path). I think for now the best thing to do is keep the revert for v2.4.3. Yes, it can be problematic for URL canonicalization when used inside |
I think I came up with a solution that works in all 3 main cases I've tested: only redirect if the base element of the path (the filename) is the same in both the original request and the rewritten request. In other words, do not redirect if the filename in the path was changed/rewritten internally. (The logic being, if the admin wanted to rewrite to the canonical path, they would have.) This seems to prevent the file server from stepping on the toes of intentional rewrites, while still enforcing path canonicalization when, for example, only the prefix of the path is changed (as in For all the test cases below, I found it helpful to turn on debug mode:
Test case 1This config (used on my Expert Caddy website) used to cause a redirect loop:
(This config could actually be better, probably by specifying Test case 2This config is from the Caddy website. Without my proposed change, it would not render pages in the /docs/ section of the site:
With my proposed change, the docs pages still function as expected (canonicalization redirects are NOT dispatched, since I explicitly rewrote the filename to be what I want it to be). Test case 3A simple file server with directory listings, that does not have an index file, where we use
With my proposed change, this continues to issue canonicalization redirects because only the path prefix is rewritten, not the filename; and the redirects correctly preserve the I'm going to commit and push this soon, then probably release v2.4.3 later today. |
i was puzzled by the HTTP 308 Permanent Redirect problem and found the issue with my configuration. When the address contains a trailing slash as in The trailing slash is not mentioned in, but also not forbidden according to https://caddyserver.com/docs/caddyfile/concepts#addresses $ mkdir -p /srv/http/example.com/
$ touch /srv/http/example.com/index.html
$ touch /srv/http/example.com/map.js
$ caddy version
v2.4.6
# Caddyfile
http://foo.example.com,
http://bar.example.com/ {
root * /srv/http/example.com/
file_server
}
$ curl --include foo.example.com/map.js
HTTP/1.1 200 OK
$ curl --include bar.example.com/map.js
HTTP/1.1 308 Permanent Redirect |
The trailing slash creates a path matcher which only matches exactly I agree this is confusing behaviour, and I plan to deprecate path matchers in site addresses soon, because it's clearer to use a Your issue is not related to the one that was originally reported in this issue. It's a different problem altogether. |
Before deprecating it, I'd like to try improving documentation around it first. It does have value and valid use cases. |
and Test case 1 from caddyserver#4205 (comment)
Phenomenon: Open web page on Firefox and Chrome, and they both say "the page cannot redirect correctly". After downgrading Caddy to v2.4.1, everything becomes OK.
Caddy configuation (Caddyfile):
In Caddy's log, there are many lines like the one below:
The text was updated successfully, but these errors were encountered: