Nginx访问Django静态文件提示Permission Denied权限问题求助
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 /rootto confirm the directory permissions (if using method 2, you should seedrwxr-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.logto ensure no more permission errors.
内容的提问来源于stack exchange,提问作者Dora

