What is the variable scope in a nginx config file?
Is everything declared inside a server block only usable inside that block?
Can variables declared in the http block be used inside each server block?
Thanx.
2 Replies
include /etc/nginx/vhosts/yoursite.com
include /etc/nginx/sites-enabled/*
The first line specifically tell nginx specifically what vhost file to load. The second tells it to load everything within /etc/nginx/sites-enabled.
-Generally speaking, anything within the http block is included in your server block. So if you have something like:
http {
index index.htm index.html;
server {
listen 80;
server yoursite.com;
root /srv/www/yoursite.com/html;
}
server {
listen 80;
server another.yoursite.com;
root /srv/www/another.yoursite.com/html;
}
}
This will tell nginx that the index line for both of your server blocks will be the same.
But sometimes you'll come across slightly unintuitive behavior. For example, I discovered in this thread
So, push as much as you can to upper-level blocks without breaking things, and make liberal use of includes to prevent repeating yourself in other cases. FastCGI is a case where you pretty much need to use includes, which is why most default configurations already do so.
This official wiki page