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

Nginx访问Django静态文件提示Permission Denied权限问题求助

Troubleshooting Nginx Permission Denied for Static Files

Hey there, let's figure out why you're hitting that "Permission denied" error when Nginx tries to access your static files. The root cause here is actually not your static folder permissions—it's the parent directory /root that's blocking access.

The Core Issue

Your project is stored under /root/project, but the /root directory has default permissions of drwx------ (only the root user can read/write/execute it). Even though your project and static folders are owned by www-data:www-data, Nginx's worker processes (running as www-data) can't traverse into /root at all—so they never even get to the static folder.

Step-by-Step Fixes

1. Recommended: Move Your Project to a Non-Root Directory (Most Secure)

Web applications shouldn't live in /root for exactly this permission reason. Let's move it to a standard web directory like /var/www:

  • First, back up your project (always a good idea!):
    tar -czf project_backup.tar.gz /root/project
    
  • Create a new directory and move your project:
    mkdir -p /var/www/project
    cp -r /root/project/* /var/www/project/
    
  • Fix permissions for the new location:
    chown -R www-data:www-data /var/www/project
    chmod -R 755 /var/www/project/static
    
  • Update your Nginx config (in /etc/nginx/sites-available/your_config):
    location ^~ /static/ {
        allow all;
        auth_basic off;
        root /var/www/project;  # Update this path to your new project location
    }
    
  • Restart Nginx to apply changes:
    systemctl restart nginx
    

2. Temporary (Not Secure) Fix: Loosen /root Directory Permissions

If you can't move the project right now, you can grant the www-data group permission to traverse /root—but note this reduces security (any user in www-data can now access /root):

chmod g+x /root

After running this, restart Nginx and test again. But please switch to the first method as soon as possible.

Verification Steps

  • Run ls -ld /root to confirm the directory permissions (if using method 2, you should see drwxr-x---).
  • Test accessing a static file directly with curl:
    curl http://your_server_ip/static/your_sample.css
    
  • Check Nginx error logs again with tail -f /var/log/nginx/error.log to ensure no more permission errors.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:54:17