Nginx+;php fpm:504超时错误-上游超时(110:连接超时)

Nginx+;php fpm:504超时错误-上游超时(110:连接超时),nginx,php,Nginx,Php,这是我们第一次设置Nginx和PHP-FPM。一周前,我们使用Apache为一个网页提供服务,但我们决定尝试Nginx组合,我们面临着几个问题 我们有一个运行Ubuntu14.04 LTS Trusty的虚拟机。在其他实用程序中,机器正在运行带有PHP-FPM和MySQL的Nginx,出现问题的时候如下: 从同一个网络,所有同事(大约10人)都可以毫无问题地访问web,而有一个同事不能(错误504) 从外部看,一些同事可以毫无问题地访问web,而其他人则看到504错误。例如,在我的情况下,我可

这是我们第一次设置Nginx和PHP-FPM。一周前,我们使用Apache为一个网页提供服务,但我们决定尝试Nginx组合,我们面临着几个问题

我们有一个运行Ubuntu14.04 LTS Trusty的虚拟机。在其他实用程序中,机器正在运行带有PHP-FPM和MySQL的Nginx,出现问题的时候如下:

  • 从同一个网络,所有同事(大约10人)都可以毫无问题地访问web,而有一个同事不能(错误504)
  • 从外部看,一些同事可以毫无问题地访问web,而其他人则看到504错误。例如,在我的情况下,我可以通过我的家庭WiFi接入,但如果我使用手机的3G,我就不能。一些同事则面临相反的情况,他们可以通过3G接入,但不能通过WiFi。其他人无法访问
我们得出的结论是,客户端可以连接到Nginx,但Nginx在将请求重定向到FPM(可通过端口7777访问)时未收到响应,并显示超时错误。我们专注于解决Nginx和FPM之间的通信故障,但几个小时后我们还没有找到解决方案

注意:此问题在我们设置的四个虚拟机中重复出现


Nginx错误日志(注意:主机名和ip已被抑制)

文件:/etc/nginx/sites available/default.conf

user www-data;
worker_processes 4;
worker_rlimit_nofile 8192;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
  worker_connections 4096;
}

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

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

  sendfile    on;
  server_tokens on;

  types_hash_max_size 1024;
  types_hash_bucket_size 512;

  server_names_hash_bucket_size 64;
  server_names_hash_max_size 512;

  keepalive_timeout  65;
  tcp_nodelay        on;

  gzip         on;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}
server {
    listen 80;
    root /var/www/html/web;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php; #$is_args$args;
    }

    location ~ ^/(app|app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}
;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;

; All relative paths in this configuration file are relative to PHP's install
; prefix.

; Include one or more files. If glob(3) exists, it is used to include a bunch of
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
include=/etc/php5/fpm/pool.d/*.conf

;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;

[global]
; Pid file
; Default Value: none
pid = /var/run/php5-fpm.pid

request_terminate_timeout=30s

; Error log file
; Default Value: /var/log/php-fpm.log
error_log = /var/log/php5-fpm.log

; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice
log_level = notice

; If this number of child processes exit with SIGSEGV or SIGBUS within the time
; interval set by emergency_restart_interval then FPM will restart. A value
; of '0' means 'Off'.
; Default Value: 0
emergency_restart_threshold = 0

; Interval of time used by emergency_restart_interval to determine when
; a graceful restart will be initiated.  This can be useful to work around
; accidental corruptions in an accelerator's shared memory.
; Available Units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
emergency_restart_interval = 0

; Time limit for child processes to wait for a reaction on signals from master.
; Available units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
process_control_timeout = 0

; The maximum number of processes FPM will fork. This has been design to control
; the global number of processes when using dynamic PM within a lot of pools.
; Use it with caution.
; Note: A value of 0 indicates no limit
; Default Value: 0
; process.max = 128

; Specify the nice(2) priority to apply to the master process (only if set)
; The value can vary from -19 (highest priority) to 20 (lower priority)
; Note: - It will only work if the FPM master process is launched as root
; - The pool process will inherit the master process priority
; unless it specified otherwise
; Default Value: no set
; process.priority = -19

; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
; Default Value: yes
;daemonize = yes

;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;

; See /etc/php5/fpm/pool.d/*.conf
;; For readability, comments have been suppressed.    

[www]

user = www-data
group = www-data

listen = /var/run/php5-fpm.sock
listen.backlog = -1
listen.owner = www-data
listen.group = www-data
listen.mode = 0666
listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 0

ping.response = pong

slowlog = /var/log/php-fpm/www-slow.log

catch_workers_output = no

php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
文件:/etc/nginx/fastcgi_参数 注意:仅行
fastcgi\u param REQUEST\u SCHEME$SCHEME已添加

fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx/$nginx_version;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;
文件:/etc/php5/fpm/php fpm.conf

user www-data;
worker_processes 4;
worker_rlimit_nofile 8192;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
  worker_connections 4096;
}

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

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

  sendfile    on;
  server_tokens on;

  types_hash_max_size 1024;
  types_hash_bucket_size 512;

  server_names_hash_bucket_size 64;
  server_names_hash_max_size 512;

  keepalive_timeout  65;
  tcp_nodelay        on;

  gzip         on;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}
server {
    listen 80;
    root /var/www/html/web;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php; #$is_args$args;
    }

    location ~ ^/(app|app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}
;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;

; All relative paths in this configuration file are relative to PHP's install
; prefix.

; Include one or more files. If glob(3) exists, it is used to include a bunch of
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
include=/etc/php5/fpm/pool.d/*.conf

;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;

[global]
; Pid file
; Default Value: none
pid = /var/run/php5-fpm.pid

request_terminate_timeout=30s

; Error log file
; Default Value: /var/log/php-fpm.log
error_log = /var/log/php5-fpm.log

; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice
log_level = notice

; If this number of child processes exit with SIGSEGV or SIGBUS within the time
; interval set by emergency_restart_interval then FPM will restart. A value
; of '0' means 'Off'.
; Default Value: 0
emergency_restart_threshold = 0

; Interval of time used by emergency_restart_interval to determine when
; a graceful restart will be initiated.  This can be useful to work around
; accidental corruptions in an accelerator's shared memory.
; Available Units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
emergency_restart_interval = 0

; Time limit for child processes to wait for a reaction on signals from master.
; Available units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
process_control_timeout = 0

; The maximum number of processes FPM will fork. This has been design to control
; the global number of processes when using dynamic PM within a lot of pools.
; Use it with caution.
; Note: A value of 0 indicates no limit
; Default Value: 0
; process.max = 128

; Specify the nice(2) priority to apply to the master process (only if set)
; The value can vary from -19 (highest priority) to 20 (lower priority)
; Note: - It will only work if the FPM master process is launched as root
; - The pool process will inherit the master process priority
; unless it specified otherwise
; Default Value: no set
; process.priority = -19

; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
; Default Value: yes
;daemonize = yes

;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;

; See /etc/php5/fpm/pool.d/*.conf
;; For readability, comments have been suppressed.    

[www]

user = www-data
group = www-data

listen = /var/run/php5-fpm.sock
listen.backlog = -1
listen.owner = www-data
listen.group = www-data
listen.mode = 0666
listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 0

ping.response = pong

slowlog = /var/log/php-fpm/www-slow.log

catch_workers_output = no

php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
文件:/etc/php5/fpm/pool.d/www.conf

user www-data;
worker_processes 4;
worker_rlimit_nofile 8192;
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
  worker_connections 4096;
}

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

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

  sendfile    on;
  server_tokens on;

  types_hash_max_size 1024;
  types_hash_bucket_size 512;

  server_names_hash_bucket_size 64;
  server_names_hash_max_size 512;

  keepalive_timeout  65;
  tcp_nodelay        on;

  gzip         on;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;
}
server {
    listen 80;
    root /var/www/html/web;

    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php; #$is_args$args;
    }

    location ~ ^/(app|app_dev|config)\.php(/|$) {
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }

    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}
;;;;;;;;;;;;;;;;;;;;;
; FPM Configuration ;
;;;;;;;;;;;;;;;;;;;;;

; All relative paths in this configuration file are relative to PHP's install
; prefix.

; Include one or more files. If glob(3) exists, it is used to include a bunch of
; files from a glob(3) pattern. This directive can be used everywhere in the
; file.
include=/etc/php5/fpm/pool.d/*.conf

;;;;;;;;;;;;;;;;;;
; Global Options ;
;;;;;;;;;;;;;;;;;;

[global]
; Pid file
; Default Value: none
pid = /var/run/php5-fpm.pid

request_terminate_timeout=30s

; Error log file
; Default Value: /var/log/php-fpm.log
error_log = /var/log/php5-fpm.log

; Log level
; Possible Values: alert, error, warning, notice, debug
; Default Value: notice
log_level = notice

; If this number of child processes exit with SIGSEGV or SIGBUS within the time
; interval set by emergency_restart_interval then FPM will restart. A value
; of '0' means 'Off'.
; Default Value: 0
emergency_restart_threshold = 0

; Interval of time used by emergency_restart_interval to determine when
; a graceful restart will be initiated.  This can be useful to work around
; accidental corruptions in an accelerator's shared memory.
; Available Units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
emergency_restart_interval = 0

; Time limit for child processes to wait for a reaction on signals from master.
; Available units: s(econds), m(inutes), h(ours), or d(ays)
; Default Unit: seconds
; Default Value: 0
process_control_timeout = 0

; The maximum number of processes FPM will fork. This has been design to control
; the global number of processes when using dynamic PM within a lot of pools.
; Use it with caution.
; Note: A value of 0 indicates no limit
; Default Value: 0
; process.max = 128

; Specify the nice(2) priority to apply to the master process (only if set)
; The value can vary from -19 (highest priority) to 20 (lower priority)
; Note: - It will only work if the FPM master process is launched as root
; - The pool process will inherit the master process priority
; unless it specified otherwise
; Default Value: no set
; process.priority = -19

; Send FPM to background. Set to 'no' to keep FPM in foreground for debugging.
; Default Value: yes
;daemonize = yes

;;;;;;;;;;;;;;;;;;;;
; Pool Definitions ;
;;;;;;;;;;;;;;;;;;;;

; See /etc/php5/fpm/pool.d/*.conf
;; For readability, comments have been suppressed.    

[www]

user = www-data
group = www-data

listen = /var/run/php5-fpm.sock
listen.backlog = -1
listen.owner = www-data
listen.group = www-data
listen.mode = 0666
listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 0

ping.response = pong

slowlog = /var/log/php-fpm/www-slow.log

catch_workers_output = no

php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on

我们到目前为止所做的工作(这些配置可能不会反映在上述文件中)

  • 将Nginx和FPM用户设置为相同(www数据)
  • 重新启动服务和计算机
  • 在nginx配置中设置代理读取超时
  • max\u execution\u time
    request\u terminate\u timeout
    设置为300(分别在php.ini和nginx.conf上)
  • 增加nginx配置上的
    worker\u进程
    worker\u连接数
  • 在nginx config和php fpm上,将
    Listen
    的值从/var/run/php5-fpm.sock更改为127.0.0.1:7777

我们将继续研究解决方案,如果找到,将发布在这里。谢谢

解决了

问题似乎存在于PHP代码中。使用xdebug时,没有设置
remote_host
属性,我们仍然不知道为什么xdebug过程会根据远程主机随机执行

注意:当我们只使用Apache时,这个问题从未发生过。当使用Nginx+phpfpm时,问题就开始出现了,但不管怎样,最终问题解决了

谢谢。

试试这个

server {
    listen       80;
    server_name  *.domain_name.com ;

    #add this begin
    large_client_header_buffers 4 16k;
    client_max_body_size 300m;
    client_body_buffer_size 128k;
    proxy_connect_timeout 600;
    proxy_read_timeout 600;
    proxy_send_timeout 600;
    proxy_buffer_size 64k;
    proxy_buffers   4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    #add this end
    ........................
}

我有同样的问题,但我没有使用xdebug。我使用的是流浪者虚拟机。它通常工作得很好,但有时我会遇到这种情况。让流浪汉停下来,让流浪汉爬起来——解决了这个问题,但我想只是暂时的。