Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
World desk9 min

How to Configure 404 Error Pages for WordPress on a LEMP Stack (Nginx + PHP-FPM)

Set up reliable WordPress 404 handling on a LEMP stack: route permalinks with try_files, choose Nginx or theme ownership, preserve HTTP 404, and troubleshoot common failures.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

WordPress 404s on a LEMP server are controlled by two different layers. Nginx can serve a lightweight static error document, or it can pass the request to WordPress so the active theme renders 404.php (classic themes) or templates/404.html (block themes). Put try_files $uri $uri/ /index.php?$args; in location / when WordPress should resolve permalinks, then choose one 404 owner deliberately. Validate with sudo nginx -t, reload only after it passes, and verify that a nonexistent URL returns HTTP 404 rather than a redirect or a misleading 200 response.

Why a LEMP WordPress site can show two different 404 pages

A request can fail at Nginx or inside WordPress. Nginx operates first: it checks the document root, location rules, and PHP handler. If no matching file or directory exists, its try_files fallback can send the request to WordPress’s index.php. WordPress then examines the path and decides whether it identifies a post, page, archive, or genuinely missing resource.

As an Amazon Associate I earn from qualifying purchases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Those paths produce different results:

  • Nginx-owned 404: Nginx returns an error document such as /404.html. This works without PHP or WordPress and is appropriate for a minimal server-level response.
  • WordPress-owned 404: WordPress loads the active theme’s 404 template, allowing the normal header, footer, navigation, styles, search form, and other theme-aware elements.

The configuration must make that ownership unambiguous. An error_page 404 /404.html; directive can intercept a 404 generated by Nginx, and with FastCGI interception enabled it can also replace a 404 returned by PHP. Defining it when you expect a theme-generated page is a common reason the plain Nginx page appears.

Prerequisites and a safe change procedure

You need shell access with permission to run privileged Nginx commands, the path to the WordPress document root, and the PHP-FPM socket or upstream name used by the site. Determine the active server block before editing; changing a disabled or unrelated file has no effect.

  1. Back up the Nginx server-block file and the active theme’s 404 template. Keep a copy of the last known-good configuration so you can restore it if Nginx refuses to reload.
  2. Confirm the document root contains index.php and the WordPress files. The root directive must point to that directory, not to its parent or to a directory containing only a deployment archive.
  3. Identify the PHP-FPM endpoint. Typical distributions use a Unix socket under /run/php/, but the filename varies by installed PHP version and operating system. Use the endpoint already configured for the site rather than copying a socket path from another server.
  4. Edit the server block, test its syntax, and reload only after the test succeeds.

The baseline Nginx routing configuration

For a normal WordPress installation, the central rule belongs in location /:

location / {
    try_files $uri $uri/ /index.php?$args;
}

Nginx first checks whether $uri is an existing file, then whether it is an existing directory. If neither exists, it internally routes the request to /index.php and appends the original query string through $args. WordPress can then resolve virtual permalinks and generate its own 404 when no content matches.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A representative server block is:

server {
    server_name example.com;
    root /var/www/example.com;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ .php$ {
        include fastcgi.conf;
        fastcgi_intercept_errors on;
        fastcgi_pass unix:/run/php/php-fpm.sock;
    }

    # Use this only when Nginx should render a static 404 page.
    error_page 404 /404.html;
    location = /404.html {
        internal;
    }
}

The PHP location must pass scripts to PHP-FPM. The exact socket in the example is illustrative; replace it with the endpoint that exists on your host. The included FastCGI configuration normally supplies parameters such as the script filename, but inspect the file supplied by your distribution if PHP is not executing correctly.

fastcgi_intercept_errors on; allows Nginx to process error responses from FastCGI using its error_page rules. That is useful for a deliberate Nginx-owned error page, but it means a PHP-generated 404 can be replaced when an error_page 404 directive is present. If WordPress should own the 404, remove the static error_page mapping (or otherwise ensure it is not applied to the WordPress response) and let the request return through the theme.

Choose the layer that should own the response

Criterion Nginx static page WordPress template
Dependency Can work when PHP or WordPress is unavailable. Requires WordPress, the active theme, and PHP-FPM to run.
Design consistency Separate HTML and CSS; it does not automatically inherit the theme. Uses the site’s header, footer, menus, styles, and theme conventions.
Dynamic features No WordPress search form or theme-aware navigation unless reproduced manually. Can call theme functions and include a search form or other dynamic content.
Configuration location Nginx server block, normally with error_page 404 /404.html;. Active theme’s 404.php or templates/404.html, with the normal WordPress fallback.
Operational behavior Very small and independent of the application stack. Consistent with application routing and theme updates, but unavailable during a PHP/WordPress outage.
Status code Can preserve the original 404 when configured as an internal error-page mapping. WordPress emits a 404 for an unresolved URL; do not redirect it to the home page.

Use the static option for a resilient, extremely lightweight response or for failures that occur before WordPress can run. Use the theme option when visitors need the site’s normal navigation, search, branding, and accessibility structure.

Option A: configure a static Nginx 404 page

1. Create the error document in the document root

Create /var/www/example.com/404.html (adjust the path to your root). Keep it self-contained: use a small amount of inline or locally available CSS, and do not assume that WordPress can supply menus or search. Include a clear explanation, a link to the site home, and any essential support contact information.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

2. Map status 404 to that URI

error_page 404 /404.html;

NGINX’s error_page directive specifies an internal redirect to the error URI; it is not an ordinary external redirect sent to the browser. The original not-found status is retained when the page is served through this mechanism.

3. Keep the endpoint internal

location = /404.html {
    internal;
}

internal prevents visitors from requesting the error endpoint as a normal public resource. Nginx can still use it for an error generated during request processing. Make sure the file actually exists; otherwise an error mapping can lead to an unhelpful secondary failure.

4. Decide how PHP errors should be handled

With fastcgi_intercept_errors on;, a 404 returned by PHP is eligible for the Nginx error_page mapping. That is appropriate when every not-found response should have the same static presentation. If your goal is a WordPress theme page, do not combine this mapping with that goal; use the theme configuration below instead.

Option B: let the active WordPress theme render the 404

Leave the WordPress fallback in location / and do not route the application’s 404 through a static Nginx error_page. WordPress’s template hierarchy then selects the appropriate file.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Classic themes: 404.php

Create or edit wp-content/themes/your-active-theme/404.php. A minimal classic-theme structure is:

<?php
get_header();
?>
<main id="main" class="site-main">
    <h1><?php esc_html_e( 'Page not found', 'your-text-domain' ); ?></h1>
    <p><?php esc_html_e( 'The page may have moved or the address may be incorrect.', 'your-text-domain' ); ?></p>
    <?php get_search_form(); ?>
</main>
<?php
get_footer();

Match the active theme’s markup, escaping, localization, landmarks, and CSS classes instead of inserting a disconnected page. A useful 404 should offer a search route or clear navigation without concealing the missing URL.

Block themes: templates/404.html

For a block theme, edit or create wp-content/themes/your-active-theme/templates/404.html. The block-theme hierarchy checks 404.html before falling back to index.html. Build the page with the theme’s Site Logo, Navigation, Query or Search, and other blocks as appropriate, then verify that the template is in the active theme rather than a different installed theme.

Do not turn a 404 into a 200

A common shortcut is to redirect every unknown path to the home page. That removes the useful not-found signal, confuses users, and can make monitoring and search systems treat missing content as valid. Keep the original request’s HTTP status at 404 while offering links or search within the response.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Validate the configuration before and after reload

  1. Save the server block and theme change, then run:
    sudo nginx -t

    Proceed only when Nginx reports that the syntax test is successful. Fix the file and line named in any error.

  2. Reload through the normal service command for the installation. A direct Nginx reload is commonly:
    sudo nginx -s reload

    A reload applies the new configuration without stopping existing workers.

  3. Request an existing static file, such as a known stylesheet or image. This confirms that the document root and static-file handling are correct.
  4. Request an existing pretty permalink. If it fails, the problem is routing or PHP rather than the 404 template itself.
  5. Request a path that cannot exist and inspect the status:
    curl -I https://example.com/this-url-should-not-exist

    The response must be 404. A 200 indicates a soft 404 or an application response that is claiming success; a 301 or 302 indicates a redirect that should be reviewed.

  6. Open the same URL in a browser and confirm the expected presentation: the static Nginx document for Option A, or the active theme’s header, footer, and search/navigation for Option B.

Use a normal body request, such as curl -i, when you need to inspect the returned HTML as well as headers. Test over the public hostname and HTTPS endpoint that visitors use; a different server block may answer requests sent to an IP address or an alternate hostname.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot the symptom you actually see

Every pretty permalink returns an Nginx 404

  • Check that location / contains try_files $uri $uri/ /index.php?$args; and that there is no earlier try_files ... =404 terminating the request.
  • Verify that root points directly to the WordPress directory containing index.php.
  • Look for a more specific location block that takes precedence over location /, or for a different server block selected by the hostname.
  • Confirm that the PHP location passes requests to a working PHP-FPM socket or upstream. If PHP cannot run, WordPress cannot resolve virtual routes.

The plain Nginx page appears instead of the theme page

  • Search the active server block and included configuration files for error_page 404. Remove the mapping if the application should render the response.
  • Check whether fastcgi_intercept_errors on; is allowing Nginx to replace a PHP-generated 404. Interception is useful for a static policy but conflicts with a theme-owned 404 when the error mapping is active.
  • Confirm that the edited 404.php belongs to the active classic theme, or that templates/404.html belongs to the active block theme.
  • Clear any server or page cache only after confirming the configuration; a cached old response can hide a correct change.

The home page appears with a 200 status

Inspect WordPress redirect logic, theme code, and plugins for a catch-all redirect. Remove the redirect and retest with curl -I. A helpful 404 page may link to the home page, but the response itself must remain 404.

PHP files download or appear in the browser

Stop treating this as a template problem. PHP is not being handed to PHP-FPM safely. Correct the PHP location, its FastCGI include, and its fastcgi_pass endpoint before serving the site again. Never leave a server configured to expose PHP source.

An Apache recipe has no effect

Nginx does not read Apache’s per-directory .htaccess files. Rewrite and permalink behavior must be expressed in the Nginx server configuration, especially the try_files fallback. Copying an Apache rule unchanged can leave every virtual WordPress URL at an Nginx-level 404.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Operational details that prevent recurring 404 mistakes

  • Keep one owner per policy: decide whether the application or Nginx owns the final 404, then remove competing mappings that silently replace it.
  • Preserve query strings: the ?$args part of the fallback carries the original query parameters to index.php. Omitting it can change how WordPress handles searches and other parameterized requests.
  • Protect the error endpoint: use an exact-match internal location for a static error file so it is not treated as a normal public page.
  • Keep the template maintainable: use the theme’s escaping, translation, accessibility landmarks, and responsive styles rather than a hard-coded fragment that will drift from the rest of the site.
  • Test after upgrades: PHP-FPM socket names, active themes, and server includes can change during an operating-system, PHP, or theme update. Repeat the existing-file, existing-permalink, and nonexistent-URL tests after such changes.
  • Monitor status, not just appearance: a page that looks like a 404 is not enough. Confirm the HTTP status and watch for accidental redirects or 200 responses.

A concise decision checklist

Choose the static Nginx route when the page must remain available during WordPress or PHP failures, must be exceptionally small, or is intended for server-level errors. Choose the WordPress route when the page needs the active design, menus, search, localization, or other theme-aware behavior. In either case, retain the standard WordPress try_files fallback for virtual permalinks unless the server is intentionally serving a non-WordPress path.

After editing, the complete acceptance test is: sudo nginx -t succeeds; the configuration reloads; a known static file loads; a known permalink loads; a deliberately nonexistent URL returns HTTP 404; and the body is rendered by the layer you selected.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Crashes, No Sound, or Screen Glitches?Free driver scan
PC Slower Than It Used to Be?Free scan - under a minute

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.