Django: nginx static content not working

I am using django with nginx and gunicorn. nginx should serve static content, but css, images and js files are not loaded in the browser. Why is this?

I changed the name of the Django project to domain

.

/etc/nginx/sites-enabled/domain.tld

server {
    listen 80;
    server_name 127.0.0.1;

    access_log /srv/domain/access.log;
    error_log /srv/domain/error.log;

    location /static {
        alias /srv/domain/collected_static;
    }

    location / {
        proxy_pass http://127.0.0.1:8888;
   }
}

      

/etc/nginx/conf/nginx.conf

user http;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
#                  '$status $body_bytes_sent "$http_referer" '
#                  '"$http_user_agent" "$http_x_forwarded_for"';

#access_log  logs/access.log  main;

sendfile        on;
#tcp_nopush     on;

#keepalive_timeout  0;
keepalive_timeout  65;

#gzip  on;

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
        root   html;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   html;
    }

    # proxy the PHP s to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP s to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache document root
    # concurs with nginx one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
#    listen       8000;
#    listen       somename:8080;
#    server_name  somename  alias  another.alias;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}


# HTTPS server
#
#server {
#    listen       443;
#    server_name  localhost;

#    ssl                  on;
#    ssl_certificate      cert.pem;
#    ssl_certificate_key  cert.key;

#    ssl_session_timeout  5m;

#    ssl_protocols  SSLv2 SSLv3 TLSv1;
#    ssl_ciphers  HIGH:!aNULL:!MD5;
#    ssl_prefer_server_ciphers   on;

#    location / {
#        root   html;
#        index  index.html index.htm;
#    }
#}
include /etc/nginx/sites-enabled/*;

}

      

gunicorn.conf.py

bind = "127.0.0.1:8888"
logfile = "/srv/domain/gunicorn.log"
loglevel = "info"
workers = 3

      

Excerpt from Django settings

DEPLOY_PATH = os.path.dirname(os.path.realpath(__file__))

STATIC_ROOT = os.path.join(DEPLOY_PATH, 'collected_static')

STATIC_URL = '/static/'

      

EDIT: Logging out of the machine (links to pastebin):

ps aux | grep nginx

ls -l * .log

+3


source to share


1 answer


Your config looks correct. As long as the files are actually compiled, Django and Gunicorn configurations have nothing to do with static files. The following possibilities come to my mind:

  • The files are not yet collected in your collect_static ( ./manage.py collectstatic

    ) directory
  • Nginx does not have access to read files
  • You are using an older version of nginx which has problems with your current configuration. You should be using the current 1.x version, if you are on Debian use the Deb repository from nginx.org.

If permissions are not an issue, check the nginx access file to see if the requests actually reach Nginx. Then check the nginx error log to see if any errors were logged.



As a side item (but not related), I recommend putting proxy headers in the location config /

and moving the app server config to a separate section, like this:

upstream app_server {
    server localhost:8888 fail_timeout=0;
}


server {
    ...
    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;

        proxy_pass http://app_server;
        break;
    }
}

      

+2


source







All Articles