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

macOS环境下Nginx部署Symfony 4.3静态资源404问题求助

Fixing Symfony 4.3 Static Asset 404s on macOS with Nginx

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.css should be present).
  • Make sure test.local is mapped to 127.0.0.1 in your /etc/hosts file (add 127.0.0.1 test.local if 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:27:51