caddy-website/src/docs/markdown/caddyfile/directives/php_fastcgi.md

82 lines
2 KiB
Markdown
Raw Normal View History

2020-01-24 12:47:52 -07:00
---
title: php_fastcgi (Caddyfile directive)
---
# php_fastcgi
An opinionated directive that proxies requests to a PHP FastCGI server such as php-fpm.
Caddy's [reverse_proxy](/docs/caddyfile/directives/reverse_proxy) is capable of serving any FastCGI application, but this directive is tailored specifically for PHP apps. This directive is actually just a convenient way to use a longer, more common configuration (below).
It expects that any `index.php` at the site root acts as a router. If that is not desirable, either perform your own URI rewrite or use something like the [expanded form](#expanded-form) below and customize it to your needs.
## Syntax
```caddy-d
2020-01-24 12:47:52 -07:00
php_fastcgi [<matcher>] <php-fpm_gateway>
```
- **<php-fpm_gateway>** is the address of the FastCGI server.
Since this directive is an opinionated wrapper over a reverse proxy, you can open a block and use any of reverse_proxy's subdirectives to customize it.
## Expanded form
The `php_fastcgi` directive is the same as the following configuration:
```caddy-d
2020-04-15 10:34:47 -06:00
route {
# Add trailing slash for directory requests
@canonicalPath {
file {
try_files {path}/index.php
}
not path */
2020-01-24 12:47:52 -07:00
}
2020-04-15 10:34:47 -06:00
redir @canonicalPath {path}/ 308
2020-01-24 12:47:52 -07:00
2020-04-15 10:34:47 -06:00
# If the requested file does not exist, try index files
@indexFiles {
file {
try_files {path} {path}/index.php index.php
split_path .php
}
}
rewrite @indexFiles {http.matchers.file.relative}
2020-01-24 12:47:52 -07:00
2020-04-15 10:34:47 -06:00
# Proxy PHP files to the FastCGI responder
@phpFiles {
path *.php
}
reverse_proxy @phpFiles <php-fpm_gateway> {
transport fastcgi {
split .php
}
2020-01-24 12:47:52 -07:00
}
}
```
Most modern PHP apps work well with this preset. If yours does not, feel free to borrow from this and customize it as needed instead of using the `php_fastcgi` shortcut.
## Examples
Proxy all PHP requests to a FastCGI responder listening at 127.0.0.1:9000:
```caddy-d
2020-01-24 12:47:52 -07:00
php_fastcgi 127.0.0.1:9000
```
Same, but only for requests under `/blog/`:
```caddy-d
2020-01-24 12:47:52 -07:00
php_fastcgi /blog/* 127.0.0.1:9000
```
When using php-fpm listening via a unix socket:
```caddy-d
php_fastcgi unix//run/php/php7.4-fpm.sock
```