1. The problem I’m having:
I would like to poll a simple REST API which I have already created with GET requests of this form:
[https://mywebsite.com/react-api-mw/v1/$MYPROJ/$MYHANDLER/$MYPROD_ID]
The PHP script I created is called index.php and it sits in the caddy_root/react-api-mw directory. It will simply parse the url and fetch the query params by exploding the URI passing ‘/’ as separator, like:
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = explode( '/', $uri );
I had this working in Apache 2 via a simple rewrite rule that prepended index.php to the URL:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /react-api-mw
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ index.php/$1 [QSA,NC,L]
</IfModule>
However I’m a bit at a loss trying to translate this in a manner that is compatible with php_fastcgi (I’m serving a Wordpress blog/shop in the same domain). I could fetch some similar posts from the community but some of them mention caddy v1 and I’m unsure about how v2 and php_fastcgi interact.
2. Error messages and/or full log output:
{"level":"error","ts":1690818714.8597414,"logger":"http.log.access.log1","msg":"handled request","request":{"remote_ip":"2.136.86.217","remote_port":"52008","proto":"HTTP/1.1","method":"GET","host":"mywebsite.com","uri":"/react-api-mw/v1/myweb/products/1010","headers":{"User-Agent":["PostmanRuntime/7.32.3"],"Accept":["*/*"],"Postman-Token":["7f8ba52f-c1d7-44e8-8cab-a7a430857661"],"Accept-Encoding":["gzip, deflate, br"],"Connection":["keep-alive"]},"tls":{"resumed":false,"version":772,"cipher_suite":4865,"proto":"","server_name":"mywebsite.com"}},"user_id":"","duration":1.739427749,"size":32942,"status":404,"resp_headers":{"Cache-Control":["no-cache, must-revalidate, max-age=0"],"Content-Type":["text/html; charset=UTF-8"],"Link":["<https://mywebsite.com/wp-json/>; rel=\"https://api.w.org/\""],"Vary":["Accept-Encoding"],"Server":["Caddy"],"Status":["404 Not Found"],"Expires":["Wed, 11 Jan 1984 05:00:00 GMT"],"Alt-Svc":["h3=\":443\"; ma=2592000"],"Content-Encoding":["gzip"]}}
3. Caddy version:
v2.6.4 h1:2hwYqiRwk1tf3VruhMpLcYTg+11fCdr8S3jhNAdnPy8=
4. How I installed and ran Caddy:
Installed via official repo (apt)
a. System environment:
Running as systemd service on an Ubuntu 22.04 VPS on hetzner
b. Command:
systemctl start caddy
d. My complete Caddy config:
mywebsite.com {
tls seb@mywebsite.com
root * /var/www/mywebsite
php_fastcgi unix//run/php/php8.1-fpm-www.sock
file_server
encode gzip
log {
output file /var/log/caddy/mywebsite-access.log {
roll_size 10mb
roll_keep 5
roll_keep_for 720h
}
}
@disallowed {
path *.sql
path /wp-content/uploads/*.php
}
rewrite @disallowed '/index.php'
}