【问题标题】:Latency issue with Nginx + Php-fpmNginx + Php-fpm 的延迟问题
【发布时间】:2013-12-09 08:50:52
【问题描述】:

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

  1. 您想访问 www.example.com
  2. 由于您已登录,php 将重定向 301 发送到 /user/home。

使用 Apache 的 php 重定向在几百毫秒内完成,而使用 nginx 大约需要 2 秒!

php 重定向功能:

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

我的应用程序虚拟主机:

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(我删除了 cmets)

[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 = /

【问题讨论】:

  • 您是使用 php 模块还是使用 php-fpm 运行 apache?这也可能是一个 php-fpm 配置问题(即关闭所有工作线程而不是保持一些打开)如果你在 unix 上运行它,你也可以为 php-fpm 使用一个 unix 套接字文件(稍微加速)。
  • 我们用 php 模块(没有 php-fpm)运行它,你说得对,这很可能是 php-fpm 配置问题我会用我的 php-fpm.conf 和我的更新我的问题www-pool.conf :-)

标签: redirect nginx php


【解决方案1】:

调整 php-fpm 设置就可以了 :-)

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

瞧。

【讨论】: