S
S
Sergey Nizhny Novgorod2016-10-10 01:09:04
Django
Sergey Nizhny Novgorod, 2016-10-10 01:09:04

Are there examples of Django static caching config for Nginx?

Hello.
Google swears at me that there is no Expire date for statics (css/js/media). As it turned out, I need to write this line in nginx. And something I'm stupid with how it should all look. Can you tell me what lines I need to add to make everything spin?

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {

  ##
  # Gzip Settings
  ##
  gzip on;
  gzip_disable "msie6";
  gzip_vary on;
  gzip_proxied any;
  gzip_comp_level 5;
  gzip_buffers 16 8k;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  

  ssl_session_cache   shared:SSL:10m;
    ssl_session_timeout 5m;
    ssl_prefer_server_ciphers on;
    ssl_stapling on;
    resolver 8.8.8.8;
  
    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  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen 443 ssl;
    ssl_certificate /etc/ssl/your_domain.crt;      
        ssl_certificate_key /etc/ssl/your_domain.key;  
        server_name bakot.ru www.bakot.ru;

        location = /favicon.ico { access_log off; log_not_found off; }
        location /static/ {
            root /home/bakotiinii/bakot;
        }

        location  /media/ {
            root /home/bakotiinii/bakot/;
        }
        
        location  /robots.txt {
            root /home/bakotiinii/bakot/faceset/static;
        }

        location / {
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_pass http://unix:/home/bakotiinii/bakot/bakot.sock;
        }
    
    error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

    server {        
    listen 80;
    server_name bakot.ru;
    return 301 https://$host$request_uri;        
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Nizhny Novgorod, 2016-10-10
@Terras

I put it like this, it seems to work.

location /static/ {
            root /home/bakotiinii/lieman;
      access_log off;
      expires 15d; 
        }

        location  /media/ {
            root /home/bakotiinii/lieman/;
      access_log off;
      expires 15d;
        }

Is this a valid option?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question