Sure!
In NGINX you can “branch-off” configuration logic based on the request method (GET, POST, PUT, …) by using the map directive.
Below is a minimal, self-contained example that shows the pattern:
nginx
# 1) Put this in http{} context (typically in conf.d/ or nginx.conf)
map $request_method $is_post {
# $request_method is the built-in variable that contains the verb
default 0; # anything not matched below
POST 1; # exact, case-sensitive match for POST
# If you also want case-insensitive, uncomment the next line:
# ~*POST 1;
}
# 2) Then you can use $is_post anywhere you like
server {
listen 80;
server_name example.com;
location /api {
# Example: bypass cache for POST, cache GET
proxy_cache_bypass $is_post;
proxy_no_cache $is_post;
proxy_pass http://backend;
}
location /test {
# Example: send to a different upstream if it's POST
if ($is_post) {
proxy_pass http://write-backend;
}
if (!$is_post) {
proxy_pass http://read-backend;
}
}
location /debug {
# Just show which branch was taken
return 200 "method=$request_method; is_post=$is_post\n";
}
}
How it works
map executes once per request (before the server/location phase).
The left-hand side is the source variable you want to examine ($request_method).
Each line on the right creates a value for a new variable ($is_post) when a key matches.
default is the fallback when nothing else matches.