skip to Main Content

I haveNginx + Gunicorn + Django server deployed on Ubuntu. It running with Debug=False settings and everything works fine. CSS is loaded fine, JS works as well. But when I try to update the css file or js file, the changes I made are not reflected when I run the server.
I tried to update an old static files, also static file created after collectstatic command, I also tried clean collectstatic as well as systemctl restart nginx (and gunicorn). I also cleaned my cache in browser. But when I look a page source code, these changes are not there.

this is how my nginx config looks like

server {
    listen       80;
    server_name  mediadbin.n-media.co.jp;
    client_max_body_size 500M;
    access_log /home/mediaroot/mediadbin/logs/nginx-access.log;
    error_log /home/mediaroot/mediadbin/logs/nginx-error.log;
    server_tokens off;

    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host               $host;
        proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host   $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Real-IP          $remote_addr;
    }

    location /static { $ I TRIED APPLY CHANGES HERE <- NOTHING happens
        alias /home/mediaroot/mediadbin/mediadbin/static;
    }

    location /media {
        alias /home/mediaroot/mediadbin/mediadbin/media;
    }

    include global404;


}

HERE is my gunicorn config


#!/bin/bash
# Name of the application
NAME="mediadbin"

# Django project directory
DJANGODIR=/home/mediaroot/mediadbin/mediadbin

# how many worker processes should Gunicorn spawn
NUM_WORKERS= $(( $(nproc) * 2 + 1 ))

# which settings file should Django use
DJANGO_SETTINGS_MODULE=mediadbin.settings

# WSGI module name
DJANGO_WSGI_MODULE=mediadbin.wsgi
echo "Starting $NAME as `whoami`"

# Activate the virtual environment
cd $DJANGODIR
ENV=new_django
source /root/.virtualenvs/new_django/bin/activate



# Start your Django Unicorn
# Programs meant to be run under supervisor should not daemonize themselves (do not use --daemon)

exec gunicorn ${DJANGO_WSGI_MODULE}:application 
--timeout 600
--name $NAME 
--workers &NUM_WORKERS 
--bind=127.0.0.1 
--log-level=debug 
--log-file=-
>>> sudo find / -name menu_detail_look.js
/home/mediaroot/mediadbin/mediadbin/static/main_app/js/menu_detail_look.js
/home/mediaroot/mediadbin/mediadbin/main_app/static/main_app/js/menu_detail_look.js

↑ Updated both, nothing happens (no err in js also)

2

Answers


  1. First of all, if you set static location nginx then static files will definitely be loaded through nginx.

    make you static block like (instead of alias use root):

    location /static/ {
            root /home/mediaroot/mediadbin/mediadbin/static/;
    }
    

    Also make sure static root is set to: /home/mediaroot/mediadbin/mediadbin/static/

    Running collectstatic will copy all static files to static root folder and nginx will serve it.

    Login or Signup to reply.
  2. Hmm it’s been so long , Aliright if now anyone working on this can check this solution

    in urls.py

    from django.conf import settings
    from django.conf.urls.static import static
    

    then at bottom define

    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    OR (DEPEND on your env.)

    if settings.DEBUG:
        # static files (images, css, javascript, etc.)
        urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
    

    and in settings.py

    define media path

    MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
    MEDIA_URL = "/media/"
    

    Make sure to define path in /etc/nginx/sites-available/domainxxx

    If your folder path is /home/admin/django/project/media

    location /media/ {
        root /home/admin/django/project;
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search