set_encode_base64url

The `set_encode_base64url` directive encodes data into a URL-safe Base64 format.

Syntaxset_encode_base64url $input_variable [$output_variable];
Defaultnone
Contexthttp, server, location, if in server, if in location
Arguments1-2

Description

The set_encode_base64url directive is part of the NGINX Set-Misc module and is designed to convert arbitrary data into a Base64 URL-safe format. This is particularly useful for embedding binary data in URLs or in HTTP headers without causing issues with character encoding. The directive takes one or two arguments: the first is the input variable to encode, and the second, which is optional, specifies the output variable where the encoded result will be stored.

When using set_encode_base64url, the process involves modifying the standard Base64 encoding by replacing '+' with '-' and '/' with '_'. This modification results in a Base64 encoded string that is safe to use in URLs without the need for further encoding (e.g., URL encoding). The padding character '=' may also be omitted in this format. Therefore, users can expect a clean, URL-friendly output that can be directly used in web contexts. This is particularly useful for generating tokens or identifiers for use in APIs, sessions, or other web applications that require safe transmission of binary data.

The directive operates in various contexts, including http, server, and location, providing flexibility in its application across different segments of an NGINX configuration. It can be combined with other NGINX directives for complex manipulations of requests and responses, making it a powerful tool in the NGINX module repertoire.

Config Example

location /example {
    set $data "example data";
    set_encode_base64url $data $encoded_data;
}
 
# In this case, $encoded_data will hold the URL-safe Base64 encoded version of 'example data'.

Ensure that the input variable contains a valid value to avoid encoding errors.

Be aware that the output variable will be overwritten; always use a unique output variable name if you need to store multiple encodings.

If no second output variable is provided, the directive will overwrite the input variable.

← Back to all directives