Rewrite rule for vue apps in Caddy v2

Dear all,

in Vue docs, there is example for Caddy v1 that not works for my Caddy v2

rewrite {
    regexp .*
    to {path} /
}

My Caddy version (caddy -version):
v2.0.0 h1:pQSaIJGFluFvu8KDGDODV8u4/QRED/OPyIR+MWYYse8=

My complete Caddyfile:

xxxxxx.net, www.xxxxxx.net {
 root *  /var/www/xxxxxx.net/html
 file_server

 encode gzip zstd

 try_files {path}.html {path}

    handle_errors {
        @404 {
            expression {http.error.status_code} == 404
        }
        rewrite @404 /404.html
        file_server
    }

 rewrite {
    regexp .*
    to {path} /
 }

  log {
     output file         /var/www/html/xxxxxx.net/logs/access.log
     format single_field common_log
   }

}

Could you please help me with this issue?

The equivalent rewrite in Caddy v2 would be this:

try_files {path} /

But since you already have try_files, all you need to do is add / to the end of your existing one for it to fallback to serving the index file!

try_files {path}.html {path} /

Also, I went ahead and edited your post to fix your Caddyfile’s formatting. Please use ``` on the lines before and after your config to use code formatting to preserve whitespace. Much easier to read.

2 Likes

Thank you for your help, but sadly not working.
When I’m looking in status

{"level":"info","ts":1590786381.9126651,"msg":"using provided configuration","config_file":"/etc/caddy/Caddy
May 29 21:06:21 d caddy[6591]: run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /etc/caddy/Caddyfile:77 - Erro
May 29 21:06:21 d systemd[1]: caddy.service: Main process exited, code=exited, status=1/FAILURE
May 29 21:06:21 d systemd[1]: caddy.service: Failed with result 'exit-code'.

Here is working code from nginx

server {
   listen      80;
   server_name xxxxxx.net;
   charset utf-8;
   root    /var/www/xxxxxx.net/html;
   index   index.html index.htm;

   # Always serve index.html for any request
   location / {
       root /var/www/xxxxxx.net/html;
       try_files $uri /index.html;
   }

   error_log  /var/log/nginx/xxxxxx-error.log;
   access_log /var/log/nginx/xxxxxx-access.log;
}

You forgot to get rid of your old (broken) rewrite directive.

Also, please use caddy fmt to make your config look nicer, it’ll be easier to read and will help you identify problems easier.

1 Like

Thank you,
I did that, remove old rewrite directive, now my caddyfile looks like this (after caddy fmt) but not work as expected. In my app, when I’m pressing F5 (Refresh) my site show me 404 page, but not refresh current page.
Will be appreciate for the help.

xxxxxxxx.net {
root *  /var/www/xxxxxxxx.net/html
file_server

encode gzip zstd

try_files {path}.html {path}

  handle_errors {
      @404 {
          expression {http.error.status_code} == 404
      }
      rewrite @404 /404.html
      file_server
  }

# rewrite {
#    regexp .*
#    to {path} /
# }

log {
   output file         /var/www/html/xxxxxxxx.net/logs/access.log
   format single_field common_log
 }

}

try_files {path}.html {path} /
did you forgot / at the end?

2 Likes

Yes I forgot it. Added, but still the same situation.

What does your full Caddyfile look like at this point? What do your logs say? How are you reloading Caddy with your config changes? Are you sure you reloaded Caddy’s config?

You can run caddy fmt --overwrite Caddyfile to fix your Caddyfile’s formatting.

3 Likes
xxxxxxx.net {
        root * /var/www/xxxxxxx.net/html
        file_server
        encode gzip zstd

        try_files {path}.html {path} /

        handle_errors {
                @404 {
                        expression {http.error.status_code} == 404
                }
                rewrite @404 /404.html
                file_server
        }

        log {
                output file /var/www/xxxxxxx.net/html/logs/access.log
                format single_field common_log
        }
}

Here is me file after

caddy fmt --overwrite /etc/caddy/Caddyfile

But again, not working properly. Still shows my custom 404 page when I’m refreshing my page.

Yes, I’ sure that reloading configuration.

sudo service caddy restart

FYI that doesn’t necessarily reload the configuration, that just stops the server (downtime! :grimacing:) and starts it again. Depending on your service file and config that doesn’t change the configuration - always use reload instead.

How can we reproduce the issue that you’re experiencing?

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