A
A
Al2018-09-09 01:16:06
Nginx
Al, 2018-09-09 01:16:06

How to make NGINX work with multiple directories of static files?

There are two directories /test/app/dist/images and /test/vendor/static .
Both can contain static files. If the image URL contains the "/static" segment, then nginx should look for such an image in the /test/vendor/static directory , and if the URL just ends with .jpg, .png, etc., then nginx should look for such a file in the /test/ directory app/dist/images .
I wrote this config:

server {
  listen *:12901;
  server_name test.lan;	

  location / { 
    proxy_pass http://lan_nodes;
    //....
  }
  
  
  location ~* /static/*\.(jpg|jpeg|gif|png|webp|ico|css|bmp|swf|js|html|txt|ejs|json) {
    root /test/app/dist/images;
    expires 10d;
  }
  
  
  location ~* \.(jpg|jpeg|gif|png|webp|ico|css|bmp|swf|js|html|txt|ejs|json)$ {
    root /test/vendor/static;
    expires 10d;
  }

}

But when I try to access the address "test.lan/static/img/test/43a398b30dd39fc56ce97898758b4776.png" I get a 404 error. Tell me how to properly configure nginx for this case?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2018-09-09
@Sanu0074

server {
  listen *:12901;
  server_name test.lan;	

  location / { 
    proxy_pass http://lan_nodes;
    //....
  }
  
  
  location ~ ^/static/.*\.(jpg|jpeg|gif|png|webp|ico|css|bmp|swf|js|html|txt|ejs|json)$ {
    root /test/vendor;
    expires 10d;
  }
    
    
  location ~ \.(jpg|jpeg|gif|png|webp|ico|css|bmp|swf|js|html|txt|ejs|json)$ {
    root /test/app/dist/images;
    expires 10d;
  }

}

P
Pavel Selivanov, 2018-09-09
@selivanov_pavel

s/root/location/g

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question