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

在Digital Ocean的Nginx服务器中隐藏URL的.html扩展名遇404/505错误求助

Fixing .html Extension Hiding on Nginx (DigitalOcean)

Hey there! I see you tried a PHP extension removal guide which didn't pan out—no worries, hiding .html extensions needs a tailored Nginx config setup. Let's walk through this step by step to get it working without those frustrating 404 or 505 errors.

Step 1: Locate Your Nginx Site Configuration

First, SSH into your DigitalOcean droplet and find your site's config file. It’s usually stored in one of these paths:

  • /etc/nginx/sites-available/your-domain.com (linked to sites-enabled for active sites)
  • /etc/nginx/conf.d/your-domain.conf

Open it with an editor like nano:

sudo nano /etc/nginx/sites-available/your-domain.com

Step 2: Add the Correct Rules for .html Extension Hiding

Inside your server block, replace or add the following rules. I’ll break down each part so you know exactly what’s happening:

# Optional: Redirect existing .html URLs to clean versions (great for SEO)
rewrite ^/(.*)\.html$ /$1 permanent;

# Serve clean URLs by checking for matching .html files
location / {
    try_files $uri $uri.html $uri/ =404;
}

What these lines do:

  • The rewrite rule sends a permanent (301) redirect for any URL ending with .html to the same path without the extension. This keeps existing links working and helps with search rankings.
  • The try_files directive tells Nginx to:
    1. First try serving the exact URI as-is (e.g., /about).
    2. If that doesn’t exist, try appending .html (so it loads /about.html behind the scenes).
    3. If neither exists, return a proper 404 error.

Step 3: Fix Common Issues That Cause 404/505 Errors

  • Verify your root directory: Make sure the root directive in your server block points to the correct folder where your HTML files live (e.g., root /var/www/your-domain.com/html;). A wrong path is a top cause of 404s.
  • Remove conflicting rules: If you have leftover PHP extension removal rules from your earlier attempt, comment them out or delete them to avoid config conflicts.
  • Test before restarting: Always validate your Nginx config to catch syntax errors that trigger 505s:
    sudo nginx -t
    
    If you see "test is successful", reload Nginx to apply changes:
    sudo systemctl reload nginx
    

Step 4: Test the Setup

Visit your site using a clean URL (e.g., your-domain.com/about instead of your-domain.com/about.html). It should load the corresponding HTML file without showing the extension. If you still get a 404, double-check that the .html file exists in your root directory (Nginx is case-sensitive

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:52:32