Redirect Example
When address: /logo was visited, redirect to /img.jpg
location = /logo {
return 307 /img.jpg;
}
Rewrite Example
Give "Hello John" if /user/john is visited, otherwise give "Hello User", all of this without redirecting from /user/*.
rewrite ^/user/(\w+) /greet/$1;
location /greet {
return 200 "Hello User";
}
location /greet/john {
return 200 "Hello John";
}
Try Files Example
Assuming that /img.jpg exists on root, that image would be loaded first, if not, the /greet path would be. Nginx' try_files works similar to rewrite.
try_files $uri /img.jpg /greet /custom_404;
location /greet {
return 200 "Hello User";
}
Custom 404
Using try_files, for instance when /does-not-exists is visited, it will then point to @custom_404
in which you can handle the error yourself.
try_files $uri /greet @custom_404;
location @custom_404 {
return 404 "Sorry, we cannot find the page you are looking for";
}