1

I managed to deploy my Django app but I can not pass the static files with Nginx. I have followed all the instructions for deploying to production. When I inspect the page all I see is empty static folder Can anyone spot the mistake?

Thanks a lot

nginx.conf

 10 upstream app_upstream {
  9     server app:8080;
  8 }
  7
  6 server {
  5     listen 80;
  4     listen 443;
  1     server_name #######;
  2
  3     location /static/ {
  4         alias /static/;
  5     }
  6
  7     location /media/ {
  8         alias /media/;
  9     }
 10
 11     location / {
 12         proxy_set_header Host $host;
 13         proxy_pass http://app_upstream;
 14     }
 15 }

settings.py

14
 13 STATIC_URL = '/static/'
 12 STATIC_ROOT = '/static/'

docker-compose.yml

....
 12   app:
 13     build: .
 14     ports:
 15       - 8000:8000
 16       - 8080:8080
 17     env_file:
 18       - db_${RTE}.env
 19     volumes:
 20       - .:/app/
 21       - static:/static/
 22       - media:/media/
 23     depends_on:
 24       - db
 25
 26   nginx:
 27     build: nginx/
 28     ports:
 29       - 443:443
 30       - 80:80
 31     volumes:
 32       - ./nginx/${RTE}/conf.d/:/etc/nginx/conf.d/
 34       - static:/static/
 35       - media:/media/
 36     depends_on:
 37       - app
 38
 39 volumes:
 40   static:
...

Error message when I docker-compose:

nginx_1  | 2022/01/10 16:26:17 [error] 10#10: *8 open() "/static/custom.css" failed (2: No such--More
--More--
patbet
  • 93
  • 1
  • 9

1 Answers1

0

Having had a similar issue (but NOT using Docker), perhaps this might help.

If you have a static folder in your root project for project level static files, and if you also have a command to run python manage.py collectstatic --noinput in your docker-compose or Dockerfile, which not understanding Docker too well, I think you'd still need in production, then perhaps renaming the STATIC_URL and STATIC_ROOT in settings.py can help since that command will try to collect all static files from all your apps into one folder. Try something like this:

    STATIC_URL = '/staticfiles/'
    STATIC_ROOT = str(BASE_DIR.joinpath('staticfiles'))
    STATICFILES_DIRS = [str(BASE_DIR.joinpath('static'))]

Note that you would also have to update your Nginx file, i.e.,

    server {
            ...
            location /staticfiles/ {
            ...
           }
raphael
  • 2,469
  • 2
  • 7
  • 19
  • Thanks for the suggestion! I can see the ````/staticfiles/```` was created and it has all the static content in it. When I access the domain though, it does not have styling. All files in my directory are under user "myName". While the ````/staticfiles/```` that was created is under ````root````. ````drwxr-sr-x - root 11 Jan 12:47 -N staticfiles/```` Could that be the issue? – patbet Jan 11 '22 at 11:51
  • Yes, but I think that is how Docker runs. I'm not very familiar with Docker, but perhaps this answers your question: https://stackoverflow.com/questions/50685775/serving-django-static-files-with-docker-nginx-and-gunicorn?rq=1? – raphael Jan 11 '22 at 13:43
  • Thanks! Solved it with lines: `STATIC_URL = '/staticfiles/' STATIC_ROOT = '/static/' STATICFILES_DIRS = [str(BASE_DIR.joinpath('static'))]` – patbet Jan 12 '22 at 22:33