You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu16.04下Nginx+php7.1-fpm出现504超时问题求助

Hey there, let's break down this 504 Gateway Time-out issue you're hitting with Nginx and PHP 7.1-FPM on Ubuntu 16.04. That log message upstream timed out (110: Connection timed out) while reading response header from upstream tells us Nginx is waiting too long for PHP-FPM to send back a response header—so we need to dig into why PHP-FPM isn't keeping up.

Common Causes & Fixes

1. First, Verify PHP-FPM is Running Properly

Sometimes the simplest fixes work first. Check if PHP-FPM is active:

sudo systemctl status php7.1-fpm

If it's stopped or in a failed state, restart it:

sudo systemctl restart php7.1-fpm

2. Adjust PHP-FPM Process Pool Settings

If PHP-FPM doesn't have enough child processes to handle incoming requests, it'll start dropping or delaying connections. Edit your PHP-FPM pool config (usually at /etc/php/7.1/fpm/pool.d/www.conf):

  • Look for pm.max_children: This sets the maximum number of PHP processes. A good rule of thumb is ~20 processes per GB of RAM (adjust based on your server's memory).
  • pm.start_servers: Number of processes to start on boot—aim for 1/4 of max_children.
  • pm.min_spare_servers / pm.max_spare_servers: Keep these within a reasonable range to ensure there are always idle processes ready to handle new requests.

After making changes, reload PHP-FPM to apply:

sudo systemctl reload php7.1-fpm

3. Tweak Timeout Settings (Nginx + PHP-FPM)

The timeout is likely hitting because either Nginx gives up waiting, or PHP-FPM kills the script too early.

Nginx Side

Add or update these directives in your virtual host config to extend how long Nginx waits for PHP-FPM:

fastcgi_connect_timeout 60s;
fastcgi_send_timeout 60s;
fastcgi_read_timeout 60s;

If your app has long-running scripts (like data exports), you can bump these to 120s or higher. Save the config and reload Nginx:

sudo systemctl reload nginx
PHP-FPM Side

Edit /etc/php/7.1/fpm/php.ini to increase script execution limits:

  • max_execution_time = 60 (default is 30—adjust to match your Nginx timeout)
  • max_input_time = 60 (gives more time for form data to be processed)

Also in www.conf, set request_terminate_timeout = 70s (a bit longer than max_execution_time to avoid premature kills). Restart PHP-FPM after changes:

sudo systemctl restart php7.1-fpm

4. Check PHP-FPM Connection Permissions (Unix Socket)

If you're using a Unix socket instead of TCP for PHP-FPM, Nginx might not have permission to access it. Check the socket's permissions:

ls -l /run/php/php7.1-fpm.sock

It should be owned by www-data:www-data with permissions 660. If not, edit www.conf:

listen.mode = 0660
listen.group = www-data

Reload PHP-FPM to apply.

5. Hunt for Slow Scripts with PHP-FPM Slow Log

To find exactly which scripts are causing timeouts, enable PHP-FPM's slow log. In www.conf:

slowlog = /var/log/php7.1-fpm/slow.log
request_slowlog_timeout = 10s

Create the log file and set correct permissions:

sudo touch /var/log/php7.1-fpm/slow.log
sudo chown www-data:www-data /var/log/php7.1-fpm/slow.log

Restart PHP-FPM, then check the slow log after the timeout happens. It'll show you the exact script and line numbers that are taking too long—this is key for optimizing your code (like fixing slow database queries or inefficient loops).

6. Check System Resources

If your server is low on CPU or RAM, PHP processes will slow down or get stuck. Use htop to monitor resource usage:

  • If CPU is spiking: Look for runaway PHP processes or resource-heavy scripts.
  • If memory is maxed out: Reduce pm.max_children to prevent PHP-FPM from eating all RAM, or consider upgrading your server.

内容的提问来源于stack exchange,提问作者O. Neel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:44:57