0

How to configure NGINX virtual host (vhost) to serve the same image (/images/game-board.png) for all image requests that start with a specific string like /images/game-board-?

Example:

For these image requests:

  • /images/game-board-is89sj.png
  • /images/game-board-32jsds.png
  • /images/game-board-abcx7v.png

Serve this file:

  • /images/game-board.png

I've tried the below location block configurations. But they are not working.

location ~ ^/images/game-board-[^/]+\.png$ { alias /var/www/html/images/game-board.png; } 
location ~ ^/images/game-board-[^/]+\.png$ { try_files $uri /images/game-board.png; } 

Update:

Ok, I found the solution. The issue was with the priority of the location blocks. Moving the alias location block above the other location blocks (which had catch-all image rules) worked for me.

1

1 Answer 1

1

You can use a nested location to apply your catch-all image rules to the game-board.png file as well:

location ~ \.(gif|jpe?g|png)$ { location ~ ^/images/game-board-[^/]+\.png$ { alias /var/www/html/images/game-board.png; } # caching policy, logging, etc. } 

This should also speed up your configuration a little, eliminating one extra libpcre library call for non-image requests (a request URI won't be matched against the ^/images/game-board-[^/]+\.png$ regex pattern if matching against the \.(gif|jpe?g|png)$ regex pattern fails).

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.