nginxwebserverstatic-filesserving

Nginx: serving static files by URL path


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.


Solution

  • 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.