I've got a working nginx rewrite for a location, but in the case of a specific request I want to rewrite to a different location.
location /api {
if (!-e $request_filename){
rewrite "^/api/.{2}\.zip" /api/general.zip last; break;
rewrite ^/api/(.*)$ /api/router.php?rest_data=$1 last; break;
}
}
So if someone requests /api/uk.zip then it downloads fine, but /api/fr.zip doesn't exist, so it should redirect to /api/general.zip
I've tried 2 location blocks, I get a duplicate location error, and I've tried using 2 if statements, but the second one gets ignored.
If the request is for something else, like /api/events then it should forward to the router page.
If-Is-Evil, avoid using if. You're a starter on nginx and maybe you didn't saw that before (check this guide for a good start). But really, avoid if. And the solution is usually try_files instead.
In your case a unique
try_file
cannot be used, because you need to apply a special treatment if the zip file is a two character one. But with two locations it should work. Something like that (untested):