4

Is there any way of serving static files by only some URL path? For example, next URL pattern http://host/static/*.png has /static/ substring (path), and Nginx will serve any statics from there.

In the web server documentation I found an example:

location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|js)$ { ...

and defined my Nginx config like that:

    location / {
        try_files $uri $uri/ /index.html;
    }

    location /apib {
         #some proxy_pass
    }

    location /apim {
         #some proxy_pass
    }

    location /api {
         #some proxy_pass
    }

I try to add additional location for */static/*.* with root dir /var/www/some_statics.

akrisanov
  • 3,212
  • 6
  • 33
  • 56

2 Answers2

4
location ~* ^/static/.+\.(png|whatever-else)$ {
    alias /var/www/some_static;
    expires 24h;
}
location / {
    # regular rules
}

Hand written, may contain mistakes.

If you want to extend the rules to match anything/something/static/*.png just remove the ^ in the patten.

msg7086
  • 461
  • 2
  • 11
  • Nginx send index page (root) from server section in config. – akrisanov Jan 14 '14 at 04:34
  • @akrisanov That's right. They are inherited from upper level config unless specified by `root` or `alias` inside the block. – msg7086 Jan 14 '14 at 04:40
  • http://d.pr/i/ViNT I mean, your solution doesn't work. My full config now: https://gist.github.com/akrisanov/8413535 – akrisanov Jan 14 '14 at 05:25
  • Did you reload the configuration / restart the nginx? and you may also try `location /static/ { alias /var/www/some_static; }` or read [this](/questions/10631933/nginx-static-file-serving-confusion-with-root-alias) – msg7086 Jan 14 '14 at 05:38
0

location ^~ /static/ {
    alias /var/www/some_static;
}
Charles Wang
  • 267
  • 1
  • 4
  • 9