-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
File server redirects not handled by internal middleware #1811
Comments
Wanted to make a note on this. It appears the trouble I'm seeing with the internal is a byproduct of something Caddy is doing to perhaps enforce clean urls. For example, check out the static scenarios in the readme at https://github.com/dusty/caddy-issue On the simplest setup, where you are just statically serving a file if its named something that matches an index, and you request the file directly, instead of serving the file it will redirect you with a 301 to a URL without that filename. eg: get /index.html redirects you to / That is likely not going to be a problem, aside from an extra http request. However, I am seeing a potential problem (unless I have a mistake in my configuration). If you have a file named notindex.html, you will get redirected to /not. It appears Caddy is just stripping away the index from the end of path and sending a redirect with that. This is going to be a problem as you will likely get a 404 error, or get served incorrect content if you happen to have something located at not/index.html It appears this behavior is what is causing the issue I was seeing with the internal. |
I've never looked at Go code before, so I thought this might be interesting to look at. I think I located the part of the code that would send a redirect on notindex.html to /not. I added a failing test and then fixed it here: https://github.com/dusty/caddy Basically the check to see if an index file was called was simply using hasSuffix on the path. I'm thinking that was probably a shortcut to deal with slashes (eg: /something/index.html). I changed it to split on / and then grab the last element and compare that directly to the index we were checking. For example notindex.html == index.html instead of strings.hasSuffix('/something/notindex.html', 'index.html') Not sure if this will fix the internal piece or broke anything else. Will look at that as soon as I figure out how to install all the deps. I installed go via homebrew and get this error. Any tips? go install: no install location for directory /Users/dusty/tmp/caddy/caddy outside GOPATH |
@dusty Nice job investigating! All your Go packages should be in your GOPATH (a "workspace"): https://golang.org/doc/code.html#Organization The default GOPATH is $HOME/go, I think. So check that folder. Having things in $HOME/tmp won't work for you unless you set that to your $GOPATH env variable. Caddy's static file server redirects requests to index files to their canonical path: So your fix is on the right track, but consider using path.Base instead. Does that help? |
Thanks! That is very helpful for a major go newbie. I'm currently googling things like "how to split a string in go" and "How to get the last element in an array in go" :) |
Ok, so I pushed out the change you suggested, much cleaner. This took care of a file named somethingindex.html being redirected to /something. It turns out this in particular is a different issue than the one I created this for. The redirect issue I am experiencing on serving an internal file named something defined as an index (index.html) still has an issue. I'll try digging for this tomorrow, any tips would be helpful. eg: GET http://localhost:4000/secure/index.html However, /private is protected by the internal command. In addition the actual server you are proxying to may be on an internal network that can't be reached and shouldn't be presented. To continue with the clean url behavior, I think it should be: |
@dusty Nice work on the index file bug; that is indeed separate, go ahead and submit a PR for review. I like @slightfoot's suggestion (in our Slack), where we possibly handle the redirect case within the |
Original issue was fixed in #1812. Could this be closed? |
1. What version of Caddy are you using (
caddy -version
)?Caddy 0.10.6
2. What are you trying to do?
Protect pages using the internal directive. An app server responds with X-Accel-Redirect
3. What is your entire Caddyfile?
4. How did you run Caddy (give the full command and describe the execution environment)?
$ caddy
5. Please paste any relevant HTTP request(s) here.
6. What did you expect to see?
File public/private/index.html served
7. What did you see instead (give full error messages and/or log)?
Returns redirect to http://localhost:3000/private/
8. How can someone who is starting from scratch reproduce the bug as minimally as possible?
Clone this repository
https://github.com/dusty/caddy-issue
The text was updated successfully, but these errors were encountered: