Rewrite nginx "rewrite block" to caddy

Is there any way to debug how it rewrites?

There are a few rewrite-specific placeholders that get changed when a rewrite occurs, see https://caddyserver.com/docs/placeholders.

You can use log to append them to requests logs, for example: log /path/to/caddy.log "{combined} /// {rewrite_uri}" - and look for the results.

1 Like

I just can’t get it work, i don’t know how to make it right.
Here are some examples:
Lighttpd Lighttpd Guide - Using Friendly URLs | MODX Documentation
NGINX:

if (!-e $request_filename) {
    rewrite ^/(.*)$ /index.php?q=$1 last;
}

Apache:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

How to write it on caddy? i’m ready to test on my side

Hi @Andrey_Izotov,

I’ve merged your latest post into this thread to keep the conversation on this topic in one place.

As noted above, the literal translation of your Apache example to a Caddyfile is as follows:

rewrite {
  regexp ^/(.*)$
  to {path} {path}/ /index.php?q={1}
}

If it’s not working as expected, you should look into logging to determine what’s going wrong. Your full Caddyfile and a file tree of your web root might also be helpful to troubleshoot.

I am using Ubuntu 17.10

This is my Caddyfile located in /var/www/

(friendlyurls) {
    rewrite {
        regexp ^/(.*)$
        to {path} {path} /index.php?q={1}
    }
}

(php-host) {
    fastcgi / /run/php/php7.2-fpm.sock php
    gzip
    cache
}

(http2https) {
    redir 301 {
        if {>X-Forwarded-Proto} is http
        /  https://{host}{uri}
    }
}

my-host.com {
    root /var/www/html/my-host.com
    #tls /etc/letsencrypt/live/my-host.com/fullchain.pem /etc/letsencrypt/live/my-host.com/privkey.pem
    import friendlyurls
    #import http2https
    import php-host
    tls off
    #log ./log/my-host.log "{combined} /// {rewrite_uri} // {rewrite_query}"
    log / stdout "{combined} /// URI:{rewrite_uri} // PATH:{path} // QUERY:{query} // FILE:{file}"
    #errors ./log/my-host.error
}

With your last config when i run my-host.com:2015/dynamic-css.css (dynamically generated file) it works, but when i am trying to serve static asset http://my-host.com:2015/assets/theme/main/js/bootstrap-switch.js
It returns 404 page.

Does the bootstrap-switch.js file exist and have the correct file permissions on the host’s file system?

What’s the output of ls -l /var/www/html/my-host.com/assets/theme/main/js/bootstrap-switch.js ?

-rwxrwxrwx 1 www-data www-data 14919 Mar 8 16:49

Just to confirm - if you comment out import friendlyurls, reload Caddy, and run curl -I http://my-host.com:2015/assets/theme/main/js/bootstrap-switch.js, does it work again?

Yes it does

That seems like unintended behaviour.

Could you please try:

  1. Rename your existing Caddyfile to Caddyfile.bak
  2. Create a new Caddyfile with the following content:
:2015 {
  root /var/www/html/my-host.com
  log / stdout "{combined} /// {rewrite_uri}"
  errors stdout
  rewrite {
    to {path} {path}/ /index.php?q={uri}
  }
}
  1. Post your caddy -version
  2. Run caddy -log stdout in the same directory as your new Caddyfile
  3. From another terminal, run curl -I http://localhost:2015/assets/theme/main/js/bootstrap-switch.js
  4. Post the output of both terminals (the curl command and the caddy output)
andrei@globus:/var/www$ caddy -version
Caddy 0.10.12 (non-commercial use only)
andrei@globus:/var/www$ caddy -log stdout
Activating privacy features... done.
http://:2015
2018/04/03 13:00:02 http://:2015
127.0.0.1 - - [03/Apr/2018:13:01:15 +0800] "HEAD /assets/theme/main/js/bootstrap-switch.js HTTP/1.1" 200 0 "-" "curl/7.55.1" /// /assets/theme/main/js/bootstrap-switch.js

It works well:

curl -I http://localhost:2015/assets/theme/main/js/bootstrap-switch.js
HTTP/1.1 200 OK
Accept-Ranges: bytes
Content-Length: 14919
Content-Type: application/javascript
Etag: "p59kitbif"
Last-Modified: Thu, 08 Mar 2018 08:49:41 GMT
Server: Caddy
Date: Tue, 03 Apr 2018 05:01:15 GMT

Alright! Now we’ve got a known working configuration, it’s time to slowly - one-by-one - add the other directives and snippets back in to your Caddyfile.

If/when it breaks again, you should be able to pin-point what the problematic configuration is.

The problem is “cache” directive, after i turn it on everything stops working.

Could be a bug of some kind in recent versions… The cache plugin is developed by @nicolasazrak:

If you could narrow your Caddyfile down to the simplest possible version that breaks with cache present (does it happen just with cache and any rewrite?) and post an issue to that Github repository (possibly linking to this forum thread), he should be better able than I to help sort it out.

1 Like

Just add cache directive to your example and it already doesn’t work

:2015 {
  root /var/www/html/my-host.com
  log / stdout "{combined} /// {rewrite_uri}"
  errors stdout
  rewrite {
    to {path} {path}/ /index.php?q={uri}
  }
  cache
}

Unfortunatelly this plugin seems to be abandonned, there is another person in issues who reported that cache breaks redir behavior, i believe it’s the same problem… But no reaction from author and last commin in Aug 2017

That’s unfortunate news.

Your best bet might be to look into Varnish or similar software to handle caching for you instead.

Author of cache module here. Sorry if it seems abandoned. I’m not adding new features but I’m looking if an issue appears. I’ll try to investigate both of the opened issues.

2 Likes

Alright, thanks, i’m happy to hear it, if you need any help or additional information, just ask.

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