Nginx php-fpm 504网关错误

Nginx php-fpm 504网关错误

经过几个小时的搜索和调试后我放弃了!

关于长时间运行的 PHP 进程有成千上万个问题和文章,但没有一个能解决我的问题。

我有一个包含以下代码的 PHP 脚本:

$cur = 0; 
    // Second, loop for $timeout seconds checking if process is running 
    while( $cur < 31 ) { 
        sleep(1); 
        $cur += 1; 

       echo "\n ---- $cur ------ \n";
    }

它只是旨在运行 31 秒。

我有一个 Nginx,在 debian 服务器中将 PHP 配置为 fastcgi。

我设置了 max_execution_time = 600

/etc/php5/fpm/php.ini

我甚至把它放在

/etc/php5/cli/php.ini

还设置

request_terminate_timeout = 600

在 /etc/php5/fpm/pool.d/www.conf 中

我还在 nginx.conf http 部分做了这些更改

client_header_timeout   600;
client_body_timeout     600;
send_timeout           600;

fastcgi_read_timeout    600;
fastcgi_send_timeout    600;
client_max_body_size    20m;
fastcgi_buffers         8 128k;
fastcgi_buffer_size     128k;

并将指令放在服务器部分内。这些指令放在 nginx 配置的位置部分内

send_timeout           600;

fastcgi_read_timeout    600;
fastcgi_send_timeout    600;
client_max_body_size    20m;
fastcgi_buffers         8 128k;
fastcgi_buffer_size     128k;

但我仍然在浏览器中遇到网关超时错误!(是的!我重启了 php-fpm 和 nginx 数千次)

你们有什么想法吗?

这是我的 nginx.conf

# cat /etc/nginx/nginx.conf 
user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;

    gzip_min_length 256;

    ##
    # Virtual Host Configs
    ##

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

这是 /etc/nginx/conf.d/increase-timeout.conf

client_header_timeout   600;
client_body_timeout     600;
send_timeout           600;

fastcgi_read_timeout    600;
fastcgi_send_timeout    600;
client_max_body_size    20m;
fastcgi_buffers     8 128k;
fastcgi_buffer_size     128k;

这是我的 /etc/nginx/sites-enabled/mysite

server {
    listen   IP:80;# default_server;## listen for ipv4; this line is default and implied
    #listen   [::]:80 default ipv6only=on; ## listen for ipv6

    root /path/to/root;
    index index.html index.php index.htm;

    server_name DOMAIN; 

    access_log /var/log/nginx/mysite-access.log;
    error_log /var/log/nginx/mysite-error.log debug;


    client_header_timeout   600;
    client_body_timeout     600;
    send_timeout           600;

    fastcgi_read_timeout    600;
    fastcgi_send_timeout    600;
    client_max_body_size    20m;

    location / {

        if ($request_uri ~ ^/index.php.*$) {

            rewrite ^(.*)$ $1 last;
        }

        if ($request_filename = "index.html") {

            break;
        }

        if ($request_uri ~ .*php$) {

            set $test A;
        }

        if ($request_filename != "index.php" ) {

                    set $test "${test}B";
            }

        if ($test = "AB") {

            rewrite ^(.*)$ /index.html last;
        }
    }

    location ~ ^/index.php.*$ {

        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
        set $path_info $fastcgi_path_info;
            fastcgi_param PATH_INFO $path_info;
        fastcgi_keep_conn       on;
                # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;

        send_timeout           600;

        client_max_body_size    20m;

        fastcgi_read_timeout    600;
        fastcgi_send_timeout    600;
        fastcgi_buffer_size             128k;
        fastcgi_buffers                 256 16k;
        fastcgi_busy_buffers_size       256k;
        fastcgi_temp_file_write_size    256k;

                fastcgi_index index.php;
                include fastcgi_params;
        }
}

答案1

尝试告诉 nginx 在脚本运行时禁用输出缓冲:

header('X-Accel-Buffering: no');

话虽如此,我刚刚用一个相当普通的 nginx apache 代理尝试了你的确切示例,除了等待 php 脚本终止后再显示它之外,没有遇到任何问题。

答案2

该错误来自 Nginx,而不是 PHP-FPM。Nginx 抱怨它在 60 秒内没有收到任何内容,这是默认超时时间。 你可以改变它

fastcgi_read_timeout 600s;

相关内容