SoFunction
Updated on 2025-03-03

Use of if judgment in Nginx configuration

When the rewrite rule of rewrite cannot meet the needs, for example, it is necessary to determine whether the file does not exist or when the path contains xx, etc., then if

if syntax

if (expression) {
    ...
}

Expression syntax:

  • When an expression is just a variable, if the value is empty or any string starting with 0 will be treated as false
  • When directly comparing variables and content, use = or !=
  • -f and !-f are used to determine whether a file exists
  • -d and !-d are used to determine whether a directory exists
  • -e and!-e are used to determine whether a file or directory exists
  • -x and !-x are used to determine whether the file can be executable

In order to configure the conditional judgment of if, the built-in global variable in nginx is required here

$args                   This variable is equal to the parameter in the request line, the same as $query_string
$content_length     Content-length field in the request header.
$content_type       Content-Type field in the request header.
$document_root     The value specified in the root directive at the current request.
$host                   Request the host header field, otherwise it is the server name.
$http_user_agent    Client agent information
$http_cookie         Client cookie information
$limit_rate         This variable can limit the connection rate.
$request_method     The action requested by the client is usually GET or POST.
$remote_addr         The IP address of the client.
$remote_port         The client's port.
$remote_user         Username that has been verified by Auth Basic Module.
$request_filename   The file path of the currently requested, generated by the root or alias directive and the URI request.
$scheme           HTTP method (such as http, https).
$server_protocol    The protocol used by the request is usually HTTP/1.0 or HTTP/1.1.
$server_addr         Server address, this value can be determined after completing a system call.
$server_name       Server name.
$server_port       The port number that requests to reach the server.
$request_uri       The original URI containing the request parameters, not the host name, such as: "/foo/?arg=baz".
$uri                       Current URI without request parameters, $uri does not contain the host name, such as "/foo/".
$document_uri       Same as $uri.

Give an example

1. If the file does not exist, return 400.

if (!-f $request_filename) {
    return 400;
}

2. If host is not, then 301 is in

if ( $host != '' ){
    rewrite ^/(.*)$ /$1 permanent;
}

3. If the request type is not POST, return 405

if ($request_method = POST) {
    return 405;
}

4. If there is any in the parametersa=1Then 301 to the specified domain name

if ($args ~ a=1) {
    rewrite ^ / permanent;
}

5. In a certain scenario, it can be used in combination with location rules, such as:

# When accessing /location = / {
    # Set the default value to xiaowu    set $name xiaowu;
    # If name=xx is included in the parameter, use this value    if ($args ~* name=(\w+?)(&|$)) {
        set $name $1;
    }
    # 301
    rewrite ^ /$ permanent;
}

The above indicates:
/ => /
/?name=ok => /?name=ok

This is the end of this article about the use of if judgment in Nginx configuration. For more related content on Nginx if judgment, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!