macOS环境下Nginx部署Symfony 4.3静态资源404问题求助
Hey there, let's troubleshoot why your Symfony 4.3 static assets are returning 404s on macOS! I've run into this exact issue before, so here's what you need to check and fix:
1. Update Your Nginx Configuration
Your current config routes all unrecognized requests to index.php, but it doesn't explicitly handle static assets—this can lead to Nginx not properly serving files like CSS, JS, or images. Add a dedicated location block for static resources to your server config:
server { server_name test.local; root /Users/user/Documents/Project/test/public; # Serve static assets directly (bypass PHP) location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { expires 1y; add_header Cache-Control "public, immutable"; try_files $uri =404; } location / { try_files $uri /index.php$is_args$args; } location ~ ^/index\.php(/|$) { fastcgi_pass 127.0.0.1:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param DOCUMENT_ROOT $realpath_root; internal; } # Block access to hidden files (like .git) location ~ /\. { deny all; access_log off; log_not_found off; } }
This block tells Nginx to directly serve files with common static asset extensions, without passing them to PHP. The expires and Cache-Control headers are optional but help with performance.
2. Verify File Permissions
Nginx runs as the _www user on macOS by default, so make sure it has read access to your public directory and its contents:
# Set correct ownership sudo chown -R _www:_www /Users/user/Documents/Project/test/public # Set proper read/write/execute permissions sudo chmod -R 755 /Users/user/Documents/Project/test/public
3. Ensure Symfony Assets Are Installed
If you're using bundled assets (like from Symfony's core or third-party bundles), run the following command to copy them to your public directory:
php bin/console assets:install public
This will populate the public/bundles folder with the necessary static files.
4. Reload Nginx
After updating your config, reload Nginx to apply the changes:
sudo nginx -s reload
5. Double-Check Paths and DNS
- Confirm your static files exist in the correct location (e.g.,
public/css/app.cssshould be present). - Make sure
test.localis mapped to127.0.0.1in your/etc/hostsfile (add127.0.0.1 test.localif missing).
That should resolve the 404 errors for your static assets! Let me know if you run into any other issues.
内容的提问来源于stack exchange,提问作者Anatilij K

