Search code examples
jsonnginxblock

Deny request to all json files from particular path in Nginx


Deny request to all json files from particular path in Nginx.

We have to block all json files,which is served from /home/assests/js/.

location /home/assets/js/mxgraph/package1.json {
    deny all;
}

location /home/assets/js/mxgraph/package2.json {
    deny all;
}

location /home/assets/js/mxgraph/package3.json {
    deny all;
}

location /home/assets/js/mxgraph/package4.json {
    deny all;
}

We are able to block all above json files,but how we can combine these blocks into one,saying block all *.json files need to deny from those path.


Solution

  • To match all URIs ending with .json you would need to use a regular expression.

    For example:

    location ~ ^/home/assets/js/.*\.json$ { deny all; }
    

    Regular expression location statements are evaluated in order until a matching rule is found, so this statement should be placed above any conflicting regular expressions. See this document for details.