- Introduced `FastcgiPlugin` for automatic HAProxy `fcgi-app` configuration generation. - Added example Docker Compose setup for PHP-FPM with FastCGI. - Updated documentation with detailed examples and usage instructions for FastCGI. - Included test cases to validate plugin behavior and generated configurations. - Enhanced `easymapping` to support `fcgi-app` definitions in global configs. |
||
|---|---|---|
| .. | ||
| index.php | ||
| info.php | ||
| README.md | ||
| test-path-info.php | ||
Sample PHP Application for FastCGI Plugin
This directory contains a sample PHP application that demonstrates the FastCGI plugin functionality with EasyHAProxy.
Files
index.php
The main page that displays:
- PHP version and configuration
- FastCGI environment variables set by EasyHAProxy
- How the FastCGI plugin works
- Links to test pages
Access: http://phpapp.local/
info.php
Standard phpinfo() page showing complete PHP configuration.
Access: http://phpapp.local/info.php
test-path-info.php
Demonstrates PATH_INFO support for RESTful URL routing.
Examples:
http://phpapp.local/test-path-info.php/usershttp://phpapp.local/test-path-info.php/users/123http://phpapp.local/test-path-info.php/api/v1/products
FastCGI Environment Variables
The FastCGI plugin generates an fcgi-app configuration that defines these CGI parameters for HAProxy to use:
| Variable | Description | Example |
|---|---|---|
SCRIPT_FILENAME |
Full path to PHP script | /var/www/html/index.php |
DOCUMENT_ROOT |
Document root directory | /var/www/html |
SCRIPT_NAME |
Script path | /index.php |
REQUEST_URI |
Full request URI with query | /index.php?page=1 |
QUERY_STRING |
Query string parameters | page=1&limit=10 |
REQUEST_METHOD |
HTTP method | GET, POST, etc. |
CONTENT_TYPE |
Request content type | application/json |
CONTENT_LENGTH |
Request body length | 1024 |
SERVER_NAME |
Virtual host name | phpapp.local |
SERVER_PORT |
Server port | 80 or 443 |
HTTPS |
SSL status | on or off |
PATH_INFO |
Extra path info (optional) | /users/123 |
How It Works
-
FastCGI plugin generates configuration (at startup)
- Creates an
fcgi-appsection with CGI parameter definitions - Includes
docroot,index, andpath-infosettings - Adds
use-fcgi-appdirective to the backend
- Creates an
-
Request arrives at HAProxy (port 80)
- URL:
http://phpapp.local/index.php
- URL:
-
HAProxy uses the fcgi-app configuration
- Sets
SCRIPT_FILENAMEto/var/www/html/index.php - Sets
DOCUMENT_ROOTto/var/www/html - Sets all other CGI variables based on the request
- Handles directory requests (appends
index.php)
- Sets
-
HAProxy forwards to PHP-FPM via FastCGI protocol
- Host:
php-fpm(container name) - Port:
9000(TCP) or Unix socket - Protocol:
fcgi - Sends CGI parameters in FastCGI format
- Host:
-
PHP-FPM executes the script
- Reads the PHP file from
SCRIPT_FILENAME - Processes the PHP code with CGI environment
- Returns HTML/JSON response
- Reads the PHP file from
-
HAProxy sends response to client
Customizing
You can customize the FastCGI plugin configuration in docker-compose-php-fpm.yml:
labels:
# Change document root
easyhaproxy.http.plugin.fastcgi.document_root: /var/www/public
# Change default index file
easyhaproxy.http.plugin.fastcgi.index_file: app.php
# Disable PATH_INFO
easyhaproxy.http.plugin.fastcgi.path_info: "false"
# Add custom FastCGI parameters
easyhaproxy.http.plugin.fastcgi.custom_params: '{"PHP_VALUE":"memory_limit=256M","APP_ENV":"production"}'
Adding Your Own PHP Application
Replace the contents of this directory with your own PHP application:
# Remove sample files
rm -rf php-app/*
# Copy your PHP application
cp -r /path/to/your/app/* php-app/
# Restart the stack
docker compose -f docker-compose-php-fpm.yml restart
Make sure your application's entry point matches the index_file configuration (default: index.php).
Troubleshooting
"File not found" error
Check that:
- The file exists in the
php-app/directory - The
document_rootmatches the container path (/var/www/html) - The volume mount is correct in docker-compose.yml
PATH_INFO not working
Ensure path_info is enabled in the plugin configuration:
easyhaproxy.http.plugin.fastcgi.path_info: "true"
PHP-FPM connection error
Verify:
- The
localport: 9000is set correctly - The
proto: fcgiparameter is set - Both containers are running and can communicate
View logs:
docker compose -f docker-compose-php-fpm.yml logs php-fpm
docker compose -f docker-compose-php-fpm.yml logs haproxy
Check connectivity:
docker compose -f docker-compose-php-fpm.yml exec haproxy ping php-fpm