Linode Forum
Linode Community Forums
 FAQFAQ    SearchSearch    MembersMembers      Register Register 
 LoginLogin [ Anonymous ] 
Post new topic  Reply to topic
Author Message
PostPosted: Sat Oct 01, 2011 1:42 pm 
Offline
Senior Member

Joined: Wed Dec 01, 2010 10:39 am
Posts: 71
Website: http://www.pannix.net
Location: Belgium
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.


Top
   
 Post subject:
PostPosted: Sat Oct 01, 2011 3:01 pm 
Offline
Senior Member
User avatar

Joined: Thu Jun 16, 2011 8:24 am
Posts: 412
Location: Cyberspace
-The stuff inside a server block cannot be accessed within another server block. Each server block counts as it's own vhost. You can place each server block within it's own file if you wish, then within the http block, you can load it via an include line, such as:
Code:
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:
Code:
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.

_________________
Kris the Piki Geeker


Top
   
 Post subject:
PostPosted: Sat Oct 01, 2011 5:27 pm 
Offline
Senior Member

Joined: Fri May 02, 2008 8:44 pm
Posts: 1121
Most settings declared in a parent block (e.g. logging, compression, etc.) will be inherited by all child blocks unless specifically overridden.

But sometimes you'll come across slightly unintuitive behavior. For example, I discovered in this thread that none of the FastCGI parameters defined in a parent block will be inherited by a child block if the child block contains any parameters of its own. So if you want to use different FastCGI parameters in different blocks, you need to redefine all of them every single time.

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 contains a number of other useful tips on reducing redundancy and improving performance. Some of the configuration directives, however, might not be available on older versions of nginx that come with Ubuntu LTS releases.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
RSS

Powered by phpBB® Forum Software © phpBB Group