Send file contents without rewriting url (file based caching)

Hello,

I found a bunch of thread regarding the migration from nginx and especially try_files to caddy and tried to use the proposed solutions.

My server handles multiple domains and proxies requests to a node.js app that renders the requested page and saves a static copy of the resulting html to the filesystem. These files should be used for subsequent requests to not put additional load on the node app.

All rendered files are saved with this pattern /var/www/cache/hostname/path/index.html:

example.com => /var/www/cache/example.com/index.html
example.com/some-page/ => /var/www/cache/example.com/some-page/index.html
example2.com => /var/www/cache/example2.com/index.html
first request to example3.com => no file exists under /var/www/cache/example3.com/index.html so forward to node
second request to example3.com => /var/www/cache/example3.com/index.html

and so on…

All requests to files e.g. /bundle.js should still be forwarded to the node app.

The caddyfile currently looks like this:

:80 {
  root /var/www/cache/

  gzip

  tls off

  log stdout

  errors visible

  proxy /proxy localhost:3001 {
    without /proxy
    transparent
  }

  rewrite / {
    to {host}{uri}index.html /proxy{uri}
  }
}

no matter what I try to do, I never end up with a prerendered file from the file system. I kind of feel lost here and 90% of time I end up with too many redirects when I try different things.

The desired process is very simple, but I’m not able to figure out a proper config.

Any help would be appreciated.

Thanks in advance.

jsvde

taking this thread into account [Redirect loop when rewriting / to /index.html to apply basicauth to it](http://Redirect Loop when rewriting to index) it seems the problem is using index.html in the redirect.

changing the config to

rewrite / {
    to {host}{uri}home.html /proxy{uri}
  }

and renaming the files from index.html to home.html (for testing) results in the correct files being served.

So the filename of index.html leads to very funny redirects.

You could instead leave them as index.html and redirect to {host}{uri}/ /proxy{uri}, if that would be more convenient.

The issue is that canonically, index.* files should be requested using the parent directory with a trailing slash, and the static file server enforces this.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.