500, 502, 503, and 504 server errors

We've gathered the common server errors 500, 502, 503, and 504: what each code means, which layer it comes from, and which log reveals the cause. Each error lists its common causes and how to fix them, and further down the page you'll find commands for a quick server check.

500 Internal Server Error

  • Fatal error in the code

    Application
    Common cause
    Code that fails after a plugin, theme, or library update, or an exception the code doesn't handle.
    Where to check
    The PHP or application error log, at the time the error appeared.

    According to RFC 9110, a 500 code means the server encountered an unexpected condition that prevented it from fulfilling the request. It's the most generic code, so the page in the browser almost never explains what happened. The common cause is a fatal error in the code: a function that doesn't exist, a syntax error, or an unhandled exception. The log shows the file name and line number. Fix the code, or roll back the most recently updated component to its previous version.

    Common WordPress errors

  • Allowed memory size of ... bytes exhausted

    memory_limit
    Common cause
    A script that used more memory than the limit set in memory_limit.
    Where to check
    The PHP error log, which shows the limit and the file that exceeded it.

    The memory_limit setting sets how much memory a single script may allocate, and its default is 128M. When a script exceeds it, PHP stops the script and the server returns a 500 error or a blank page. Raise the limit in the PHP settings, for example to 256M, and check what's using the memory: a large import, image processing, or a plugin that loads a lot of data at once. Raising the limit alone doesn't fix a memory leak in the code.

    php.ini directives

  • Maximum execution time of 30 seconds exceeded

    max_execution_time
    Common cause
    A script that ran longer than the time set in max_execution_time.
    Where to check
    The PHP error log, with the file and line where the script stopped.

    The default max_execution_time is 30 seconds for requests that come from the browser. On systems other than Windows, time spent on system calls and input and output operations usually doesn't count toward the limit, so a request waiting on a slow database sometimes ends with a 504 from the web server instead. Raise the limit only for scripts that need it, and move long tasks such as imports and backups to run in the background, in a job queue, or as cron jobs.

    php.ini directives

  • Error in the .htaccess file

    Apache
    Common cause
    A directive with a typo, or a directive from a module that isn't enabled on the server.
    Where to check
    The Apache error log, which shows the file and the problematic directive.

    Apache reads the .htaccess file on every request, and a line it doesn't recognize takes down the entire site with a 500 error. This usually happens right after adding a redirect rule or copying a block of settings from another site. The log shows a message such as Invalid command with the directive's name. Fix or remove the line, and wrap module directives in an IfModule block when you're not sure the module is enabled.

    .htaccess cheat sheet

  • File permissions and ownership

    Permissions
    Common cause
    A file or folder that the site's process can't read or write to.
    Where to check
    The web server error log, and ls -l on the path it shows.

    MDN lists incorrect file permissions among the common causes of a 500 error. After a site migration, a new release, or a backup extraction, files may be left owned by another user, and the cache, log, and upload folders may not be writable. Restore ownership to the site's user, and usually set 755 for folders and 644 for files. Permission 777 lets any user on the server change the files, so don't use it.

    Linux file permissions

  • Disk full

    Storage
    Common cause
    A full partition, so the application can't save sessions, cache files, or logs.
    Where to check
    The commands df -h and df -i.

    When the disk is full, write operations fail: sessions aren't saved, cache files aren't created, and the database may stop accepting changes. The result is sometimes a 500 error, and sometimes unexpected behavior, such as being logged out of the site again and again. The df -h command shows free space, and df -i shows free inodes, which run out when there are millions of small files. Clear old logs, local backups, and cache files, and set up log rotation with logrotate.

    Linux directories and logs

502 Bad Gateway

  • PHP-FPM or the application service isn't running

    Service down
    Common cause
    The service behind the web server stopped, crashed, or didn't start after an update or reboot.
    Where to check
    systemctl status on PHP-FPM or on the application's service.

    According to RFC 9110, a 502 code means a server acting as a gateway or proxy received an invalid response from the server behind it. On a server with Nginx and PHP-FPM, or with a reverse proxy to a Node.js or Python application, this is the most common problem: the web server is running, but the service that should respond to it is down. Check the service status, read its log with journalctl, fix the configuration error, and start the service again.

    systemctl commands

  • Wrong address or socket in fastcgi_pass or proxy_pass

    Proxy settings
    Common cause
    The web server connects to a different port or socket than the one the service listens on.
    Where to check
    ss -ltnp for ports and ss -lxp for sockets, against the pool's listen setting.

    After a PHP upgrade or a pool change, the socket path may change while the web server keeps connecting to the old path. The same problem occurs when an application starts on a different port than the one set in proxy_pass. Compare fastcgi_pass or proxy_pass with the address the service actually listens on. With a socket, also check that the web server user has access to it, according to the pool's listen.owner, listen.group, and listen.mode settings.

    Nginx cheat sheet

  • The process exited in the middle of the request

    Crash or out of memory
    Common cause
    A process that crashed, was stopped by request_terminate_timeout, or was killed when memory ran out.
    Where to check
    The PHP-FPM log, and the kernel log with journalctl -k.

    When the process handling the request exits before it finishes sending a response, the web server gets a partial or empty response and returns 502. The PHP-FPM setting request_terminate_timeout kills a process that runs longer than the time set in it, and it's off by default. The kernel's OOM killer also kills processes when the server runs out of memory, and the message Out of memory appears in the kernel log. Set the limit to fit the truly long tasks, and check which process is using the memory.

  • Invalid or oversized response headers

    Invalid response
    Common cause
    An application that returns malformed headers, or headers larger than the web server's buffer for them.
    Where to check
    The web server error log at the time of the failed request.

    In Apache, the ProxyBadHeader setting determines what happens when the server behind mod_proxy returns invalid headers, and the default, IsError, ends the request with a 502. In Nginx, response headers larger than the buffer set in proxy_buffer_size or fastcgi_buffer_size lead to a similar error, for example when the application sends many or large cookies. Reduce the headers in the application, or increase the buffer in the settings.

503 Service Unavailable

  • Maintenance mode

    Retry-After
    Common cause
    The site was deliberately put into maintenance mode during an update or a new release.
    Where to check
    The Retry-After header in the response, with curl -I.

    According to RFC 9110, a 503 code means the server can't handle the request right now, because of a temporary overload or scheduled maintenance, and it may include a Retry-After header with the suggested wait time. In Laravel, php artisan down returns 503, and the --retry option sets Retry-After. In WordPress, the .maintenance file shows the message Briefly unavailable for scheduled maintenance with a 503. When an update is interrupted, delete the file, and in Laravel, run php artisan up.

    Laravel Artisan commands

  • Server overload

    Resources
    Common cause
    Memory, CPU, or a connection pool that reached its limit, for example during a sale or a bot crawl.
    Where to check
    The uptime and free -m commands, and the web server access log.

    According to MDN, some applications return 503 when they reach a memory, CPU, or connection pool limit. In Apache with mod_proxy, when a wait time is set with the acquire parameter and no connection in the pool frees up before it expires, the server returns 503. Check whether the load comes from visitors or from bots, add caching for the most viewed pages, and when the load is constant, add server resources.

    Bots and crawlers

  • Rate limiting in Nginx

    limit_req
    Common cause
    A limit_req or limit_conn rule that rejects requests beyond the set rate or number of connections.
    Where to check
    Entries about rejected requests in the Nginx error log.

    The Nginx limit_req and limit_conn modules return 503 by default for rejected requests. When regular visitors get blocked, increase burst or the configured rate. You can also change the rejection code to 429 with limit_req_status, so it's clear that this is rate limiting and not a failure. MDN recommends 429 Too Many Requests for requests rejected because of rate limiting.

    Nginx cheat sheet

504 Gateway Timeout

  • The application didn't respond in time

    proxy_read_timeout
    Common cause
    A request that outlasts the web server's timeout, such as an import, a heavy report, or a slow API call.
    Where to check
    The Nginx error log and the PHP-FPM slowlog.

    According to RFC 9110, a 504 code means a server acting as a gateway or proxy didn't receive a timely response from the server behind it. In Nginx, the proxy_read_timeout and fastcgi_read_timeout timeouts are 60 seconds by default, and they're measured between two read operations, not over the entire response. Raise the timeout only in the location that needs it, and move long tasks to run in the background, so visitors get a fast response.

    Nginx cheat sheet

  • ProxyTimeout in Apache

    Apache
    Common cause
    The server behind mod_proxy doesn't send data within the time set in ProxyTimeout.
    Where to check
    The Apache error log, and the Timeout and ProxyTimeout settings.

    In Apache, the timeout for the server behind mod_proxy is set with ProxyTimeout, and when it isn't set, the value of Timeout applies, which defaults to 60 seconds. You can set a separate timeout for each backend with the timeout parameter of ProxyPass. As with Nginx, raise the timeout only for the paths that need it, and first check why the request is slow.

  • PHP-FPM pool is full

    pm.max_children
    Common cause
    All the pool's processes are busy, and new requests wait in the queue.
    Where to check
    A pm.max_children warning in the PHP-FPM log, and the pool's slowlog.

    The pm.max_children setting limits how many requests PHP-FPM handles at the same time. When all processes are busy, new requests wait in the queue, and when the wait is longer than the web server's timeout, the result is a 504, and sometimes a 502. Raise the value according to the server's free memory and the memory an average PHP process uses, and use the slowlog to find the requests that hold processes for a long time.

  • Slow query or overloaded database

    Database
    Common cause
    Queries without an index, locked tables, or a database that doesn't respond.
    Where to check
    Running queries with SHOW FULL PROCESSLIST, and the mysqladmin ping command.

    When the database is slow, PHP waits for a response, and the wait time usually doesn't count toward max_execution_time. So the web server hits its own timeout first and returns 504. When the database is completely down, the application usually returns 500 or a connection message, such as Error establishing a database connection in WordPress. Check which queries are running, add indexes for the slow queries, and make sure the database service is running.

    MySQL commands

CDN, proxy, and the visitor's network

  • An error page from Cloudflare or another CDN

    Codes 520 to 526
    Common cause
    A problem with the connection between the CDN and the origin server, not with the CDN itself.
    Where to check
    A direct request to the origin server with curl and the --resolve option.

    When the site goes through a CDN or proxy service, first identify who returned the error. Codes such as 520, 521, 522, and 524 on a Cloudflare-branded page point to a problem with the connection to the origin server, for example a server that's down, a firewall blocking the service, or a request that takes too long. A 502 or 504 error on the web server's own page comes from the server. A direct request to the server's IP address, bypassing the CDN, shows whether the server itself is working.

    curl commands

  • The error appears for only one visitor

    Visitor's network
    Common cause
    A VPN, proxy, firewall, or DNS settings on the visitor's network.
    Where to check
    Opening the site from another network, for example from a phone without Wi-Fi.

    According to MDN, when the site works for other visitors, 502 and 504 errors may come from the visitor's network, especially when they use a VPN or special network settings. In that case, check the network, firewall, proxy, and DNS settings on the device. When the error also appears on another network, the problem is on the server side, so check the logs according to the cards on this page.

Commands and logs for a quick check

Checking from outside

  • curl -I https://example.com

    Shows the status code and the response headers, for example Retry-After on a 503 error. Some servers return a different response to a HEAD request than to regular browsing.

  • curl -s -o /dev/null -w "%{http_code} %{time_total}\n" https://example.com

    Shows only the status code and the response time of a GET request. A 504 error that arrives after exactly 60 seconds usually points to the web server's timeout.

  • curl -sI --resolve example.com:443:192.0.2.10 https://example.com

    Sends the request directly to the server's IP address, bypassing the CDN. This lets you tell an origin server error apart from a CDN error.

Error logs

  • tail -f /var/log/nginx/error.log

    Shows the Nginx error log in real time. Load the failing page, and the new line shows the cause, for example a refused connection or an expired timeout.

  • tail -n 100 /var/log/httpd/error_log

    Shows the last 100 lines of the Apache error log on Red Hat family distributions. On Debian and Ubuntu, the log is at /var/log/apache2/error.log.

  • tail -f /var/log/php-fpm/error.log

    The main PHP-FPM log on Red Hat family distributions, with warnings such as reaching pm.max_children and processes that exited. On other distributions, the path is set by the PHP-FPM error_log setting.

  • journalctl -u php-fpm --since "1 hour ago"

    Shows the service's entries from the last hour. On Debian and Ubuntu, the service name includes the PHP version, and you can find the exact name with the next command.

  • systemctl list-units --type=service | grep -i fpm

    Finds the exact name of the PHP-FPM service on the server, for use with systemctl and journalctl.

  • journalctl -k | grep -i "out of memory"

    Searches the kernel log for processes killed when the server ran out of memory. A PHP or database process killed this way explains sudden 502 errors.

  • awk '$9 ~ /^5/' /var/log/nginx/access.log | tail -n 20

    Shows the last 20 requests that ended with a 5xx error, from an access log in the common combined format. This shows which URLs fail and when.

  • tail -f storage/logs/laravel.log

    The error log of a Laravel application, from the project's root folder. With a daily log, the file name includes the date.

  • tail -f wp-content/debug.log

    The WordPress error log, written when WP_DEBUG and WP_DEBUG_LOG are enabled in wp-config.php.

Services and resources

  • systemctl status php-fpm

    Shows whether PHP-FPM is running, when it started, and the last lines of its log. A failed state explains a 502 error. The service name varies between distributions: on Debian and Ubuntu, it includes the PHP version.

  • systemctl status nginx

    Shows the web server status. For Apache, the service name is httpd on Red Hat family distributions and apache2 on Debian and Ubuntu.

  • sudo systemctl restart php-fpm

    Restarts PHP-FPM, using the service name on your server. Caution: requests being handled at that moment are cut off. Read the log before restarting, so you don't lose the cause of the problem.

  • sudo nginx -t && sudo systemctl reload nginx

    Checks the Nginx configuration syntax, and reloads it only when the check passes, without dropping active connections.

  • sudo apachectl configtest

    Checks the Apache configuration syntax before a reload, and shows Syntax OK when the configuration is valid.

  • sudo ss -ltnp

    Shows the ports listening for TCP connections and the process behind each port. Make sure the application listens on the port set in proxy_pass.

  • sudo ss -lxp | grep -i php

    Shows the PHP-FPM Unix sockets. Compare the path with the one set in fastcgi_pass.

  • uptime

    Shows the load average over the last 1, 5, and 15 minutes. A value higher than the number of CPUs over time points to an overloaded server.

  • free -m

    Shows free memory and swap usage, in megabytes. The available column shows how much memory is actually available for new processes.

  • ps aux --sort=-%mem | head -n 15

    Shows the processes using the most memory. It helps you calculate how much memory an average PHP process uses before you change pm.max_children.

  • df -h && df -i

    Shows the free space on each partition and the number of free inodes. A full partition causes write errors and a 500 error.

  • mysqladmin ping

    Checks whether the database server is available, and returns mysqld is alive when it's running. When authentication is required, add a username with -u and a password prompt with -p.

Common settings

  • pm.max_children = 20

    In the PHP-FPM pool file: the maximum number of processes that handle requests at the same time. Raise it according to the free memory, and restart the service.

  • request_slowlog_timeout = 5s slowlog = /var/log/php-fpm/www-slow.log

    Writes the call stack of every request that takes longer than five seconds to a separate log. This shows you the function where the request gets stuck.

  • request_terminate_timeout = 120s

    Kills a process that handles a single request for longer than the set time. Meant for cases where max_execution_time doesn't stop the script. Off by default (0).

  • fastcgi_read_timeout 120s;

    In Nginx: the timeout for a response from PHP-FPM, between two read operations. The default is 60 seconds. Set it in the location block of the path that needs it.

  • proxy_read_timeout 120s;

    In Nginx: the timeout for a response from an application behind proxy_pass, between two read operations. The default is 60 seconds.

  • ProxyTimeout 120

    In Apache: the timeout in seconds for the server behind mod_proxy. When the setting is missing, the value of Timeout applies.

    Example: ProxyPass "/app" "http://127.0.0.1:3000/" timeout=120

  • memory_limit = 256M

    In php.ini or .user.ini: the maximum memory for a single script. The default is 128M.

  • max_execution_time = 60

    In php.ini or .user.ini: the maximum run time in seconds for a request from the browser. The default is 30 seconds.

  • define( 'WP_DEBUG', true ); define( 'WP_DEBUG_LOG', true ); define( 'WP_DEBUG_DISPLAY', false );

    In wp-config.php, above the line That's all, stop editing: writes errors to wp-content/debug.log without showing them to visitors. When you finish checking, set WP_DEBUG back to false.

  • wp plugin deactivate --all

    Deactivates all WordPress plugins with WP-CLI, to check whether a plugin is causing the 500 error. Caution: after the check, reactivate the plugins one at a time.

  • APP_DEBUG=false

    In the Laravel .env file on a live site: the details of every error are saved in storage/logs, and visitors see a generic error page. The value true on a live site exposes sensitive configuration data to visitors.

  • php artisan down --retry=60

    Puts a Laravel application into maintenance mode, which returns 503 with the Retry-After header. The php artisan up command brings the site back online.

    Example: php artisan down --secret="my-bypass-token"

Which layer the error comes from

A request to a website usually passes through several layers: the browser, a CDN if there is one, the web server, the service that runs the code, such as PHP-FPM, and finally the database. A 500 code usually comes from the application itself, so the cause appears in the PHP or application log. Codes 502 and 504 come from the web server or the CDN, and describe a problem in the layer behind them: 502 when an invalid response was received or the connection failed, and 504 when the response didn't arrive in time. A 503 code describes a temporary condition, such as maintenance or overload.

What visitors see

On a live site, visitors should see a generic error page, without paths, queries, or configuration details. So turn off error display on the page, in PHP with display_errors, in Laravel with APP_DEBUG=false, and in WordPress with WP_DEBUG_DISPLAY, and keep all the details in the log. Long-lasting 5xx errors also slow down Google's crawling, and according to Google, pages that keep returning a server error are eventually dropped from the index.

Did you know?

With our web hosting plans, your sites run on fast, secure servers, with personal support via HelpDesk. For a high-traffic site or an application that needs its own settings, a Linux VPS gives you guaranteed resources and full root access, so you can tune PHP-FPM and the web server timeouts yourself.

Web hosting plans

Server errors FAQ

What's the difference between a 502 error and a 504 error?

In both errors, the web server or the proxy service is running, and the problem is in the service behind it. With a 502 error, an invalid response was received or the connection to the service failed, for example when PHP-FPM is down. With a 504 error, the service didn't respond in time, usually because of a slow request, a full process pool, or an overloaded database. In both cases, the web server error log shows exactly what happened.

How do I find the cause of a 500 error on my site?

The page visitors see almost never explains the cause, and the details are in the error log. Open the PHP or application error log, for example wp-content/debug.log in WordPress or the storage/logs folder in Laravel, and look for the line from the time the error appeared. The log shows the file name and line number, so you can fix the code or roll back the most recently updated component to its previous version. When the log is empty, check the web server error log and the .htaccess file.

Do server errors hurt my site's rankings on Google?

A short outage that's fixed quickly usually leaves no lasting effect. According to Google, when a site returns 500, 502, or 503 errors, its crawling slows down, and once the site returns normal responses again, the crawl rate gradually goes back up. Pages that keep returning a server error are eventually dropped from the index, so it's important to spot the errors quickly and fix the cause.

How do I put a site into maintenance mode the right way?

In maintenance mode, the site returns a 503 code, which tells browsers and search engines that the condition is temporary, and it's best to add the Retry-After header with the estimated wait time. In Laravel, run php artisan down, and the --secret option lets you keep browsing the site while you work. In WordPress, maintenance mode turns on automatically during an update. When you finish, bring the site back online and use curl -I to check that it returns a 200 code.

How do I know my site needs a server with more resources?

The common signs are 502, 503, and 504 errors that recur at peak hours, pm.max_children warnings in the PHP-FPM log, a high load average in the uptime command, and memory running out. When the code and caching are already optimized and the errors continue, it's time for more resources. A Linux VPS gives you guaranteed resources and full root access, so you can enlarge the process pool and tune the timeouts to fit your site.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement