Nginx errors and troubleshooting

Troubleshooting server errors and solutions

When working with NGINX web servers, errors can sometimes occur that interfere with the proper functioning of your website. In this article, we’ll go over the most common errors, such as 502 Bad Gateway, 504 Gateway Time-out, and 413 Request Entity Too Large, and show you how to fix them.


502 Bad Gateway

This error occurs when NGINX is unable to receive a response from one of the services on the server. It often happens when NGINX is used in combination with Apache, Memcached, or is handling PHP-FPM requests. Usually, the problem is caused by a service being down — in this case, check the status of the service and restart it if necessary.

For PHP-FPM, it’s also important to verify socket permissions. In /etc/php-fpm.d/www.conf, the permissions should be set correctly:

listen = /tmp/php5-fpm.sock 
listen.group = www-data
listen.owner = www-data

504 Gateway Time-out

This error occurs when NGINX waits too long for a response from a connected service. It usually happens if the service responds too slowly.

To fix this, you can increase timeout settings. For NGINX + Apache, add the following to your server configuration:

server { 
... 
send_timeout 800;
proxy_send_timeout 800;
proxy_connect_timeout 800;  
proxy_read_timeout 800;  
... 
}

If PHP is taking too long to process in PHP-FPM, also increase the FastCGI timeout:

location ~ \.php$ { 
include fastcgi_params;
fastcgi_index index.php; 
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
fastcgi_pass unix:/tmp/php5-fpm.sock;  
fastcgi_read_timeout 800;
}

413 Request Entity Too Large

This error occurs when you try to upload a file that is too large. By default, NGINX limits uploads to 1Mb.

To increase the limit, open nginx.conf and find:

client_max_body_size 1m;

Then change it to your desired value, for example:

client_max_body_size 30m;

You can also completely disable request body size checking by setting it to zero:

client_max_body_size 0;

Note

Using a VPS or a dedicated server gives you full control over services. This makes it easier to check their status, modify configurations, and quickly fix issues as they arise.

After making changes, always reload NGINX.


How to Reload NGINX

To reload NGINX, use restart or reload:

service nginx reload

or

/etc/init.d/nginx reload

or

nginx -s reload

To reload only the configuration without stopping the server:

nginx -s reload

You can check the configuration for errors with:

nginx -t

Reload vs Restart

Reload — applies the new configuration gracefully: new processes start with the updated configuration while old processes shut down gradually. Restart — abruptly stops and starts the server. If there are configuration errors, a restart may cause the server to stop, whereas reload will revert to the previous working configuration.

Need help?Our engineers will help you free of charge with any question in minutesContact us