Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I have a question concerning the setup of nginx on a server handling different virtual hosts. I am now a bit confused where the root directive should be. I have seen the following two possibilities.

Variant A

server {
listen          80;
server_name     www.site.com;
access_log      /var/log/nginx/www.site.com.access.log main;
root            /var/www/$server_name/htdocs;

location / {
   index index.html;
   }
}

or the other one

Variant B

server {
listen          80;
server_name     www.site.com;
access_log      /var/log/nginx/www.site.com.access.log main;

location / {
   root         /var/www/$server_name/htdocs;
   index        index.html;
   }
}

I am a bit confused if the two variants are equal. For the different vhosts I planned to have individual .conf files with different server_name and root directives in /etc/nginx/sites-available. To activate a vhost I would have a symbolic link to the conf file in /etc/nginx/sites-enabled. Or would you store the configuration file in /var/www/$server_name to have it "close" to the www data?

So to sum up, I have two questions:
1. Which of the two variants of the nginx.conf file is better for the multiple vhost setup and why?
2. Where would you store the configuration files for each vhost?

Thanks.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Variant A is the proper way to do it. Nginx will only ever use one location block so if you define root in a location it will only be available in that location. This almost always leads to duplication of either root directives of file paths, neither of which is good.

If you define it in the server block it is always inherited by the location blocks so it will always be available in the $document_root variable, thus avoiding the duplication of file paths.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.