Lần đầu tiên chúng tôi thiết lập Nginx và PHP-FPM. Một tuần trước, chúng tôi đã sử dụng Apache để phục vụ một trang web, nhưng chúng tôi đã quyết định thử nghiệm sự kết hợp Nginx và chúng tôi đang đối mặt với một số vấn đề.Nginx + php-fpm: lỗi hết thời gian chờ 504 - thời gian hết hạn (110: Kết nối hết thời gian)
Chúng tôi có Máy ảo chạy Ubuntu 14.04 LTS Trusty. Trong số các tiện ích khác, máy đang chạy Nginx với PHP-FPM và MySQL, và đây là khi sự cố xảy ra:
- Từ cùng một mạng, tất cả đồng nghiệp (khoảng 10) đều có thể truy cập web mà không gặp sự cố. là một đồng nghiệp không thể (Error 504).
- Từ bên ngoài, một số đồng nghiệp có thể truy cập web mà không gặp sự cố và những người khác thấy lỗi 504. Ví dụ, trong trường hợp của tôi tôi có thể truy cập thông qua WiFi nhà của tôi nhưng nếu tôi sử dụng 3G của điện thoại di động tôi không thể. Một số đồng nghiệp đã đối diện ngược lại, họ có thể truy cập thông qua 3G nhưng không có WiFi. Và những người khác không thể truy cập.
Chúng tôi đã đi đến kết luận rằng khách hàng có thể kết nối với Nginx, nhưng Nginx, khi chuyển hướng yêu cầu tới FPM (có thể truy cập qua cổng 7777) không nhận được phản hồi và hiển thị lỗi hết thời gian chờ. Chúng tôi đã tập trung vào việc khắc phục sự cố liên lạc giữa Nginx và FPM nhưng sau vài giờ chúng tôi chưa tìm được giải pháp.
Lưu ý: sự cố này được lặp lại trên bốn máy ảo mà chúng tôi đã thiết lập.
Nginx lỗi log (lưu ý: hostname và ip đã bị đàn áp)
2014/09/27 01:57:26 [error] 12686#0: *8 upstream timed out (110: Connection timed out) while reading response header from upstream, client: <ip>, server: , request: "GET/HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock", host: "<hostname>"
2014/09/27 02:03:26 [error] 12718#0: *1 upstream timed out (110: Connection timed out) while reading response header from upstream, client: <ip>, server: , request: "GET/HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock", host: "<hostname>"
2014/09/27 02:05:29 [error] 12744#0: *1 upstream timed out (110: Connection timed out) while reading response header from upstream, client: <ip>, server: , request: "GET/HTTP/1.1", upstream: "fastcgi://unix:/var/run/php5-fpm.sock", host: "<hostname>"
File: /etc/nginx/nginx.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/*;
}
file : /etc/nginx/sites-available/default.conf
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;
}
File:/etc/nginx/fastcgi_params
Note: chỉ dòng fastcgi_param REQUEST_SCHEME $scheme;
đã được thêm vào.
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;
File: /etc/php5/fpm/php-fpm.conf
;;;;;;;;;;;;;;;;;;;;;
; 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
File: /etc/php5/fpm/pool.d/www.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
gì chúng tôi đã làm cho đến nay (các cấu hình có thể không được phản ánh trong các tệp ở trên)
- Đặt người dùng Nginx và FPM giống nhau (www-data).
- Khởi động lại các dịch vụ và máy móc.
- Đặt
proxy_read_timeout
trong cấu hình nginx. - Đặt
max_execution_time
vàrequest_terminate_timeout
thành 300 (trên php.ini và nginx.conf, tương ứng). - Tăng số lượng
worker_process
vàworker_connections
trên cấu hình nginx. - Thay đổi giá trị
Listen
từ /var/run/php5-fpm.sock thành 127.0.0.1:7777 trên cả cấu hình nginx và php-fpm.
Chúng tôi sẽ tiếp tục làm việc trên một giải pháp và nếu được tìm thấy sẽ được đăng ở đây. Cảm ơn!
Tôi có cùng một vấn đề nhưng tôi không sử dụng Xdebug. Tôi đang sử dụng máy ảo lang thang. Nó thường hoạt động tốt, nhưng đôi khi tôi nhận được điều này. –
Runnning tạm dừng lang thang và lang thang lên - cố định này nhưng tôi đoán chỉ tạm thời. –