I have a location in nginx where GET and POST requests should go to different named locations. But this code doesn't work:
if ( $request_method = GET ) {
try_files /NON-EXISTENT @named_location_get
}
if ( $request_method = POST ) {
try_files /NON-EXISTENT @named_location_post
}
Because try_files
is not allowed inside if
.
Is there any other way to redirect request to named location, that would work inside if
?
I don't know where this is documented, but I have seen configurations where a variable is used following the
@
symbol, to make the namedlocation
conditional.Use this with a
map
block and you can eliminate theif
blocks.I have tested this example: