Hemen
Hemen

Reputation: 55

Nginx serving Django static files must have folder with same name as URL

I am serving static files using Nginx. My config looks like this:

# django settings
STATIC_URL = '/static_folder/'
STATIC_ROOT = '/app_data/'

# nginx config
location /static_folder/ {
    root /app_data/;
}

It does not work like this. I need to change the STATIC_ROOT to include the static_folder part. Like this:

# django settings
STATIC_URL = '/static_folder/'
STATIC_ROOT = '/app_data/static_folder/' # <-- here

# nginx config
location /static_folder/ {
    root /app_data/;
}

I want to be able to serve like this:

/app_data/logo.png
instead of this:
/app_data/static_folder/logo.png

It is not a big deal if you have one URL part in STATIC_URL but if I use nested URLs, I need to repeat it in STATIC_ROOT too. It gets too deep. For example:

# django settings
STATIC_URL = '/static_folder/and/another/folder'
STATIC_ROOT = '/app_data/static_folder/and/another/folder/' 

# nginx config
location /static_folder/ {
    root /app_data/;
}

How can I get rid of this and serve files in /app_data/ without including static_url parts in the folder structure.

Upvotes: 0

Views: 75

Answers (1)

IVNSTN
IVNSTN

Reputation: 9299

I guess in your case it has to be alias, not root:

location /static_folder/ {
    alias /app_data/;
}

Upvotes: 1

Related Questions