Quantcast
Channel: Nginx Forum - How to...
Viewing all articles
Browse latest Browse all 4759

Nginx variable scope

$
0
0
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; }

Viewing all articles
Browse latest Browse all 4759

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>