redirect - Nginx + Php-fpm 的延迟问题

标签 redirect nginx php

我有一个以前在 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(我删除了评论)

[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-fpm 设置就成功了:-)

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

瞧。

关于redirect - Nginx + Php-fpm 的延迟问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20466568/

相关文章:

redirect - 在不丢失反向链接的情况下进行 301 NGINX 重定向的正确方法

apache - 将带有查询字符串的 url 重定向到 seo 友好的目录,如 urls

javascript - 如何确定用户何时离开网页(不包括某些链接)

php - Laravel orderBy 使用 2 个表?

php - Magento API v2 PHP 错误

php - 当尝试访问 php 中的父构造函数时,调用 PHP 中非对象的成员函数 query()

ruby-on-rails - Heroku 应用程序自动重定向到 HTTPS

node.js - 如何在windows中使用NGINX为node js配置HTTPS

php - 找不到类 PDO - 刚刚升级到 PHP 5.6 (Debian Wheezy)

ruby-on-rails - capistrano 部署后 ec2 服务器中缺少 puma.sock