nginx FAQ Is there a proper way to use nginx variables to make sections of the configuration shorter, using them as macros for making parts of configuration work as templates? ) saying (bold is mine):
Q: Is there a proper way to use nginx variables to make sections of the configuration shorter, using them as macros for making parts of configuration work as templates?
A: Variables should not be used as template macros. Variables are evaluated in the run-time during the processing of each request, so they are rather costly compared to plain static configuration. Using variables to store static strings is also a bad idea. Instead, a macro expansion and "include" directives should be used to generate configs more easily and it can be done with the external tools, e.g. sed + make or any other common template mechanism.
For example instead of one super long add_header Content-Security-Policy
for better readability I am using:
set $CSP "default-src 'none'";
set $CSP "${CSP}; connect-src 'self'";
set $CSP "${CSP}; script-src 'self' https://*.domain.org 'unsafe-inline' 'unsafe-eval'";
set $CSP "${CSP}; style-src 'self' https://*.domain.org 'unsafe-inline'";
set $CSP "${CSP}; img-src 'self' data: https://*.domain.org";
set $CSP "${CSP}; font-src 'self' https://*.domain.org";
## CSP closing colon.
set $CSP "${CSP};";
add_header Content-Security-Policy "$CSP";
How much impact on nginx performance would that use of variable have appropriately? Has there been any performance testing / research on that subject?