perl_set

The `perl_set` directive allows setting a variable using Perl code within an NGINX configuration.

Syntaxperl_set $variable_name 'perl_code';
Defaultnone
Contexthttp
Arguments2

Description

The perl_set directive is designed to allow users to define NGINX variables based on arbitrary Perl expressions. This directive is particularly useful in situations where values need to be computed dynamically. The directive takes two parameters: the first is the variable name, and the second is the Perl expression that will be used to set the value of this variable.

When NGINX encounters the perl_set directive, it runs the specified Perl code during the request processing phase, allowing it to determine the value of the specified variable right before it is used. This makes perl_set suitable for creating complex variable values based on the context of the HTTP request, such as headers, connection details, or dynamically generated content. It's important to have the Perl module enabled in NGINX to use this directive, and proper testing is advised before deploying in a production environment due to potential performance impacts.

Parameters of perl_set include the variable name prefixed with a $, and the Perl expression wrapped in quotes. For instance, if you want to compute a variable based on some request parameter, it requires proper syntax and a functional Perl script that interacts with NGINX's context properly. The behavior of these variables is determined by the context in which they are defined, which can impact scope and availability throughout different phases of request processing.

Config Example

perl_set $dynamic_value 'sub { return "Hello, World!"; }';

Ensure the Perl module is compiled and loaded into NGINX before using this directive.

Incorrect syntax in the Perl expression might lead to configuration errors or runtime failures.

Performance may be impacted if overused or improperly written, as the Perl interpreter will evaluate the expression on each request.

← Back to all directives