set_sha1

The set_sha1 directive computes the SHA-1 hash of a specified input string in NGINX.

Syntaxset_sha1 $destination_variable $input_variable;
Defaultnone
Contexthttp, server, location, if in server, if in location
Arguments1-2

Description

The set_sha1 directive is part of the NGINX Set-Misc module and provides an easy way to compute the SHA-1 hash of a given string. This directive can take one or two arguments: the first argument specifies the variable to which the resulting SHA-1 hash will be assigned, and the optional second argument is the input string whose hash needs to be calculated. When the second argument is not provided, the module computes the SHA-1 hash of the value of the first variable specified.

SHA-1 is a cryptographic hash function that produces a 160-bit hash value, typically rendered as a hexadecimal number that is 40 digits long. In using this directive, the resulting hash can be utilized in various NGINX configurations, such as for creating unique identifiers or for securing raw data. The behavior of this directive is determined by the availability of the input variable; if the variable does not exist or is empty, the result may also be empty. Understanding how to reference and manage variables is essential for effective use of this directive.

Config Example

location /hash {
    set $input_string "Hello, World!";
    set_sha1 $hashed_value $input_string;
    return 200 "SHA1: $hashed_value";
}

Using an undefined variable for input may result in an unexpected empty hash.

Ensure the input variable contains a valid string before invoking set_sha1 to avoid empty results.

← Back to all directives