Upgrade rewrite from v1 to v2?

I need some help with upgrading a rewrite from v1 to v2:

  rewrite /api {
          r (.*)
          to /index.php?page=newznabapi&{query}
  }

So, the above is the original v1 rewrite, that works like a charm. But with v2, I can’t find any way to make this work.

rewrite /api/ /index.php?page=newznabapi&{query}

Isn’t doing it. And I’ve been trying to get it work for 2 weeks now… But can’t… Please help?

Path matching in Caddy v2 is exact-match, meaning /api/ will only match exactly /api/. To match paths starting with /api/, then add a *, like this: /api/*. Or if the / is not necessary, you can do /api*.

Ok, I might be pretty stupid… But I cannot get this to work…

This is the v1 configuration:

http://spotweb.delaars12.loc {
  ext .html .htm .php
  root /opt/share/spotweb/
  log /var/log/caddy/spotweb-local.log {
        rotate_size 100
        rotate_age  5
        rotate_keep 5
  }
  fastcgi / 127.0.0.1:9001 php
  rewrite /api {
          r (.*)
          to /index.php?page=newznabapi&{query}
  }
}

Please, help??? The rewrite is very important…

What’s the full v2 Caddyfile you’ve tried? What errors did you see?

I Tried the following two:

http://spotweb.delaars12.loc {
  root * /opt/share/spotweb/
  try_files {path}.html {path}.php
  file_server
  rewrite /api* /index.php?page=newznabapi&{query}
  php_fastcgi 127.0.0.1:9001
  log {
     output file /var/log/caddy/spotweb.log
     format single_field common_log
  }
}

http://spotwebapi.delaars12.loc {
  try_files {path}.html {path}.php
  file_server
  root * /opt/share/spotweb
  php_fastcgi 127.0.0.1:9001 {
      index index.php
  }
  rewrite /api* /index.php?page=newznabapi&{query}
  log {
     output file /var/log/caddy/spotweb-api.log
     format single_field common_log
  }
}

But both don’t work at all… It drives me crazy!

Hmm. That all looks okay to me. You can add the debug global option to get additional logging for the rewrites to see what’s happening. Add this at the top of your Caddyfile:

{
    debug
}

You can find the logs in Caddy’s stdout. You didn’t say how you’re running Caddy, so I’m not sure where that would be for your setup.

I am running caddy with the following startup on Raspberry Pi 3b+ raspbian 10.4:

[Unit]
Description=Caddy
Documentation=https://caddyserver.com/docs/
After=network.target

[Service]
User=caddy
Group=caddy
ExecStart=/usr/bin/caddy run --environ --config /etc/caddy/Caddyfile
ExecReload=/usr/bin/caddy reload --config /etc/caddy/Caddyfile
TimeoutStopSec=5s
LimitNOFILE=1048576
LimitNPROC=512
PrivateTmp=true
ProtectSystem=full
AmbientCapabilities=CAP_NET_BIND_SERVICE

[Install]
WantedBy=multi-user.target

I will add the debug option and see what happens :wink:

Ok, boot me into a christmas-tree. It’s working now…

2 Likes

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