Search code examples
djangonginxhttp-status-code-403django-staticfilesubuntu-22.04

Django - 403 error while accessing static files in ec2 ubuntu server with nginx


I have used nginx default port 80. Deployed successfully django app. But got 403 error while accessing static files.. I know it is permission related issue. So I tried all the ways. but nothing is workout.

gave 777 permission and www-data user. still I got permission denied error.

file path structure

STATIC_ROOT = os.path.join(CORE_DIR, 'staticfiles')
STATIC_URL = "static/"

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(CORE_DIR, 'core/static'),
)
location /static/ {
        root /home/ubuntu/FunQuiz-backend/FunQuizAPI/staticfiles/;
        autoindex on;
    }

Important: when I give wrong path in static location.. still I got 403 permission denied error


Solution

  • Change root to alias

    location /static/ {
        alias /home/ubuntu/FunQuiz-backend/FunQuizAPI/staticfiles/;
        autoindex on;
    }
    

    Change user to root or ubuntu in /etc/nginx/nginx.conf

    user    ubuntu;  ## or root if ubuntu also gives permission denied error
    

    Restart nginx for changes to take effect.