October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk9 min

Nginx Server Block Returning 404? How to Fix a Second Virtual Host

A second NGINX site returning 404 is usually caused by the wrong server block handling the request or by an incorrect URI-to-file mapping. Use explicit Host tests, inspect the effective configuration, verify DNS and paths, and troubleshoot HTTP and HTTPS separately.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

A second NGINX site usually returns 404 for one of two reasons: the request is being handled by a different server block, or the correct block is selected but its root, alias, location, or try_files rules point to a file that does not exist. Prove which server block received the request first, then verify the filesystem mapping.

Why the second virtual host returns 404

NGINX makes two separate decisions for every request. First, it selects a server block using the local address, listening port, and request host name. Only after that does it choose a location and translate the URI into a file path or upstream request.

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.

If the Host value does not match any server_name on that address and port, NGINX sends the request to the default server for that listening context. You may then see the first site’s 404 page, its content, or a deliberate catch-all response even though the second configuration file looks correct.

If the intended block did receive the request, a 404 normally means that the URI did not resolve to a readable file or directory. NGINX’s static-content behavior returns 404 when the requested file is missing. A rule such as try_files $uri $uri/ =404; also deliberately ends in a 404 when neither candidate exists.

#1 Best Overall
Recipe Book to Write in Your Own Recipes, Spiral Blank Cookbook 150 Recipes
  • B5 Recipe Book: This hardcover blank recipe book measures 10''×7.3'', containing 150 blank pages, comes with 1 sheet of DIY category stickers, perfect for writing down your own recipes and organizing them in one place.
  • Practical Layout - 5 pages of conversion charts show you everything you'll ever need to know in the kitchen. The table of contents will help organize your personal favorite recipes. A back pocket is designed for loose items.
  • Easy to Use - This blank cookbook is made of high-quality paper with a beautiful hardcover for long-term use. The sturdy gold double-wire spiral binding makes writing easier and page turning smoother.
  • Make Your Own Recipes - This blank cookbook has up to 150 blank pages for you to write in your own recipes, with plenty of space to record ingredients and instructions. You can write down all the special tips for your family members.
  • Thoughtful Gift - A personalized recipe book makes a thoughtful gift for your children or newlyweds, and a great gift for housewarming, weddings, bridal showers, anniversaries, or any other occasion.

How NGINX chooses a server block

Listening address and port are considered first

Server blocks are grouped by their listen address and port. A block listening on port 80 is not used to serve a request that arrived on port 443, even if both blocks contain the same server_name. The same distinction applies when one site listens on a specific address and another listens on a wildcard address.

Within the matching address-and-port group, NGINX compares the request’s host name with the configured server_name values. The apex name and the www name are separate names unless both are listed.

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

The default server belongs to the listen port

default_server is a property of a listen directive, not of a domain name. For a given address and port, the default block handles requests that do not match another name. The first server declared for that address and port is otherwise used as the default when no block is explicitly marked.

NGINX documentation summarizes this rule as: “The default server is a property of the listen port and not of the server name.” That is why changing a server_name alone does not change which block receives unmatched traffic.

A reliable 404 troubleshooting sequence

Follow these checks in order. Each step eliminates a different failure class, so avoid jumping straight to changing root or copying the first site’s configuration.

  1. Validate syntax before inspecting behavior

    Run:

    sudo nginx -t

    Do not reload a configuration that fails this test. A successful result confirms syntax and that NGINX can load the referenced files, but it does not prove that the request will select the intended block.

    Free tools Windows power users keep installed

    One-click scans. No signup required.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  2. Print the complete effective configuration

    Run:

    sudo nginx -T

    Search the output for every listen, server_name, root, alias, index, location, try_files, return, and proxy_pass. This shows included files and duplicate definitions that may not be obvious from the file you edited.

    To check how the running binary was built and where it expects its main configuration, inspect:

    nginx -V 2>&1

    The output includes the configure arguments, including the compiled configuration path. Use that path when an installation does not follow the distribution’s usual directory layout.

  3. Confirm that the site file is active

    On Debian or Ubuntu, a file in /etc/nginx/sites-available/ is not active merely because it exists. Check that the intended file is linked into /etc/nginx/sites-enabled/ and that the link points to the file you edited.

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

    On other systems, inspect the include directives shown by nginx -T. If the file is not included, NGINX cannot select its server block.

  4. Compare the listen contexts

    Check whether both sites listen on the address and port you expect. A typical pair of HTTP blocks may both use listen 80;. If one uses listen 127.0.0.1:80; and the other uses listen 0.0.0.0:80;, they are not equivalent contexts.

    Look for accidental or conflicting default_server declarations. Only one default server can apply to a particular address-and-port combination. A second declaration for the same combination normally causes a configuration error rather than silently selecting the block you intended.

  5. Test the exact Host header over HTTP

    Do not test only by IP address. A request made to an IP uses that IP as the host value, which may select a different server block. Send an explicit host header instead:

    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.
    curl -i -H 'Host: second.example' http://SERVER_IP/

    Test the www name separately:

    curl -i -H 'Host: www.second.example' http://SERVER_IP/

    If the first command returns the second site’s expected response but a browser does not, the problem is likely DNS, browser caching, a proxy, or the name being entered differently. If both commands return the same 404 from the first site, inspect server selection and the active configuration.

  6. Test HTTPS with SNI and the intended address

    Port 443 has its own server-selection context. Use --resolve to connect to a known address while sending the desired HTTPS name through DNS resolution and SNI:

    curl -ik --resolve second.example:443:SERVER_IP https://second.example/

    The -k option bypasses certificate verification for diagnosis only. It does not fix a certificate problem. If HTTP works but this command returns the wrong content or a 404, inspect the port-443 block, its certificate configuration, server_name, locations, and root independently of the port-80 block.

  7. Check DNS independently of NGINX

    Verify that the public name resolves to the address where this NGINX instance is running:

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
    dig +short second.example

    A correct server block cannot handle traffic that reaches another server, load balancer, CDN, or old address. Check the apex and www records separately because they can point to different destinations.

  8. Verify the root, file, and directory traversal permissions

    Suppose the selected block contains:

    root /var/www/second;

    For a request to / with index index.html;, verify that /var/www/second/index.html exists. For a request to /assets/app.css, verify that the corresponding file is at /var/www/second/assets/app.css.

    Rank #2
    Pipilo Press Blank Family Cookbook Recipe Binder - 6.5 x 8.2 in Hardcover Spiral Bound - 90 Ruled Pages with Memo Pocket - Write In Your Own Recipes & Meal Planning Notebook
    • Create a Culinary Legacy: The Pipilo Press Blank Family Cookbook Recipe Binder offers a timeless platform for chronicling beloved recipes. With its hardcover and spiral binding, this recipe book to write in your own recipes ensures durability and ease of use for generations
    • Thoughtful Gifting Choice: This book for writing recipes is an ideal present for various occasions, adding a personal touch to celebrations. Its elegant design makes it perfect for moms cookbook DIY projects or anyone passionate about preserving family traditions
    • Durable Design Features: Constructed with a 300gsm cardstock cover and matte lamination, this cookbook you can write in ensures longevity. Its spiral-bound format provides convenient access to pages, making it a practical tool for everyday use
    • Organized Recipe Storage: The included memo pocket offers a space to store handwritten notes or recipe cards, enhancing its functionality. This feature transforms the recipe book for moms into a sentimental family memento
    • Ample Writing Space: Measuring 6.5 x 8.2 inches, this blank spiral family recipe cookbook offers plenty of room for detailed notes. It is perfect for jotting down ingredients, preparation methods, and serving suggestions for a complete culinary record

    The NGINX worker must be able to traverse every parent directory and read the target file. Check ownership and mode bits on the document root and its parents. A permission problem often appears in the error log as an open() failure; do not “fix” it by making the entire tree world-writable.

  9. Read access and error logs while reproducing the request

    Use the paths configured by access_log and error_log, then make one request and inspect the corresponding entries. The access log can confirm the host, URI, status, and selected server fields when those fields are present in the log format. The error log may show the exact filesystem path NGINX attempted, a permission denial, a rewrite loop, or an upstream failure.

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

    If the log format does not include the host or server name, temporarily add those fields to a diagnostic format in a controlled environment, reload after validation, and remove the extra logging when finished.

  10. Inspect location precedence and try_files

    A nested prefix location or regular-expression location can override the root you expected from the server block. Review the complete location tree in nginx -T, not only location /.

    For static content, this common pattern is intentional:

    try_files $uri $uri/ =404;

    NGINX checks whether the URI maps to a file, then to a directory, and returns 404 if neither exists. For an application, the final argument normally needs to be the documented front-controller or named-location fallback rather than =404. The correct fallback depends on the application and should not be copied blindly from an unrelated site.

    Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  11. Reload only after validation, then repeat the same tests

    Use:

    sudo nginx -t && sudo systemctl reload nginx

    A reload applies the validated configuration without unnecessarily stopping the service. Repeat the explicit HTTP and HTTPS curl commands after the reload. Testing with the same host, path, and address makes it clear whether the change altered server selection or only filesystem lookup.

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

Minimal working configuration for a static second site

This is a complete baseline for a second static site on HTTP:

server {
    listen 80;
    server_name second.example www.second.example;
    root /var/www/second;
    index index.html;

    location / {
        try_files $uri $uri/ =404;
    }
}

The two names must be the names clients actually send, and /var/www/second must contain the requested files. If the site should answer only one name, remove the other name rather than relying on an accidental redirect.

After creating the file, confirm that it is included, run sudo nginx -t, reload, and test both host names explicitly. A successful configuration test alone does not verify that index.html exists or that DNS points to this server.

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

Root versus alias: the path may not be what you expect

How root builds a path

With root /var/www/second;, NGINX appends the request URI to the root directory. A request for /images/logo.svg is therefore looked up beneath /var/www/second/images/logo.svg, subject to the active location.

How alias changes the mapping

alias replaces the portion matched by the location instead of simply appending the entire URI. For example, an alias inside location /downloads/ must be written with care so that trailing slashes and the matched prefix produce the intended path. Mixing a server-level root with an alias in a nested location is a common reason one URL works while another returns 404.

When diagnosing, write down the exact URI, identify the location that wins, and calculate the resulting filesystem path before changing permissions or moving files.

HTTP and HTTPS must be fixed separately

Port 80 and port 443 do not share server-selection state. A working HTTP block does not automatically configure HTTPS. The HTTPS block needs its own listen 443 context, name matching, certificate settings, root or upstream, locations, and fallback behavior.

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

Common mistakes include adding the domain only to the port-80 server_name, leaving the port-443 block pointed at the first site’s root, or copying a redirect block that sends traffic to a different host. Use the two explicit curl tests to establish which protocol is failing before editing either block.

Use a deliberate catch-all for unmatched hosts

An explicit catch-all makes accidental host mismatches visible:

server {
    listen 80 default_server;
    server_name _;
    return 404;
}

The underscore has no special catch-all meaning. The behavior comes from default_server on the listen directive; the underscore is simply a name that is unlikely to match a real host.

This policy prevents an unknown domain or an IP-based request from displaying another customer’s site. It also makes troubleshooting easier: a 404 from the catch-all indicates that the request did not match a named virtual host, while a 404 from the intended block points toward its path, location, or application rules.

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

Common symptoms and the most likely fix

Symptom What it usually proves Next check
The second domain shows the first domain’s page A different server block is probably handling the request. Run an explicit-Host curl test and inspect listen, server_name, and enabled includes.
The response is the catch-all 404 The host did not match a named block on that address and port. Check spelling, apex versus www, DNS destination, and the relevant HTTP or HTTPS port.
Explicit host testing reaches the second block but still returns 404 Server selection is working; URI-to-file mapping or application fallback is failing. Check root, alias, location precedence, try_files, and the requested file.
The home page works but assets return 404 The asset URI may be handled by a different location or mapped to the wrong directory. Calculate the path produced for the asset URI and inspect nested or regex locations.
HTTP works but HTTPS returns 404 The port-443 block is different, missing, or still points to another site’s configuration. Use curl --resolve and inspect the complete 443 configuration.
Changing a file has no effect The file may not be included, or NGINX may not have been reloaded. Confirm it appears in nginx -T, validate, reload, and retest.

Final verification checklist

  • The intended configuration appears in sudo nginx -T.
  • The site file is enabled through the appropriate include path.
  • The requested name, including apex or www, appears in the correct server_name.
  • The block listens on the address and port used by the request.
  • HTTP and HTTPS have been tested independently.
  • DNS resolves each name to the intended server address.
  • The active location maps the URI to the expected file or application fallback.
  • The document root and every parent directory are traversable by the NGINX worker.
  • Logs confirm the host, URI, selected path, and status during reproduction.
  • sudo nginx -t succeeds before every reload.

The key distinction is simple: server selection happens before filesystem lookup. Prove which block received the request, then prove that block can resolve the URI. That method fixes both classes of second-virtual-host 404 without relying on guesswork.

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 *

Free tools Windows power users keep installed

One-click scans. No signup required.

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
Windows Errors? Fix Them Before They SpreadFree repair scan

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.