Nginx + Php-fpm的延迟问题

时间:2013-12-09 08:50:52

标签: redirect nginx php

我有一个曾经在Apache上运行的php应用程序我刚刚切换到nginx。 我的php应用程序有一个php路由器,所以对于某些页面,流程就像是:

  1. 您想要访问www.example.com
  2. 由于您已登录,因此php会将重定向301发送到/ user / home。
  3. 使用Apache,php重定向在几百毫秒内完成,而使用nginx需要~2秒!

    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;
        }
    

    我的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/*;
    }
    

    我的应用程序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;
        }
    
    }
    

    任何人都知道延迟来自哪里? : - )

    更新: 它可能确实是一个php-fpm问题所以这是我的php-fpm.conf(我删除了评论)

    [global]
    pid = /var/run/php5-fpm.pid
    
    error_log = /var/log/php5-fpm.log
    
    include=/etc/php5/fpm/pool.d/*.conf
    

    我的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 = /
    

1 个答案:

答案 0 :(得分:2)

调整php-fpm设置: - )

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

瞧。