Eras
Eras

Reputation: 428

Latency issue with Nginx + Php-fpm

I have a php application that used to be running on Apache I just switched to nginx. My php application has a php router, so for some pages the flow is like is :

  1. You want to reach www.example.com
  2. Since you're logged in php sends a redirect 301 to /user/home.

With Apache the php redirection is being done in a couple hundred ms while with nginx it takes ~2s !

The php redirect funtcion :

public function redirect($url, $code = 301)
{
    if($code) {
        $codeHeader = false;
        switch ($code) {
            case 301:
                $codeHeader = "HTTP/1.1 301 Moved Permanently";
                break;
        }
        if($codeHeader){
            header($codeHeader);
        }
    }
            header("Location: $url");
            exit;
    }

My nginx.conf :

user www-data;
worker_processes 8;

pid /var/run/nginx.pid;

events {
    worker_connections 2048;
    multi_accept on;
    use epoll;
}

http {


    sendfile on; 
    tcp_nopush on;
    tcp_nodelay on; 
    keepalive_timeout 30;
    reset_timedout_connection on;
    client_body_timeout 10;
    server_tokens off;


    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local]  '
                '"$request" $status $body_bytes_sent '
                '"$http_referer" "$http_user_agent"';

    access_log /var/log/nginx/access.log main buffer=16k;
    error_log /var/log/nginx/error.log crit;

    gzip on;
    gzip_min_length 10240;
    gzip_proxied expired no-cache no-store private auth;
    gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
    gzip_disable "MSIE [1-6]\.";
    application/xml application/xml+rss text/javascript;


    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

My application vhost :

server {
    listen      80;
    root        /srv/www/example;
    server_name www.example.com;
    access_log  /var/log/nginx/access.example.log;
    error_log   /var/log/nginx/error.example.log;
    index       /index.php;

    location / {
        try_files $uri $uri/ /index.php?q=$uri&$args;
    } 

    location ~ \..*/.*\.php$ {
        return 403;
    }

    # Block hidden files
    location ~ (^|/)\. {
        return 403;
    } 

    location ~ \.(php|phtml)$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass_header X-Real-IP;
        fastcgi_pass_header X-Forwarded-For;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PHP_VALUE "error_log=/var/log/nginx/error.example.log";
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
    }

}

Anyone knows where the lag is comming from ? :-)

UPDATE : It might indeed be a php-fpm problem so here's my php-fpm.conf ( I removed the comments )

[global]
pid = /var/run/php5-fpm.pid

error_log = /var/log/php5-fpm.log

include=/etc/php5/fpm/pool.d/*.conf

And my www-pool

user = www-data
group = www-data

listen = 127.0.0.1:9000

pm = dynamic

pm.max_children = 10

pm.start_servers = 4

pm.min_spare_servers = 2

pm.max_spare_servers = 6

chdir = /

Upvotes: 4

Views: 1612

Answers (1)

Eras
Eras

Reputation: 428

Tweaking the php-fpm settings did it :-)

pm.max_children = 100
pm.start_servers = 25
pm.min_spare_servers = 25
pm.max_spare_servers = 50

Voila.

Upvotes: 2

Related Questions