# 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. The editor generates a unique ID from the profile name; use **Change** to provide a manual ID when creating a profile. Saved IDs are read-only because Web Services refer to them. 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. ## Nagios Core: PHP pages and CGI checks under `/nagios/` The following is a **site-specific example** of the generic three-service arrangement above. It replaces common Angie/Nginx rules for a Nagios Core UI; the FastCGI engine does not contain any Nagios-specific code. Adjust paths and socket names to your installation. **Prerequisites:** Nagios static/PHP files are under `/usr/local/nagios/share/`, executable CGI files under `/usr/local/nagios/sbin/`, PHP-FPM is listening on `unix:/run/php/php8.3-fpm.sock` and fcgiwrap on `unix:/run/fcgiwrap.socket`. Verify that the RustProxy operating-system account can read the site, traverse both document roots and connect to both Unix sockets. Do not change socket permissions to world-writable as a workaround. CGI programs may require Nagios-specific OS users/groups to read status data. 1. Under **Web Server → FastCGI profiles**, create `nagios-php` with **handler `php_fpm`**, enabled, endpoint `unix:/run/php/php8.3-fpm.sock`, executable extensions `php` and public `SCRIPT_NAME` prefix `/nagios`. Leave `PHP_VALUE` / `PHP_ADMIN_VALUE` at defaults unless the PHP-FPM pool genuinely needs overrides. 2. Create `nagios-cgi` with **handler `fcgiwrap`**, enabled, endpoint `unix:/run/fcgiwrap.socket`, executable extensions `cgi`, public `SCRIPT_NAME` prefix `/nagios/cgi-bin`, and `PATH_INFO` disabled unless a specific script uses it. This handler does **not** use `PHP_VALUE` or `PHP_ADMIN_VALUE`. 3. Create these **Web Services** (the profile and service are separate objects): | Web Service ID | Document root | Shared profile | Index files | `try_files` | |---|---|---|---|---| | `nagios-web` | `/usr/local/nagios/share` | `nagios-php` | `index.php`, `index.html`, `index.htm` | `$uri`, `$uri/`, `=404` | | `nagios-cgi-scripts` | `/usr/local/nagios/sbin` | `nagios-cgi` | none | `$uri`, `=404` | | `nagios-empty` | An existing, empty directory such as `/srv/rustproxy/empty` | disabled | none | `=404` | Keep `symlink_policy=deny` for executable Web Services. The empty default service avoids accidentally exposing the same files outside `/nagios/`. 4. Create a **Vhost** for `n.linuxiarz.pl` on HTTP and HTTPS listeners. Use `nagios-empty` as the default Web Server source; configure a TLS certificate (RustProxy ACME or imported), HTTPS redirection and HTTP/2 as required. Configure Vhost **Basic Auth** with users managed by RustProxy. The Angie `auth_basic_user_file` directive is **not** read automatically; verified Vhost Basic Auth is what supplies `REMOTE_USER` / `AUTH_USER` to CGI. Only enable `pass_authorization` when a script requires `HTTP_AUTHORIZATION`. 5. In **Vhost → Custom locations**, add these entries in the order shown. Choose action **Web Server** for the two service routes, **Redirect** for the first two, and **Local response** for the hidden-file deny. Enter full HTTPS URLs for redirect targets (the Custom location redirect editor requires an absolute URL). | Match | Pattern | Action / target | Path rewrite | |---|---|---|---| | `exact` | `/` | `302` to `https://n.linuxiarz.pl/nagios/` | — | | `exact` | `/nagios` | `301` to `https://n.linuxiarz.pl/nagios/` | — | | `regex` | `^/nagios/(?:[^/]+/)*\.[^/]+(?:/|$)` | Local response `403`, no content | — | | `regex` | `^/nagios/cgi-bin/(.*\.cgi)$` | Web Service `nagios-cgi-scripts` | `regex` → `/$1` | | `prefix` | `/nagios/` | Web Service `nagios-web` | `strip_prefix` | The hidden-file rule must be tested before the CGI regex so a dot-prefixed executable cannot bypass it. An additional PHP-specific regex is **unnecessary** here: `nagios-web` serves static files and dispatches `.php` to its PHP-FPM profile. If you need different PHP socket/root rules, use an additional PHP-specific Web Service and a regex Custom location instead. **Expected mapping:** `GET /nagios/` finds `/usr/local/nagios/share/index.php` (or the configured existing index), `GET /nagios/stylesheets/style.css` stays static, and `GET /nagios/cgi-bin/status.cgi?host=all` selects the CGI Web Service. The latter sends `SCRIPT_FILENAME=/usr/local/nagios/sbin/status.cgi`, `SCRIPT_NAME=/nagios/cgi-bin/status.cgi`, original `REQUEST_URI`, `QUERY_STRING=host=all` and `REMOTE_USER` when RustProxy Basic Auth has successfully authenticated the user. Do **not** add `$1` expressions into Custom FastCGI parameters: core script and identity fields are calculated safely by RustProxy. Verify after applying the configuration: ```bash curl -I http://n.linuxiarz.pl/ curl -Ik https://n.linuxiarz.pl/nagios curl -ik -u operator https://n.linuxiarz.pl/nagios/ curl -ik -u operator 'https://n.linuxiarz.pl/nagios/cgi-bin/status.cgi?host=all' curl -Ik https://n.linuxiarz.pl/nagios/.htaccess ``` Expect redirects for `/` and `/nagios`, a Basic Auth challenge without credentials, a PHP/static page for `/nagios/`, FastCGI output (not CGI source code) for the CGI URL, and `403` for dotfiles (a Basic Auth challenge may happen first). A `502` suggests a missing backend/socket or bad socket permissions; a `403` can also mean the script is inaccessible/non-executable, and `404` can mean a wrong rewrite or document root. The exact Nagios page may require additional stylesheets/aliases or Nagios-specific CGI environment and permissions. Test these against a real installed Nagios, not only a mock FastCGI server. ## Connection handling Each FastCGI request uses one new connection to the configured endpoint (no `FCGI_KEEP_CONN` pooling). A PHP-FPM worker serves one connection at a time, so keeping idle connections open would pin workers that other requests need. - TCP endpoints are opened with `TCP_NODELAY`; the request records (`BEGIN_REQUEST`, `PARAMS`, `STDIN`) are written through a 16 KiB buffer, so a request without a body is sent in a single write. With a request body, the parameters are sent first and the body follows as it arrives from the client. - Responses are read through a 32 KiB buffer. `STDOUT` records that have already arrived are merged into one body chunk of up to 64 KiB; RustProxy never waits for more output to fill a chunk, so `flush()` in the application still reaches the client immediately. - The connect/request timeouts, concurrency limit, header limit and stderr limit are unchanged. ## 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. ### `.htaccess` front-controller suffixes When `.htaccess` compatibility is enabled, `AcceptPathInfo On` allows an eligible FastCGI script to receive a rewritten `script.php/extra/` URL even if **PATH_INFO** is disabled at Web Service level. `AcceptPathInfo Off` overrides the Web Service toggle and denies suffix routing; `AcceptPathInfo Default` restores the Web Service setting. `SCRIPT_FILENAME` is always the existing script file under the Web Service document root. `PATH_INFO` is the path after that script, including the trailing slash, and `REQUEST_URI` remains the original requested URL. The FastCGI endpoint is still configured in the Web Service; `.htaccess` cannot select a socket. Explicit index lists remain unchanged; the default static index list also tries `index.php` last on PHP-FPM services handling `.php`. Re-apply the Web Service when the `.htaccess` file changes.