V1 to V2 Caddyfile conversion issue

1. Caddy version (caddy version):

v2.3.0 h1:fnrqJLa3G5vfxcxmOH/+kJOcunPLhSBnjgIvjXV/QTA=

a. System environment:

Windows 7

b. Command:

caddy.exe run --config Caddyfile.V2

d. My complete Caddyfile or JSON config:

localhost:20000, 127.0.0.1:20000 {

  root ./webapp

  php_fastcgi localhost:6545 
  
  try_files {path}.html {path}.htm {path}.php {path}
  
}

3. The problem I’m having:

This was my V1 Caddyfile:

localhost:20000, 127.0.0.1:20000 {

  root ./webapp
  
  tls self_signed
  
  ext .html .htm .php
  
  on startup php_cgi.bat &
  fastcgi / 127.0.0.1:6545 php  
 
}

I would like to get EXACTLY the same with Caddy 2.

I understand that the “on” directive has gone. No problem. I can run php_cgi.bat alone.
Now I’m using this V2 caddyfile:

localhost:20000, 127.0.0.1:20000 {

  root ./webapp

  php_fastcgi localhost:6545 
  
  try_files {path}.html {path}.htm {path}.php {path}
}

I’m no longer getting 404 errors.
Anything not matching a request is redirected to index.php.
Why?

Can I avoid this behaviour and have my 404 errors back?

You need to enable file_server for Caddy to serve files. In v1, that was implicitly enabled, but in v2 you must explicitly enable it.

See the upgrade guide:

I did but I still don’t get a 404.
I get a 302 instead, and got redirected to index.php (which in this case sends to login.php)
::1 - - [27/Apr/2021:22:27:35 +0200] “GET /loginaaaaaaaaaaaaaaaaa HTTP/2.0” 302 2438
::1 - - [27/Apr/2021:22:27:35 +0200] “GET /login HTTP/2.0” 200 4286

If I comment out php_fastcgi localhost:6545 it behaves as expected and a 404 is fired when a page is not found. With the php_fastcgi directive I get redirected to index.php.
Is it possible to avoid it?

Ah, to turn off the fallback to index.php, you can set index off:

php_fastcgi localhost:6545 {
	index off
}

The majority of modern PHP apps use index.php for routing, so that’s the default behaviour.

1 Like

It works!

It does exacly what I need if I use:

php_fastcgi localhost:6545 {
	index off
}
try_files {path} {path}.html {path}.php {path}/index.html {path}/index.php

Thank you very much!

1 Like

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