nginx 和 php-fpm 等待服务器时间过长

nginx 和 php-fpm 等待服务器时间过长

我正在使用 docker 挂载 nginx 和 PHP-FPM,使用 Phalcon 框架和 React 作为前端

`upstream fastcgi_backend {
server bfy-%APP_VERSION_UID%_buildify-php-fpm_1:90%FPM_PORT_PREFIX%;
}

server {
# Port 80 will require Nginx to be started with root permissions
# Depending on how you install Nginx to use port 80 you will need
# to start the server with `sudo` ports about 1000 do not require
# root privileges
# listen      80;

listen        80;
server_name $hostname;

##########################
# In production require SSL
# listen 443 ssl default_server;

# ssl on;
# ssl_session_timeout  5m;
# ssl_protocols  SSLv2 SSLv3 TLSv1;
# ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
# ssl_prefer_server_ciphers   on;

# These locations depend on where you store your certs
# ssl_certificate        /var/nginx/certs/default.cert;
# ssl_certificate_key    /var/nginx/certs/default.key;
##########################

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

# This is the folder that index.php is in
root /var/www/html/public;
index index.php index.html index.htm;

charset utf-8;
client_max_body_size 100M;
fastcgi_read_timeout 1800;

open_file_cache max=100;
output_buffers 5 256k;

set_real_ip_from 172.18.0.9;
real_ip_header X-Real-IP;

# Represents the root of the domain
# http://localhost:8000/[index.php]
location / {
    # Matches URLS `$_GET['_url']`
    try_files $uri $uri/ /index.php?_url=$uri&$args;
}

# When the HTTP request does not match the above
# and the file ends in .php
location ~ [^/]\.php(/|$) {
    # try_files $uri =404;

    # Ubuntu and PHP7.0-fpm in socket mode
    # This path is dependent on the version of PHP install
    fastcgi_pass  fastcgi_backend;


    # Alternatively you use PHP-FPM in TCP mode (Required on Windows)
    # You will need to configure FPM to listen on a standard port
    # https://www.nginx.com/resources/wiki/start/topics/examples/phpfastcgionwindows/
    # fastcgi_pass  127.0.0.1:9000;

    fastcgi_index /index.php;

    include fastcgi_params;
    fastcgi_split_path_info ^(.+?\.php)(/.*)$;
    if (!-f $document_root$fastcgi_script_name) {
        return 404;
    }

    fastcgi_param PATH_INFO       $fastcgi_path_info;
    # fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    # and set php.ini cgi.fix_pathinfo=0

    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_buffers 64 4k;
    fastcgi_busy_buffers_size 24k;
    fastcgi_buffer_size 16k;
    fastcgi_read_timeout 600s;
    fastcgi_connect_timeout 600s;
}

location ~ /\.ht {
    deny all;
}

location ~* /collections.json$ {
        try_files $uri $uri/ /index.php?_url=$uri&$args;
}

location /products/ {
    location ~* .js$ {
        try_files $uri $uri/ /index.php?_url=$uri&$args;
    }
}

location /collections/ {
    location ~* /products.json$ {
        try_files $uri $uri/ /index.php?_url=$uri&$args;
    }
}

location ~* \.(js|json|css|png|jpg|jpeg|gif|ico)$ {
    expires       max;
    log_not_found off;
    access_log    off;
}

gzip on;
gzip_disable "msie6";

gzip_comp_level 6;
gzip_min_length 1100;
gzip_buffers 16 8k;
gzip_proxied any;
gzip_types
    text/plain
    text/css
    text/js
    text/xml
    text/javascript
    application/javascript
    application/x-javascript
    application/json
    application/xml
    application/xml+rss
    image/svg+xml;
gzip_vary on;
}

这是 php-fpm 配置文件

[global]
; Pid file
; Note: the default prefix is /var
; Default Value: none
pid = /run/php-fpm.pid

; Error log file
; If it's set to "syslog", log is sent to syslogd instead of being written into a 
local file.
; Note: the default prefix is /var
error_log = /proc/self/fd/2


; syslog_ident is prepended to every message. If you have multiple FPM
; instances running on the same server, you can change the default value
; which must suit common needs.
; Default Value: php-fpm
;syslog.ident = php-fpm

; 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

process_control_timeout = 0

daemonize = no

; Set open file descriptor rlimit for the master process.
; Default Value: system defined value
rlimit_files = 1024

; Set max core size rlimit for the master process.
; Possible Values: 'unlimited' or an integer greater or equal to 0
; Default Value: system defined value
rlimit_core = 0

; Specify the event mechanism FPM will use. The following is available:
; - select     (any POSIX os)
; - poll       (any POSIX os)
; - epoll      (linux >= 2.5.44)
; - kqueue     (FreeBSD >= 4.1, OpenBSD >= 2.9, NetBSD >= 2.0)
; - /dev/poll  (Solaris >= 7)
; - port       (Solaris >= 10)
; Default Value: not set (auto detection)
events.mechanism = epoll


[www]
; if we send this to /proc/self/fd/1, it never appears
access.log = /proc/self/fd/1

user = %FPM_USER%
group = %FPM_GROUP%

;listen.owner = root
;listen.group = root
;listen.mode = 0660
;listen.allowed_clients = 127.0.0.1
listen = 0.0.0.0:%FPM_PORT%

pm = dynamic
pm.max_children = 200
pm.start_servers = 15
pm.min_spare_servers = 15
pm.max_spare_servers = 25
pm.max_requests = 500
pm.process_idle_timeout = 10s

clear_env = no

; Ensure worker stdout and stderr are sent to the main error log.
catch_workers_output = yes

; for debugging 1h
request_terminate_timeout = 3600

我使用 Shopify API 将其用作无头电子商务。API 的响应很快,但页面需要大约 12 秒才能加载,当我检查请求时,我发现几乎所有时间都花在等待服务器上, 请看截图即使我删除 API 调用,页面加载时间也相同

我想改善服务器响应时间,以便页面加载时间不至于达到 10-12 秒。我尝试减少和增加 pm.max_childern 和其他 pm 属性,但没有什么效果。我正在使用FROM php:7.2-fpm AS phalcon_build

是不是配置有问题导致页面加载缓慢?

答案1

您应该安装 xhprof,这样您就会看到时间花在了哪里。

使用 PECL (https://pecl.php.net/package/xhprof

pecl install xhprof

并使用此项目中的xhprof_htmlxhprof_lib来显示结果:https://github.com/longxinH/xhprof

答案2

是的,您的配置有问题,但主要问题出现在 PHP 执行中(或任何持久层,例如 PHP 下面的 DB)。

检查会话争用、不必要的 DNS 查找,并添加仪器来记录执行线程离开 PHP(即 http 调用、数据库调用、外部脚本)时的经过时间。

相关内容