Skip to content

Instantly share code, notes, and snippets.

@lidio601
Last active October 29, 2018 18:04
Show Gist options
  • Save lidio601/efa630c124e50953a6e0331b87b86a51 to your computer and use it in GitHub Desktop.
Save lidio601/efa630c124e50953a6e0331b87b86a51 to your computer and use it in GitHub Desktop.
Nginx + PHPfpm timeout test

Nginx + PHPfpm timeout test

Test the connection timeout by tweaking those parameters: fastcgi_read_timeout, request_terminate_timeout, set_time_limit.

  • If hitting the fastcgi_read_timeout setting, Nginx will terminate the HTTP connection with a HTTP 504
  • If hitting the request_terminate_timeout setting, PHPfpm will terminate the HTTP connection with a HTTP 502
  • If hitting the set_time_limit setting, PHP itself will terminate the HTTP connection with a HTTP 200
version: '2'
services:
phpfpm:
image: php:7.1-fpm
volumes:
- "./:/var/www/html"
- ./phpfpm.conf:/usr/local/etc/php-fpm.d/zzz-www.conf
nginx:
image: nginx:stable
volumes:
- "./:/var/www/html"
- ./nginx.conf:/etc/nginx/conf.d/zzz-nginx.conf
ports:
- 8888:8080
links:
- phpfpm
<?php
/**
* @see https://easyengine.io/tutorials/php/increase-script-execution-time/
* @see https://stackoverflow.com/questions/16002268/prevent-nginx-504-gateway-timeout-using-php-set-time-limit
*/
register_shutdown_function(function() {
echo "shutdown function called\n";
});
// If hit, returns HTTP 200 + PHP "Fatal error: Maximum execution time"
set_time_limit(15); // Set time limit
for (;;) ; // Busy wait
server {
listen 8080 default_server;
server_name _;
root /var/www/html;
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass phpfpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SERVER_NAME $http_host;
fastcgi_intercept_errors on;
# If hit, returns 504 Gateway Time-out
# http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_read_timeout
fastcgi_read_timeout 5;
# (number of seconds)
}
}
}
pm = static
pm.max_children = 20
pm.max_requests = 10000
; number of seconds
; returns 502 Bad Gateway
request_terminate_timeout = 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment