Trying to serve files with browse on one route but have website work on another

1. Caddy version (2.0.0):

2. How I run Caddy:

I’m running caddy using a caddy file which I modify and run caddy reload.

a. System environment:

CentOS 7

b. Command:

caddy reload

d. My complete Caddyfile or JSON config:

http://adam.teaches.engineering {
    handle /files* {
        file_server browse {
            root /var/www/html/myFiles
        }
    }

     handle * {
       root * /var/www/myWebsiteFolder
       file_server
       reverse_proxy /abc/ws localhost:1025
     }

}

3. The problem I’m having:

I’d like to be able to access “http://adam.teaches.engineering” and get the webpage at /var/www/myWebsiteFolder.

But when I access “http://adam.teaches.engineering/files” I want to be able to browse the files at the
/var/www/html/myFiles folder.

I’ve got a websocket connection that goes to http://adam.teaches.engineering/abc/ws that needs to get proxied to locahost:1025 as well.

4. Error messages and/or full log output:

There are no error messages but when I visit http://adam.teaches.engineering/files I just get a 404 page.

When I visit adam.teaches.engineering , the webpage works. The proxy also works.

5. What I’ve tried:

I’m trying to do the simplest thing which is to just serve a webpage at /files rather than *.

http://adam.teaches.engineering {
    root /files* /var/www/webPage
    file_server
}

But this gives me a 404. It’s as if the path matcher doesn’t work?

The file_server in Caddy takes the defined root and appends the current request path to it, so it’ll be looking in /var/www/html/myFiles/files.

To solve this, you should use the handle_path directive instead of handle, which has built-in path prefix stripping logic.

For your proxy, I recommend separating that into another handle:

http://adam.teaches.engineering {
	handle_path /files* {
		root * /var/www/html/myFiles
		file_server
	}

	handle /abc/ws {
		reverse_proxy localhost:1025
	}

	handle {
		root * /var/www/myWebsiteFolder
		file_server
	}
}

This makes sense although I’m getting unrecognized directive for handle_path .
I’m using Caddy V2, is there some update I need to make?

I tried using:

        uri strip_prefix /files
        root * /var/www/html/myFiles
	file_server
}

But that just redirects to the main website again…

Ah, yeah I missed that you’re running Caddy v2.0

Please upgrade to v2.2.1

Alright upgraded :slight_smile:
So it just strips the path from the url and takes me to adam.teaches.engineering right away now.

It’s still not going to the files :confused:

What’s your full Caddyfile now? What do you see in your logs? What happens when you use curl -v to make the request?

We need more info to dig deeper. Please be as specific as possible.

This topic was automatically closed after 30 days. New replies are no longer allowed.