$request_method
The $request_method variable in NGINX contains the HTTP method used for the current request. — NGINX Core (HTTP)
Description
The $request_method variable is part of the core NGINX variables and is used to capture the method of the HTTP request being processed. This variable will contain the method verb, such as GET, POST, PUT, DELETE, or HEAD, which indicates the action that the client intends to perform on the given resource. It is crucial in scenarios where specific handling of request types is required, for instance, distinguishing between GET and POST for processing data differently. This variable is set during the request processing phase when NGINX parses the incoming HTTP request headers. It is often used in conditionals or in logging configurations to capture details about the request for better diagnostics or to apply different rules based on the request type. For example, some server configurations might only allow POST requests for certain endpoints, and using the $request_method variable can help enforce this: ``` if ($request_method !~ ^(GET|POST)$) { return 405; } ``` This example restricts access to only GET and POST methods, returning a 405 Method Not Allowed status for all others.
Config Example
location /example {
if ($request_method = POST) {
# Handle POST requests
}
if ($request_method = GET) {
# Handle GET requests
}
}Subsystem
httpCacheable
NoContexts
http, server, location, ifEnsure proper casing: methods are case-sensitive, so 'get' and 'GET' are treated differently.
Avoid using $request_method in situations where its value changes mid-request, as it reflects the value at the start of the request.