In my Django project, I have static folders for each app, app1/static/, app2/static, etc. It works fine with the development server.
However when I switch to use Django development server + nginx through fastcgi. There’s a problem that I can only map /static to one location. Is it possible, in Nginx, to map app1/static/, app2/static, etc to url /static.
The following config does not seem to work. What’s the correct way to achieve it? Thanks
location ^~ /static/ {
root "app1/static/";app2/static;
}
You should follow https://docs.djangoproject.com/en/dev/howto/static-files/.
What it says is that you serve your files from
static/sub-folders in your app-folders. In your templates you use{{ STATIC_URL }}or in older versions{% get_static_prefix %}. In debug mode under the development server Django will directly serve the files from thestaticsub-folders in your app-folders.When you switch to nginx, you run
collectstaticas Aviral suggests. This collects all files from thestatic/sub-folders into theSTATIC_ROOTfolder. In order to not have all files in one folder, I would suggest to usestatic/app-namefolders in your apps. In this caseSTATIC_ROOTwill be populated with distinctapp-namesub-folders and the static files for each app are separated. The templates must refer to files like{{ STATIC_URL }}/app-name/my_static_image.jpg.Your nginx config should than simply look like: