skip to Main Content

I have django application and nginx, from browser, it access to the file /static.

such as

http://example.com/static/file.png

However my application has files under /staticroot.

http://example.com/staticroot/file.png

it shows the image.

So, I set on nginx.

location /static {
    alias /staticroot;
}

However it doesn’t appear

My environment is Debug mode.

My settings py is like this,

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)

STATIC_ROOT = os.path.join(config("PROJ_PATH"), 'staticroot')

How can I fix this?

2

Answers


  1. In order to serve staticroot content you’ve to rename your STATIC_URL='/staticroot/' & in nginx file make changes like this

    location /staticroot/ {
        alias /home/your_username/project_name;
    }
    

    When request comes to /staticroot/somefile Nginx will look for directory named as staticroot in file system and it will serve provided file.
    So when you define url for your static or media content make sure you’ve created directory with the same name.
    For more info check Serving Static Content on Nginx docs.

    Login or Signup to reply.
  2. in /etc/nginx/sites-available/you_project
    set

    location /static/ {
        root /home/your_admin_name/your_project_folder_name;
    }
    

    in settings.py

    STATIC_URL = '/static/'
    STATICFILES_DIRS = [
        os.path.join(BASE_DIR, 'static'),
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search