Skip to content

Instantly share code, notes, and snippets.

@franz-josef-kaiser
Created October 8, 2021 14:06
Show Gist options
  • Save franz-josef-kaiser/c470432c6b4ab6d2b62c60fc23371a5d to your computer and use it in GitHub Desktop.
Save franz-josef-kaiser/c470432c6b4ab6d2b62c60fc23371a5d to your computer and use it in GitHub Desktop.
Docker Nginx and PHP-FPM Config including Health Check, Alive Check and Status Pages

PHP-FPM and Nginx Health Check and Status pages

Access

  1. localhost/stub_status - Nginx status
  2. localhost?full&json - PHP FPM Full status (including workers) as JSON
  3. localhost?full&html - PHP FPM Full status (including workers) as HTML
  4. localhost/ping - PHP FPM alive check

TODO

  1. Configure Docker container name in nginx.conf in the fastcgi_pass in the location blocks and in the upstream directive incl. port.
  2. Configure FPM port in php-fpm.conf.
  3. Configure /ping location in nginx.conf at location ~ ^/(status|ping/?) and in php-fpm.conf using ping.path.
  4. Configure /ping response in php-fpm.conf using ping.response.
  5. Configure /status location in nginx.conf at location ~ ^/(status|ping/?).
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param REQUEST_SCHEME $scheme;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
# Configuration File - Nginx Server Configs
# https://nginx.org/en/docs/
worker_processes auto;
worker_rlimit_nofile 8192;
events {
worker_connections 8000;
}
pid /var/run/nginx.pid;
error_log /dev/stderr warn;
http {
log_format main '[$remote_addr] - [USER:$remote_user] [$time_iso8601] [$status] "$request" ';
access_log /dev/stdout;
keepalive_timeout 20s;
sendfile on;
tcp_nopush on;
index index.php;
include mime.types;
default_type application/octet-stream;
charset utf-8;
upstream php {
zone php 64k;
server php:9000;
}
server {
listen 80;
server_name localhost;
root /app/docroot;
index index.php index.html;
location / {
root /app/app/public;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
root /app/app/public;
try_files $uri =404;
include fastcgi.conf;
fastcgi_pass php;
}
# PHP FPM server stats and health status
location ~ ^/(status|ping/?)$ {
satisfy any;
access_log off;
allow all;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
include fastcgi.conf;
fastcgi_pass php;
}
# Nginx server stats
location ~ ^/(stub_status/?)$ {
stub_status on;
allow all;
access_log off;
}
}
}
[global]
; Error log file
error_log = /dev/stderr
daemonize = no
; Possible Values: alert, error, warning, notice, debug
log_level = notice
; https://github.com/docker-library/php/pull/725#issuecomment-443540114
log_limit = 8192
emergency_restart_threshold = 3
emergency_restart_interval = 60s
[www]
user = www-data
group = www-data
listen = 9000
; if we send this to /proc/self/fd/1, it never appears
access.log = /dev/stdout
access.format = "%R - %u %t \"%m %r%Q%q\" %s %f %{mili}d %{kilo}M %C%%"
; Ensure worker stdout and stderr are sent to the main error log.
catch_workers_output = yes
decorate_workers_output = no
pm = dynamic
pm.max_children = 50
pm.start_servers = 4
pm.min_spare_servers = 4
pm.max_spare_servers = 6
pm.max_requests = 500
pm.status_path = /status
ping.path = /ping
ping.response = pong
chdir = /
; Give PHP some environment information
env[DOCROOT] = $DOCROOT
env[CONTAINERID] = $CONTAINERID
env[PATH] = $PATH
env[WEBSITE] = $WEBSITE
env[BASE_URL] = $BASE_URL
env[BASE_PATH] = $BASE_PATH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment