You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NGINX仅匹配主域名不匹配www子域名问题求助

Hey there! Let's troubleshoot why your www.domain1.ca and www.domain2.us are returning 404s while the root domains work perfectly. This is a super common NGINX hiccup—usually just a missing line or misconfigured server_name directive. Let's break this down step by step.

1. Check Your server_name Directives

The most likely culprit is that your NGINX server blocks aren't explicitly including the www. subdomains in their server_name lists. NGINX only routes requests to server blocks where the requested hostname matches one of the values in server_name.

Fix Example: Explicitly List Both Domains

Update each server block to include both the root domain and its www. counterpart:

server {
    listen 80;
    listen [::]:80;

    # Include both root and www subdomain
    server_name domain1.ca www.domain1.ca;

    root /var/www/domain1; # Ensure this path matches your root domain's setup
    index index.html index.htm;

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

server {
    listen 80;
    listen [::]:80;

    server_name domain2.us www.domain2.us;

    root /var/www/domain2;
    index index.html index.htm;

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

Alternative: Use Wildcards (If You Want All Subdomains)

If you plan to use other subdomains later, you can use a wildcard to match all subdomains (including www):

server {
    listen 80;
    listen [::]:80;

    # Matches any subdomain of domain1.ca (www, app, api, etc.)
    server_name *.domain1.ca;

    root /var/www/domain1;
    index index.html index.htm;

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

Just note that wildcards only work at the start or end of the hostname (e.g., *.domain1.ca or domain1.*—sub.*.ca won't work).

2. Verify No Conflicting Server Blocks

Sometimes a default server block (marked with default_server in the listen directive) might be catching your www requests if your target server blocks don't match. Double-check that you don't have a generic server { listen 80 default_server; ... } block that's overriding your domain-specific configs.

3. Optional: Redirect www to Root Domain (Or Vice Versa)

If you want to standardize traffic (e.g., send all www users to the root domain), you can add a redirect server block. This ensures no 404s and keeps your URL structure consistent:

# Redirect www.domain1.ca to domain1.ca
server {
    listen 80;
    listen [::]:80;
    server_name www.domain1.ca;
    return 301 $scheme://domain1.ca$request_uri;
}

# Main domain server block
server {
    listen 80;
    listen [::]:80;
    server_name domain1.ca;
    root /var/www/domain1;
    index index.html index.htm;

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

4. Test and Reload NGINX

After making changes, always validate your config to catch syntax errors:

nginx -t

If you get a "test is successful" message, reload NGINX to apply the changes:

# For systemd-based systems (Ubuntu 16.04+, Debian 9+, etc.)
sudo systemctl reload nginx

# For older systems using init.d
sudo service nginx reload

Common Pitfalls to Watch For

  • Incorrect Root Path: Make sure the root directive in your www-enabled server block points to the same directory as your root domain—if it's pointing to a non-existent folder, you'll get a 404.
  • SSL Configs: If you're using HTTPS, repeat the same fixes for your listen 443 server blocks (don't forget to include www in server_name there too).

内容的提问来源于stack exchange,提问作者Jacob Ensor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 07:03:24