Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm currently running Nginx + PHP-FPM in my server for my websites. Today when I run Apache "ab" tool I noticed horrible response times, average 5595 and max 17415ms to respond.

My Nginx config file:

worker_processes  4;
error_log  /var/log/nginx/error.log info;
events {
 worker_connections 1024;
 use epoll;
 multi_accept on;
http {
 server_name_in_redirect off;
 server_names_hash_max_size 10240;
 server_names_hash_bucket_size 1024;
 include    mime.types;
 default_type  application/octet-stream;
index index.html index.htm index.php;
 server_tokens off;
 sendfile on;
 tcp_nopush on;
 tcp_nodelay on;
 keepalive_timeout  15;
 gzip on;
 gzip_vary on;
 gzip_disable "MSIE [1-6]\.";
 gzip_proxied any;
 gzip_http_version 1.1;
 gzip_min_length  1400;
 gzip_comp_level  9;
 gzip_buffers  16 8k;
 gzip_types    text/plain text/xml text/css application/x-javascript application/xml image/png image/x-icon image/gif image/jpeg application/xml+rss text/javascript application/atom+xml;
 ignore_invalid_headers on;
 client_header_timeout  10m;
 client_body_timeout 10m;
 send_timeout     10m;
 recursive_error_pages on;

 keepalive_requests 100;
 reset_timedout_connection on;
 connection_pool_size  256;
 client_header_buffer_size 256k;
 large_client_header_buffers 4 256k;
 client_max_body_size 200M; 
 client_body_buffer_size 128k;
 request_pool_size  32k;
 output_buffers   4 32k;
 postpone_output  1460;
 proxy_temp_path  /tmp/nginx_proxy/;
 client_body_in_file_only on;
 log_format bytes_log "$msec $bytes_sent .";

 ## Proxy options
  proxy_buffering           on;
  proxy_cache_min_uses       3;
  proxy_cache_path          /etc/nginx/proxy_temp/ levels=1:2 keys_zone=cache:10m inactive=10m max_size=1000M;
  proxy_cache_valid         any 10m;
  proxy_ignore_client_abort off;
  proxy_intercept_errors    on;
  proxy_next_upstream       error timeout invalid_header;
  proxy_redirect            off;
  proxy_set_header          X-Forwarded-For $remote_addr;
  proxy_connect_timeout     60;
  proxy_send_timeout        60;
  proxy_read_timeout        60;

 include "/etc/nginx/vhosts/*";

My PHP-FPM config (only relevant parameters):

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 50
php_admin_value[cgi.fix_pathinfo] = 0
php_admin_value[memory_limit] = 128M

My Server

CPU Intel i3-540 3.06GHz with 4 processors
CentOS 5.6 x64

# ulimit -n

Benchmark ploted graphic

Almost forgot, the php-fpm is handled by nginx:

server {

         location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(.*)$;

               fastcgi_index  index.php;
               include fastcgi_params;
              fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_param PATH_TRANSLATED $document_root$fastcgi_script_name;
               fastcgi_param SERVER_NAME $http_host;
               fastcgi_ignore_client_abort off;

Does anyone have any tips on how I can optimize it?

share|improve this question

migrated from Oct 20 '11 at 10:22

This question came from our site for professional and enthusiast programmers.

Are you sure Nginx is the problem? What's the CPU use / load averages / what're the top CPU users / how much memory & swap are you using / etc. (The top few lines of "top -n 1" should answer all of these questions). If you are sure it's nginx, is there anything in the error log? What ab settings are you using to get these results? – Shish Oct 20 '11 at 10:45

You're benchmarking PHP, not Nginx. To expect 600+ request per second with PHP requires some very solid code and a quite fast server.

Where you're probably going wrong is that you think Nginx is like Apache - which is very far from the truth. In a typical setup Apache will embed the PHP executable inside its own processes.

You say the php-fpm is handled by nginx: but it's not. The PHP is passed via fastcgi to php-fpm. Fastcgi is just a communication protocol. PHP is handled by PHP itself and if you want to optimize it you should optimize your PHP code.

share|improve this answer

Your pm.max_spare_servers = 50 is too high. Try with this config:

pm = dynamic
pm.max_children = 30
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 4
pm.max_requests = 500

read this to understand the parameters and how to calculate them.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.