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

Nginx新手求助:如何移除URL中的frontend文件夹路径

Fixing Nginx URL Rewrite to Remove 'frontend' from Your URLs

Hey there! I get that figuring out Nginx rewrites can be tricky when you're just starting out—let's get this sorted for you.

Your goal is to turn URLs like localhost/project_name/frontend/dashboard into localhost/project_name/dashboard, while still serving the actual files from the frontend folder (where all your templates live). The issue with your original config is that it's targeting the wrong path and the rewrite logic isn't pointing to the right files.

Here's a working configuration tailored to your needs:

server {
    listen 80;
    server_name localhost;
    # Replace this with the actual path to the folder containing your 'project_name' directory
    root /var/www/html; 

    # Handle all requests under /project_name
    location /project_name {
        # First check if the requested file/directory exists directly
        try_files $uri $uri/ @rewrite-to-frontend;
    }

    # Named location to rewrite requests to the frontend folder
    location @rewrite-to-frontend {
        # Rewrite /project_name/xxx to /project_name/frontend/xxx
        rewrite ^/project_name/(.*)$ /project_name/frontend/$1 last;
        # If you need support for extensionless PHP files (like dashboard → dashboard.php)
        rewrite ^/project_name/(.*)$ /project_name/frontend/$1.php last;
    }

    # Ensure PHP files are parsed correctly
    location ~ \.php$ {
        # Adjust this to match your PHP-FPM socket path (depends on your PHP version)
        fastcgi_pass unix:/run/php/php8.2-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Let's break down what this does:

  • Root Directory: Make sure the root directive points to the parent folder of project_name—this lets Nginx locate your frontend subfolder correctly.
  • Main Location Block: The location /project_name catches all requests to your project. It first checks if the requested path exists (in case you have files outside frontend), then falls back to our rewrite rule.
  • Rewrite Rule: The @rewrite-to-frontend block takes any path after /project_name/ (like dashboard) and maps it to /project_name/frontend/dashboard, so Nginx looks for the file in the correct folder. The second rewrite handles extensionless PHP files, which matches your original @extensionless-php intent.
  • PHP Handling: The final location block ensures Nginx passes PHP files to your PHP-FPM process for parsing—critical if your templates are PHP-based.

Steps to test this:

  1. Replace the root path and PHP-FPM socket path with values that match your server setup.
  2. Run sudo nginx -t to check for configuration errors.
  3. If everything checks out, reload Nginx with sudo systemctl reload nginx.
  4. Now try accessing localhost/project_name/dashboard or localhost/project_name/feed—they should load without the frontend segment, and no more 404s!

What was wrong with your original config?

Your original location /project_name/frontend was targeting URLs that already include frontend, but you want to remove that segment from the URL. The rewrite logic was also pointing to the wrong path, which is why Nginx couldn't find your files and threw a 404.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 03:57:44