111 lines
12 KiB
Markdown
111 lines
12 KiB
Markdown
# FastCGI and CGI applications in the Web Server
|
|
|
|
RustProxy 0.42.2 can serve executable application scripts through **existing external FastCGI backends**. It does not embed PHP, execute scripts in the RustProxy process, start a CGI worker, or parse Nginx/Angie configuration files. The routing and application settings are generic: they work with PHP sites, CGI control panels, monitoring tools, custom FastCGI applications, and other programs using compatible FastCGI responders.
|
|
|
|
## Supported backends
|
|
|
|
Select **Web Server → Web Service → PHP-FPM / FastCGI → Handler** (or edit a shared profile). The persisted field is `php.handler` for backwards compatibility; older `php` configurations still default to `php_fpm`.
|
|
|
|
| Handler (`php.handler`) | Use | Example `php.endpoint` | Example `php.extensions` |
|
|
|---|---|---|---|
|
|
| `php_fpm` | Existing PHP-FPM pool | `unix:/run/php/site.sock` | `php`, `phtml` |
|
|
| `fcgiwrap` | Existing CGI scripts executed by a separately running **fcgiwrap** service | `unix:/run/fcgiwrap.socket` | `cgi`, `pl` |
|
|
| `fastcgi` | Other compatible FastCGI responders (no CGI process launcher built into RustProxy) | `tcp:127.0.0.1:9001` | `fcgi` |
|
|
|
|
All handlers support Unix sockets (`unix:/absolute/path.sock`) and local TCP (`tcp:127.0.0.1:port` or `tcp:[::1]:port`). TCP is deliberately restricted to loopback. FastCGI must **not** be exposed on a public interface. The backend process must already be running, and the RustProxy service account must be able to connect to its socket. The configured `php.extensions` determine which *existing files* are executable; everything else follows static-file policy, except that a `fcgiwrap` Web Service deliberately refuses to serve ordinary files and directory listings to avoid leaking CGI source or adjacent configuration. PHP extensions can only be assigned to `php_fpm`; `PHP_VALUE` and `PHP_ADMIN_VALUE` are PHP-FPM-only.
|
|
|
|
The existing **PHP-FPM profiles** section also stores the new handler choices without changing existing profile IDs. Create a separate Web Service/profile for different document roots, handler types, sockets, or trust boundaries. Each Web Service has one FastCGI handler; combine several services under one domain with **Vhost → Custom locations** rather than attempting to paste Nginx `location` or `fastcgi_pass` directives.
|
|
|
|
## How paths are mapped
|
|
|
|
When a request is routed to a Web Service, RustProxy resolves the effective path **inside that service's `document_root`**. The path is validated and the script must exist as a regular file; symlinks are disallowed for FastCGI script execution. The FastCGI request contains:
|
|
|
|
| Parameter | Source and behavior |
|
|
|---|---|
|
|
| `SCRIPT_FILENAME` | Canonical Web Service document root plus the validated effective script path; never taken from an HTTP header or arbitrary custom FastCGI parameter. |
|
|
| `SCRIPT_NAME` | `php.script_name_prefix` + effective script path. Leave the prefix empty for normal mounts. Set it to the public URL prefix stripped/replaced by the Vhost location, such as `/portal/cgi-bin`. |
|
|
| `REQUEST_URI` | Original public path and query, for example `/portal/cgi-bin/status.cgi?view=1`. |
|
|
| `QUERY_STRING` / `REQUEST_METHOD` | Request query / method. Request bodies are streamed to FastCGI. |
|
|
| `PATH_INFO` | Optional remaining `/path/info` after the existing script filename, when `php.path_info=true`. |
|
|
| `REMOTE_ADDR` / `REMOTE_PORT` | Validated request client address / port. |
|
|
| `REMOTE_USER` / `AUTH_USER` | Only set when **Vhost Basic Auth** successfully authenticated the user; never copied from an unverified `REMOTE_USER` header. |
|
|
| `HTTP_AUTHORIZATION` | Forwarded only when authorized by Vhost credential-forwarding settings and `php.pass_authorization`; disable when not needed. |
|
|
| `HTTP_*` | Selected incoming headers, with hop-by-hop/unsafe headers removed. |
|
|
|
|
Custom FastCGI parameters (`php.custom_params`) are bounded static `NAME = value` pairs. Core CGI, identity, PHP control and `HTTP_*` parameters are **reserved** to avoid spoofing `SCRIPT_FILENAME`, `REMOTE_USER` and other security-sensitive values. Dynamic Nginx expressions (`$1`, `$remote_user`, `$http_authorization`) are **not** evaluated in custom parameters; equivalent core variables are generated by RustProxy. For private per-site values, prefer backend environment configuration over secrets in the RustProxy JSON configuration.
|
|
|
|
## Generic example: static assets, PHP and executable CGI on one HTTPS domain
|
|
|
|
Suppose an application is exposed as `https://portal.example.test/console/` with these filesystem paths and processes:
|
|
|
|
```text
|
|
/srv/apps/console/public/ # index.html, assets, PHP scripts
|
|
/srv/apps/console/cgi-bin/ # executable *.cgi scripts
|
|
unix:/run/php/console.sock # existing PHP-FPM pool
|
|
unix:/run/fcgiwrap.socket # existing fcgiwrap worker
|
|
```
|
|
|
|
Create **three reusable Web Services** (one safe default, one PHP/static application root and one isolated CGI root):
|
|
|
|
| Service ID | Document root | Handler | Extensions | Public `SCRIPT_NAME` prefix |
|
|
|---|---|---|---|---|
|
|
| `empty-default` | `/srv/www/empty` (empty directory) | Disabled | — | — |
|
|
| `console-web` | `/srv/apps/console/public` | `php_fpm`, endpoint `unix:/run/php/console.sock` | `php` | `/console` |
|
|
| `console-cgi` | `/srv/apps/console/cgi-bin` | `fcgiwrap`, endpoint `unix:/run/fcgiwrap.socket` | `cgi` | `/console/cgi-bin` |
|
|
|
|
For PHP/CGI services, enable FastCGI and set `symlink_policy=deny`. The PHP Web Service safely serves regular static files **and** invokes PHP-FPM for `.php`, including `index.php`. Set `console-web.index` to `index.php, index.html` and adjust `try_files` to the application's needs. Keep non-executable files out of the CGI root; use filesystem permissions and socket ownership to isolate each runtime. Enable `path_info` only if `/script.php/extra` or `/script.cgi/extra` is needed.
|
|
|
|
Create a **Vhost** bound to the HTTP and HTTPS listeners, select the appropriate certificate (RustProxy ACME or an imported certificate), enable **Force HTTPS** (and HTTP/2 if desired), and choose `empty-default` as the default Web Server source to avoid unintentionally exposing the application at unprefixed `/index.php` paths. Add these **Custom locations** (keep the CGI regex rule ahead of the fallback prefix rule):
|
|
|
|
| Match type | Path/pattern | Source | Path rewrite mode and value |
|
|
|---|---|---|---|
|
|
| `regex` | `^/console/cgi-bin/(.*\.cgi(?:/.*)?)$` | `console-cgi` | `regex` → `/$1` |
|
|
| `prefix` | `/console/` | `console-web` | `strip_prefix` |
|
|
| `exact` | `/` | Redirect to `/console/` (302) | Not applicable |
|
|
| `exact` | `/console` | Redirect to `/console/` (301) | Not applicable |
|
|
|
|
The exact and regex rules use Rust's `regex` syntax, not PCRE look-arounds. `strip_prefix` and `regex` rewrite values remove the public path prefix while preserving script names. For example, `GET /console/cgi-bin/health.cgi?x=1` is processed by `console-cgi` as `/health.cgi` with `SCRIPT_FILENAME=/srv/apps/console/cgi-bin/health.cgi`, `SCRIPT_NAME=/console/cgi-bin/health.cgi` and `REQUEST_URI=/console/cgi-bin/health.cgi?x=1`. Likewise, `GET /console/index.php` or `GET /console/` (with `index.php` configured) is executed by `console-web` through PHP-FPM; `/console/assets/style.css` is served statically from that same service. An additional regex PHP location can be configured if PHP scripts need a **different** root or socket; otherwise a separate PHP regex location is unnecessary. You may also replace `console-cgi` with a `fastcgi` responder service for a compatible standalone application.
|
|
|
|
**Protect access:** Enable **Vhost Basic Auth** if the application needs it, using the existing RustProxy credential settings (or another supported authentication provider; only verified Vhost Basic Auth currently maps to `REMOTE_USER`). Avoid publishing admin/CGI directories as static paths. `deny all` for hidden files can be implemented with Vhost ACL/location rules and optional `.htaccess` security rules. Server-level HTTPS, certificate renewal, IPv6 listeners and access/error logging are configured through RustProxy listeners/Vhosts/logs, not inside FastCGI settings.
|
|
|
|
## Migrating from Nginx / Angie
|
|
|
|
| Original directive | RustProxy equivalent |
|
|
|---|---|
|
|
| `listen 80` / `listen 443 ssl` / `http2 on` | Named HTTP / HTTPS listeners, HTTPS Vhost selection, HTTP/2 setting |
|
|
| `return 302 https://$host$request_uri` | Force HTTPS (RustProxy's configured HTTPS redirect status/semantics may differ from 302) |
|
|
| `server_name` | Vhost hostnames |
|
|
| `acme` / `$acme_cert_*` | Certificates → ACME certificate and Vhost certificate selection; not Angie variable import |
|
|
| `access_log` / `error_log` | RustProxy logs and monitoring settings |
|
|
| `auth_basic` / `auth_basic_user_file` | Vhost Basic Auth / credential management, not direct use of Angie `auth_basic_user_file` lines |
|
|
| `location =` with `return` | Exact Custom location with Redirect |
|
|
| `location /prefix/` / `alias` | Web Service `document_root` plus Custom location `strip_prefix` |
|
|
| `location ~ regex` / `rewrite ... break` | Regex Custom location with regex path rewrite |
|
|
| `include fastcgi_params` / `include fastcgi-php.conf` | RustProxy generates CGI/FastCGI core parameters; no Nginx `include` parser |
|
|
| `fastcgi_pass` | Web Service FastCGI endpoint: Unix or loopback TCP |
|
|
| `fastcgi_param SCRIPT_FILENAME` / `SCRIPT_NAME` | Document root, optional URL prefix, location rewrite; do **not** override with untrusted variables |
|
|
| `fastcgi_param REMOTE_USER`, `AUTH_USER` | Verified Vhost Basic Auth identity (when available) |
|
|
| `location ~ /\.` + `deny all` | Web Service hardening and explicit restrictive ACL/location rules |
|
|
|
|
**Not a full Nginx/Angie interpreter.** RustProxy does not accept raw `server { ... }` files, Nginx `include`, arbitrary runtime `fastcgi_param` expressions, or arbitrary system-level PHP/CGI process management. Translate the routing and authentication rules into the panel/API as above. Full location, rewrite and authentication behavior is documented in [Custom locations](CUSTOM_LOCATIONS.md), [Vhosts](VHOSTS.md) and [Access](ACCESS.md).
|
|
|
|
## Verification and troubleshooting
|
|
|
|
```bash
|
|
# Check HTTP -> HTTPS and the application's protected/redirect paths:
|
|
curl -I http://portal.example.test/console/
|
|
curl -Ik https://portal.example.test/console/
|
|
# Use appropriate test credentials, if Basic Auth is enabled:
|
|
curl -ik -u operator https://portal.example.test/console/cgi-bin/health.cgi
|
|
# Direct non-executable file requests under a CGI-only root must be forbidden:
|
|
curl -Ik https://portal.example.test/console/cgi-bin/config.txt
|
|
```
|
|
|
|
- **502 / 504:** inspect the external process, socket permissions, FastCGI stderr (bounded in logs) and timeouts. A Unix socket name alone does not start `fcgiwrap` or PHP-FPM.
|
|
- **403:** check route-to-Web-Service selection, file visibility, symlink policy, script extension allowlist, Vhost authentication and `.htaccess` protection. A CGI-only root intentionally returns 403 for non-executable files.
|
|
- **404:** verify regex location captures, replacement path, actual file under `document_root`, `index` and `try_files` (note that `try_files` candidates are evaluated inside the selected Web Service).
|
|
- **Incorrect `SCRIPT_NAME` / missing authentication identity:** confirm the public `script_name_prefix`, whether the Vhost location strips/replaces the prefix, Basic Auth success and forwarding settings.
|
|
- **Browser downloads a script:** verify the active Web Service and extension. RustProxy refuses to send known executable script extensions as static files when no matching handler is configured.
|
|
|
|
The new protocol paths are covered by source-level tests using a mock FastCGI responder. Before production use, run `./dev.sh check` and `./dev.sh test` with the project toolchain and exercise real PHP-FPM and fcgiwrap instances; mock responders cannot prove OS-level script execution.
|