Caddy2 matcher / rewrite / modx

1. My Caddy version (caddy version): (devel) /has to do this for php sockets to work/

2. How I run Caddy:

./caddy run --config /root/downloads/configfile --adapter caddyfile

a. System environment:

Ubuntu, php-fpm

b. Command:

./caddy run --config /root/downloads/config --adapter caddyfile

d. My complete Caddyfile or JSON config:

xxxxx.ru {
  root * /home/xxxxx/www/public
  log /var/log/caddy/caddy.log
  php_fastcgi unix//var/run/php/php-fpm-xxxxx.sock

  @a {
    path_regexp myregex ^(.*)$
  }

  rewrite match:a /index.php?q={http.matchers.path_regexp.myregex.1}
  file_server
}

3. The problem I’m having:

Basically I am trying to mimic .htaccess

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

4. Error messages and/or full log output:

run: adapting config using caddyfile: parsing caddyfile tokens for 'rewrite': /root/downloads/config:10 - Error during parsing: Wrong argument count or unexpected line ending after '/index.php?q={http.matchers.path_regexp.myregex.1}'

5. What I already tried:

Googled and looked through community. Here is the topic.

The solution there is for V1 and does not work as rewrite directive changed…

I looked through rewrite (Caddyfile directive) — Caddy Documentation as well.
Tried different methods. One of them is here - V2: Having trouble with rewrite

However when i try to use matcher a it gives me an error of unknown directive, so I had to change it to @a and now I have this parsing unexpected. So basically lost.

Help needed

Can someone can help to adapt that original apache rewrite to V2 Caddy so i can try to run modx?

You were close! Since then, the matcher syntax changed. Now we use @ both when defining and using matchers.

rewrite @a /index.php?q={http.matchers.path_regexp.myregex.1}

Thanks for filling out the issue template, it’s appreciated!

1 Like

Indeed that was really close :slight_smile:

Thank you for the prompt reply. It does work, but much better than I expected… still need to find a solution to mimic -f -d from original .htaccess as now it send everything to that index.php, however if the file is there - it should just server it as it does with .css and others.

For that you’ll want to change your matcher to use try_files:

@a {
	not {
		file {
			try_files {path}
		}
	}
}
rewrite @a /index.php?q={path}

This will match is only if there is no existing file on disk for {path}. Otherwise, it’ll rewrite. I’m pretty sure the regex in your config is unnecessary, I think the {path} placeholder is all you need for what you’re trying to do.

Edit: Actually I think even simpler:

try_files {path} /index.php?q={path}

There’s a try_files directive which is a shortcut for this sort of thing. Not 100% certain it’ll work the way you need it to, but give it a shot!

1 Like

I’ve ended up with the same
try_files {path} /index.php?q={path}
still does not give me expected behavior

Have to give up here and switch to nginx. The date you will have config files for most of the CMS - would be the incredible day for your product.

Can you really call it a product since it’s free, though? :thinking:

1 Like

It really is. I mean this is the solid solution helping thousands around the world. Does not matter if it is free or paid. This is still a product. A very very good product btw. So, please no offence here.

And I would really like more and more start using this blazing fast server and this requires more use case scenarios for general not that tech savvy users. It is done already for V1 so I am sure when V2 would be a full release there would be a much better knowledge base which would help all of us.

Thanks!

@rzimin
Hello there, i was the author of v1 configuration for modx, now slowly migrating to v2.
What is the problem with that configuration:

    here was wrong version

It seems to work fine for me

1 Like

Path doesn’t work because modx doesn’t understand q parameter starting with “/”

Okay after few hours it seems i have solved the puzzle
Here is the working solution:

root * /var/www/html/globus-furniture
encode zstd gzip
respond /core/* 404 {
    close
}
@pages {
    path_regexp modx ^/(.*)$
    file {
    	try_files {path} /index.php
    }
    not path /manager /manager/* /connectors/*
}
rewrite @pages {http.matchers.file.relative}?q={http.regexp.modx.1}
php_fastcgi unix//run/php/php7.4-fpm.sock
redir /index.php / permanent
file_server

The biggest challenge was to unite try_files and regexp

1 Like

Nice! Glad you figured it out!

FYI, you can simplify your matcher like this:

    not path /manager /manager/* /connectors/*

Okay i fixed it, thank you

Please fix this information because now we have a solution.

1 Like

Seems it was done already by the devs.
Thank you for your solution.

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