The Nginx SSI module also offers the possibility to work with variables. Displaying a variable (in other words, inserting the variable value into the final HTML source code) can be done with the echo command:
<!--# echo var="variable_name" -->
The command accepts the following three parameters:
- var: The name of the variable you want to display, for example, REMOTE_ADDR to display the IP address of the client.
- default: A string to be displayed in case the variable is empty. If you don't specify this parameter, the output is (none).
- encoding: Encoding method for the string. The accepted values are none (no particular encoding), url (encode text like a URL: a blank space becomes %20, and so on) and entity (uses HTML entities: & becomes &).
You may also affect your own variables with the set command:
<!--# set var="my_variable" value="your value here" -->
The value parameter is itself parsed by the engine; as a result, you are allowed to make use of existing variables:
<!--# echo var="MY_VARIABLE" --> <!--# set var="MY_VARIABLE" value="hello" --> <!--# echo var="MY_VARIABLE" --> <!--# set var="MY_VARIABLE" value="$MY_VARIABLE there" --> <!--# echo var="MY_VARIABLE" -->
Here is the code that Nginx outputs for each of the three echo commands from the preceding example:
(none) hello hello there