大网数据广告活动
服务器运维问答

How to prevent whole server to suffer from one website's timeout?

· 52CE 我爱测

Since I started hosting a website on the server, I get 505's, 504's, timeouts on it, as well as for the other websites hosted on that same server (but these other websites have no issues). The server's OS is Ubuntu16.04 and it ha…

问题原文

原帖链接 · ccl18 · CC BY-SA 3.0

Since I started hosting a website on the server, I get 505's, 504's, timeouts on it, as well as for the other websites hosted on that same server (but these other websites have no issues).

The server's OS is Ubuntu16.04 and it has php7.0-fpm nginx, apparently misconfigured.

How to avoid all other websites to suffer from the newly hosted website's timeouts? Or how to avoid these timeouts at all (without fixing the php errors as I have nothing to do with that)?

The php code has many errors. php7.0-fpm logs reveal a timeout.

/var/log/php7.0-fpm.log

[26-Mar-2018 20:29:54] WARNING: [pool www] child 17012, script '/var/www/xxxxxx.com/index.php' (request: "POST /index.php?function=my-profile") execution timed out (306.572078 sec), terminating [26-Mar-2018 20:29:54] WARNING: [pool www] child 17012 exited on signal 15 (SIGTERM) after 1100.011016 seconds from start [26-Mar-2018 20:29:54] NOTICE: [pool www] child 19559 started

/var/log/nginx/error.log

Nginx reveals so many php warnings that I'm not quite sure where to look at.

/etc/nginx/nginx.conf

http {
    # ...
    fastcgi_buffers 8 16k;
    fastcgi_buffer_size 32k;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    # ...
}

/etc/php/7.0/fpm/pool.d

pm = dynamic
pm.start_servers = 4
pm.min_spare_servers = 2
pm.max_spare_servers = 6
pm.process_idle_timeout = 10s
pm.max_requests = 500

/etc/php/7.0/fpm

default_socket_timeout = 60
max_execution_time = 300
max_input_time = 60
memory_limit = 128M

The server block includes this part /etc/nginx/global/wordpress.conf

# Deny access to any files with a .php extension in the uploads directory
# Works in sub-directory installs and also in multisite network
# Keep logging the requests to parse later (or to pass to firewall utilities such as fail2ban)
location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
}

# Directives to send expires headers and turn off 404 error logging for Static assets
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpe?g|gif|png|ico|zip|pdf|t?gz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|swf|bmp|txt|rtf|md)$ {
    access_log off;
    log_not_found off;
    expires max;

    # CORS headers; this is wide-open, you want to tight it up a bit
    add_header Cache-Control public;
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Methods GET,OPTIONS;
    add_header Access-Control-Allow-Headers *;
}

# Attempted to match last if rules below fail.
location / {
    try_files $uri $uri/ /index.php?$args;
}

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# Pass PHP scripts to PHP-FPM daemon
# Check: http://wiki.nginx.org/Pitfalls
location ~* \.php$ {
    # filter out problem conditions
    try_files $uri $uri/ =404;

    # bring in parameters
    include fastcgi.conf;

    # send requests to upstream
    # fastcgi_pass phpfpm;
    # fastcgi_pass 127.0.0.1:9000;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_read_timeout 300;

}

已采纳解答

原帖链接 · Bert · CC BY-SA 3.0

Despite your will, you have created the same situation when someone is under a PHP-POOL attack. This means exactly what is happening to you, the PHP get's stuck for one website, and the rest cannot operate because of that. What I strongly recommend is the following:

Create a new PHP pool for each of the websites. You can achieve this by creating new config files. On CentOS 7, I have them in /etc/php-fpm.d/mywebsite.conf. Find your current pool config file and simply create a copy for it to the same folder. Then you have to change the followings:

  • [mywebsite]
  • user = nginx
  • group = nginx
  • listen = /run/php/mywebsite.sock
  • listen.owner = nginx
  • listen.group = nginx
  • listen.mode = 0660

Some just needs to be uncommented, some needs to be edited. After you have done this, do the same for each of the websites. Then in the websites nginx config, replace the fastcgi_pass unix:/run/php/php7.0-fpm.sock; with the appropriate file locations of the sock files. Restart NginX and PHP-FPM and you should be ready to go.

Also... 128MB for a PHP? That is thin like one hair piece. Increase it to 1024M at the least, or 2048 if you can. Depends on your HW OFC.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-03-27)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。