Nginx新手求助:如何移除URL中的frontend文件夹路径
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
rootdirective points to the parent folder ofproject_name—this lets Nginx locate yourfrontendsubfolder correctly. - Main Location Block: The
location /project_namecatches all requests to your project. It first checks if the requested path exists (in case you have files outsidefrontend), then falls back to our rewrite rule. - Rewrite Rule: The
@rewrite-to-frontendblock takes any path after/project_name/(likedashboard) 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-phpintent. - 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:
- Replace the
rootpath and PHP-FPM socket path with values that match your server setup. - Run
sudo nginx -tto check for configuration errors. - If everything checks out, reload Nginx with
sudo systemctl reload nginx. - Now try accessing
localhost/project_name/dashboardorlocalhost/project_name/feed—they should load without thefrontendsegment, 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

