Attempting to emulate Apache MultiViews with Nginx try_files
Posted
by
Samuel Bierwagen
on Server Fault
See other posts from Server Fault
or by Samuel Bierwagen
Published on 2012-04-06T23:05:07Z
Indexed on
2012/04/06
23:32 UTC
Read the original article
Hit count: 243
nginx
|url-rewriting
I want a request to http://example.com/foobar
to return http://example.com/foobar.jpg
. (Or .gif, .html, .whatever)
This is trivial to do with Apache MultiViews, and it seems like it would be equally easy in Nginx. This question seems to imply that it'd be easy as try_files $uri $uri/ index.php;
in the location block, but that doesn't work.
try_files $uri $uri/ =404;
doesn't work, nor does try_files $uri =404;
or try_files $uri.* =404;
Moving it between my location / {
block and the regexp which matches images has no effect.
Crucially, try_files $uri.jpg =404;
does work, but only for .jpg files, and it throws a configuration error if I use more than one try_files rule in a location block!
The current server {
block:
server {
listen 80;
server_name example.org www.example.org;
access_log /var/log/nginx/vhosts.access.log;
root /srv/www/vhosts/example;
location / {
root /srv/www/vhosts/example;
}
location ~* \.(?:ico|css|js|gif|jpe?g|es|png)$ {
expires max;
add_header Cache-Control public;
try_files $uri =404;
}
}
Nginx version is 1.1.14.
© Server Fault or respective owner