Fix 504 Gateway Timeout on Nginx (PHP-FPM Ubuntu/Virtualmin Guide)

Fix 504 Gateway Timeout errors on Nginx caused by slow PHP-FPM scripts or timeouts. Learn how to diagnose and resolve it on Ubuntu or Virtualmin.

A 504 Gateway Timeout error on Nginx usually means your server took too long to respond. This often happens when PHP-FPM is slow, overloaded, or stuck running heavy scripts. The good news is that you can fix this error with a few configuration changes. In this guide, you’ll learn how to diagnose the cause and apply the right solution on Ubuntu or Virtualmin.

A 504 Gateway Timeout error on Nginx is one of the most frustrating issues you can encounter on an unmanaged VPS. It means Nginx passed the request to your backend (usually PHP-FPM) but didn’t get a response fast enough. This is common on WordPress websites using heavy plugins, slow database queries, or misconfigured timeouts.

Fortunately, the solution is usually straightforward. This guide walks you through the most reliable fixes for Ubuntu and Virtualmin servers running Nginx + PHP-FPM.

What Causes a 504 Gateway Timeout?

This error occurs when PHP-FPM takes too long to process a request. Common causes include:

Let’s diagnose and fix the issue.

🛠️ Step 1: Check PHP-FPM Status

Start by verifying whether PHP-FPM is running normally:

				
					systemctl status php8.1-fpm
				
			

If you’re on PHP 8.2:

				
					systemctl status php8.2-fpm
				
			

If you see errors like “child exited with signal” or “failed”, restart the service:

				
					systemctl restart php8.1-fpm
				
			

🛠️ Step 2: Increase Nginx Timeout Values

Nginx might be giving up too early. Increase key timeout settings in your site config:

Open:

				
					nano /etc/nginx/sites-available/yourdomain.com.conf
				
			

Add or update:

				
					proxy_connect_timeout       300;
proxy_send_timeout          300;
proxy_read_timeout          300;
send_timeout                300;
fastcgi_read_timeout        300;
				
			

These raise the limit to 5 minutes, ideal for imports or slow operations.

Test config:

				
					nginx -t
				
			

Reload:

				
					systemctl reload nginx
				
			

🛠️ Step 3: Increase PHP-FPM Timeout

Nginx may wait longer, but PHP-FPM might still terminate requests early.

Edit:

				
					nano /etc/php/8.1/fpm/pool.d/www.conf
				
			

Find:

				
					request_terminate_timeout
				
			

Set it to:

				
					request_terminate_timeout = 300
				
			

Also increase max execution time:

				
					/etc/php/8.1/fpm/php.ini
				
			

Modify:

				
					max_execution_time = 300
				
			

Restart PHP-FPM:

				
					systemctl restart php8.1-fpm
				
			

🛠️ Step 4: Check for Slow WordPress Plugins

Certain plugins frequently trigger 504s:

  • WooCommerce analytics

  • WP All Import / Export

  • Backup plugins

  • Security scanners

  • Plugins with remote API calls

  • Broken caching plugins

Check your logs if a specific plugin is causing long execution times.

Disable suspected plugins in WordPress or temporarily via SFTP by renaming the folder:

				
					wp-content/plugins/plugin-name-disabled
				
			

🛠️ Step 5: Check Server Resource Usage

Run:

				
					top
				
			

Or:

				
					htop
				
			

Look for:

  • PHP-FPM using 100% CPU

  • MySQL consuming all resources

  • High memory usage

  • Swap usage pushed to the limit

If memory is low, increase swap:

				
					fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
				
			

Add to /etc/fstab:

				
					/swapfile none swap sw 0 0
				
			

🛠️ Step 6: Check MySQL Bottlenecks

MySQL slowdowns can cause PHP-FPM to stall.

Check slow query log:

				
					tail -n 50 /var/log/mysql/mysql-slow.log
				
			

If WordPress queries are extremely slow:

  • optimize database

  • remove heavy plugins

  • repair tables

  • upgrade server resources

🛠️ Step 7: Virtualmin-Specific Fixes

In Virtualmin:

Web Configuration → PHP Options

Set:

  • PHP execution mode = FPM

  • PHP script timeout = 300 seconds

Virtualmin PHP Options

Also check:

Web Configuration → PHP-FPM Configuration

Ensure your domain uses the correct PHP version’s socket.

🧪 Final Test

Restart everything:

				
					systemctl restart nginx
systemctl restart php8.1-fpm
systemctl restart mysql
				
			

Reload your site.

🎉 Conclusion

A 504 Gateway Timeout usually happens because PHP-FPM or MySQL took too long to respond. By increasing your timeout limits, checking slow plugins, reviewing server performance, and tuning PHP-FPM, you can eliminate this error for good.

If the issue still persists, it’s usually caused by:

  • A faulty plugin

  • Long-running WooCommerce/WP All Import tasks

  • Insufficient VPS resources

Feel free to share your environment details in the forums and I can help you troubleshoot deeper.

Tharindu

Hey!! I'm Tharindu. I'm from Sri Lanka. I'm a part time freelancer and this is my blog where I write about everything I think might be useful to readers. If you read a tutorial here and want to hire me, contact me here.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button