Welcome! Log In Create A New Profile

Advanced

Nginx variable scope

Posted by w_boba 
Nginx variable scope
September 03, 2013 11:51AM
Hello, All !

I am wondering, if it is possible to limit visibility of a variable in Nginx ? For example if some variable was set inside "server" block, it is visible only within this "server" and not outside. I've done some reading and it looks like variables once defined somewhere in config are visible to all other parts of the Nginx configuration...

Maybe there is some other way to create template-based configuration in Nginx, where I can "include" the same piece of configuration from external file and set certain parameters via variables, like "root" or "proxy_pass", adding minimum number lines to the main config ?

Or, in other words, will the config below work as expected?

main.conf:

upstream some_upstream {
server some.upstream.com:8000;
server some.upstream1.com:8001 backup; }

upstream some_other_upstream {
server some.other.upstream.com:8002;
server some.other.upstream1.com:8003 backup; }

server {
set $var_servername somename.com;
set $var_listenport 12345;
set $var_root /web/somename;
set $var_upstream some_upstream;
include config.inc; }

server {
set $var_servername someothername.com;
set $var_listenport 12346;
set $var_root /web/someothername;
set $var_upstream some_other_upstream;
include config.inc; }

config.inc:

listen $var_listenport;
server_name $var_servername;

location / {
root $var_root; }

location /proxied {
proxy_pass $var_upstream; }
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 149
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready