Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX cache static files

Tags:

nginx

static

I'm having some trouble defining a rule to cache my static files. I've found this solution:

location ~* \.(ico|js|css|png|gif|jpe?g)$ {   expires 7d; } 

which actually looks like what I need. The problem is, if I include this code into my NGINX.conf, no static files are delivered anymore and my site is blank. Any ideas/hints what might cause this result? Maybe I have to add, that the static files are distributed in different directories :/. My NGINX config file looks like this:

server {   server_name               bla.domain.com;    listen                    80;   root                      /var/repo/;                                 location / {     default_type            text/html;     index                   index.html;      if ($request_method !~ ^(GET)$ ) {       return 444;     }      if ($http_user_agent ~* LWP::Simple|BBBike|wget) {       return 403;     }      if ( $http_referer ~* (babes|forsale|girl|jewelry|love|nudit|organic|poker|porn|sex|teen) ) {       return 403;     }   }    location /bf/football/ {     alias   /var/repos/f20;   }    location /bf/f20/ {     alias   /var/repo/f20;   }    location /bf/zoo/ {     alias   /var/repo/zoo/;   }    location /kbloader/ {     alias   /var/repo/kbloader/;   } } 

Would be nice if someone could help me out with this or point me in the right direction.

like image 885
Szop Avatar asked Oct 22 '13 10:10

Szop


People also ask

Does nginx cache js files?

NGINX is a powerful tool for static content delivery (ex. images, JavaScript, CSS, etc.). It proposes a simple mechanism for caching on a client's side allowing to reduce server load and increase content delivery speed. Below an example of server configuration is shown.

How long will nginx cache CUR files?

It caches RSS and ATOM feeds for 1 hour, Javascript and CSS files for 1 year, and other static files ( images and media ) for 1 month. The caches are all set to "public", so that any system can cache them. Setting them to private would limit them to being cached by private caches, such as our browser.

Where are nginx static files stored?

To serve static files with nginx, you should configure the path of your application's root directory and reference the HTML entry point as the index file. In this example, the root directory for the snake deployment is /home/futurestudio/apps/snake which contains all the files.

How do I nginx cache?

NGINX uses a persistent disk-based cache located somewhere in the local file system. So start by creating the local disk directory for storing cached content. Next, set the appropriate ownership on the cache directory. It should be owned by the NGINX user (nginx) and group (nginx) as follows.


2 Answers

Put this before your other location block:

location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {     expires 30d;     add_header Vary Accept-Encoding;     access_log off; } 

That should work.

You could also use this:

## All static files will be served directly. location ~* ^.+\.(?:css|cur|js|jpe?g|gif|htc|ico|png|html|xml|otf|ttf|eot|woff|woff2|svg)$ {     access_log off;     expires 30d;     add_header Cache-Control public;      ## No need to bleed constant updates. Send the all shebang in one     ## fell swoop.     tcp_nodelay off;      ## Set the OS file cache.     open_file_cache max=3000 inactive=120s;     open_file_cache_valid 45s;     open_file_cache_min_uses 2;     open_file_cache_errors off; } 
like image 57
Joost van der Laan Avatar answered Sep 23 '22 17:09

Joost van der Laan


Put this before the server section in nginx config file as shown bellow:

. . . # Expires map map $sent_http_content_type $expires {     default                    off;     text/html                  epoch;     text/css                   max;     application/javascript     max;     ~image/                    max; }  server {    listen 80 default_server;    listen [::]:80 default_server;     expires $expires; . . . 

the ~image will handle all kind of images ( instead of hardcoding them )

for further informations on how to handle nginx caching see link

like image 36
rachid el kedmiri Avatar answered Sep 22 '22 17:09

rachid el kedmiri