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

Nginx部署网站仅首页可访问,其他页面报404错误求助

Troubleshooting Nginx 404s for Non-Homepage Routes

Hey there! Let's figure out why only your homepage loads while every other route throws a 404. Since nginx -t checks out and your error.log is empty, this is almost certainly a routing misconfiguration—let's break down the most common fixes based on your site type.

1. If You're Running a Single-Page App (SPA: React, Vue, Angular, etc.)

SPAs handle routing entirely on the frontend, but Nginx doesn't know that by default. When you visit /about, Nginx tries to find a physical file at /path/to/your/site/about (which doesn't exist), hence the 404.

Fix this by adding a try_files directive to your location block to forward all non-file requests to your root index.html:

server {
    listen 80;
    server_name your-domain.com;
    root /path/to/your/website/files;
    index index.html index.htm;

    location / {
        # First check if the requested file exists, then the directory, then fall back to index.html
        try_files $uri $uri/ /index.html;
    }
}

2. If You're Running a Multi-Page Static Site

If your site has physical files like about.html, contact.html, etc., Nginx might not be looking for the .html extension when you visit /about.

Adjust your location block to check for the .html version of the requested path:

server {
    listen 80;
    server_name your-domain.com;
    root /path/to/your/website/files;
    index index.html index.htm;

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

This tells Nginx to first look for the exact file, then the same path with .html appended, then the directory, and finally return 404 if none exist.

3. If You're Using a CMS (WordPress, Drupal, etc.)

CMS platforms often use "pretty permalinks" that rely on rewriting requests to a central index file (like index.php). Add this rule to your location block:

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

This ensures all non-static requests get passed to your CMS's index file for processing.

Final Steps

After updating your configuration:

  • Test the config again with nginx -t to make sure you didn't introduce typos
  • Reload Nginx to apply changes: sudo systemctl reload nginx
  • Double-check that your root directive points to the correct directory where all your site files are stored

If you still run into issues, feel free to share the full content of your sites-available config file—we can dig deeper!

内容的提问来源于stack exchange,提问作者Gurjyot Singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 09:37:16