error adding second virtual host on nginx

July 23, 2013 12.8k views
Using this tutorial https://www.digitalocean.com/community/articles/how-to-set-up-nginx-virtual-hosts-server-blocks-on-ubuntu-12-04-lts--3, I'm trying to set up virtual hosts with nginx to have multiple static sites on Digital Ocean. I created the first virtual host using my IP address without a problem i.e. when I went to my ip address, I got the hello world message "Success, you have set up virtual host" created in the tutorial. At the bottom of the tutorial, it says to go through the same process again to create more virtual hosts (i.e. to host other domains). Therefore, I went through the process again to host a domain whose nameservers I have pointed to digital ocean, however, I found certain steps confusing when trying to duplicate the process and when I tried to restart nginx I got this error sudo service nginx restart Restarting nginx: nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32 nginx: configuration file /etc/nginx/nginx.conf test failed It started getting confusing the second time around when I got to step four, "Create the New Virtual Host File". I did this (replacing example.com) with my domain name sudo cp /etc/nginx/sites-available/default /etc/nginx/sites-available/example.com However, the instructions make clear that I have to create a new virtual host file each time, so I think I did that correctly. At Step 5, I did this (replacing example.com with my domain name) sudo nano /etc/nginx/sites-available/example.com and then added the information the tutorial specified into the file. Then I did this (replacing example.com with my domain name) sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/example.com Then I didn't do the next step because I already removed the default when I created the first virtual host using my ip address sudo rm /etc/nginx/sites-enabled/default Afterwards I restarted nginx and got the error I referred to above Restarting nginx: nginx: [emerg] could not build the server_names_hash, you should increase server_names_hash_bucket_size: 32 nginx: configuration file /etc/nginx/nginx.conf test failed Now when I visit the ip address (which worked before) I'm getting this message ERROR The requested URL could not be retrieved While trying to retrieve the URL: http://192.241.133.226/ The following error was encountered: Connection to 192.241.133.226 Failed The system returned:
1 comment
10 Answers

add line

server_names_hash_bucket_size 64;

before

server {
Did you create a different root directory for each virtual host? In your
/etc/nginx/nginx.conf file, do you have a line that reads server_names_hash_bucket_size?
Hi Pablo,

1) yes, I created a different root for each virtual host. The first is

root /var/www/example.com/public_html;

with the server name set to the ip address
server_name 192.241.133.226;

the second is

root /var/www/domain.com/public_html;
index index.html index.htm;

with the server name set to my domain name

server_name domain.com;


2. I do have a line that read `server_names_hash_bucket_size` in the nginx.conf file. It is commented out. After doing a google search I discovered that if a domain name is long, then this number should be raised from the default to 64. However, my domain name is only 8 letters. do you think that's the issue?


3. If I'm runnining into this issue with only two virtual hosts, will i have more problems with many virtual hosts set up?
The tutorial on Digital Ocean isn't entirely clear to me. Step 4 seems to suggest that I create a new virtual host file each time I create a new virtual host. Therefore, in sites-available folder, I have in addition to the default site available file, also files for each of the other sites. So three files.

However, another tutorial i read (not on DO) says that ....

"When adding new virtual hosts in your nginx configuration file",

Therefore, this seems to suggest that the virtual hosts go in one file.

Which is it?

The Nginx wiki provides some configuration examples. According to one of these examples, both server blocks go one file, e.g.


http {
index index.html;


server {
server_name www.domain1.com;
access_log logs/domain1.access.log main;

root /var/www/domain1.com/htdocs;
}


server {
server_name www.domain2.com;
access_log logs/domain2.access.log main;


root /var/www/domain2.com/htdocs;
}
}
I recommend creating a separate file for each virtualhost. As long as you have "include sites-enabled/*;" in nginx.conf, you should be fine.
i have same problem ,

i created separate file for each virtualhost , domain.com and domain2.com in here /etc/nginx/sites-available

and i linked them to /etc/nginx/sites-enabled

after adding frist , when i want add more virtualhost nginx start fail
when i want add more virtualhost nginx start fail
What error does it output? Check its error logs and see which virtualhost is causing issues.
Have another answer? Share your knowledge.