Question

error adding second virtual host on nginx

  • Posted July 23, 2013

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:

Subscribe
Share

Same problem here. No solution yet?


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

add line

server_names_hash_bucket_size 64;

before

server {

This comment has been deleted

<blockquote>when i want add more virtualhost nginx start fail</blockquote>What error does it output? Check its error logs and see which virtualhost is causing issues.

i have same problem , <br> <br>i created separate file for each virtualhost , domain.com and domain2.com in here /etc/nginx/sites-available <br> <br>and i linked them to /etc/nginx/sites-enabled <br> <br>after adding frist , when i want add more virtualhost nginx start fail

I recommend creating a separate file for each virtualhost. As long as you have “include sites-enabled/*;” in nginx.conf, you should be fine.

The <a href=“http://wiki.nginx.org/Configuration”>Nginx wiki</a> provides some configuration examples. According to one of these <a href=“http://wiki.nginx.org/ServerBlockExample”>examples</a>, both server blocks go one file, e.g.<br /> <br><pre> <br>http { <br> index index.html; <br><br /> <br> server { <br> server_name www.domain1.com; <br> access_log logs/domain1.access.log main; <br> <br> root /var/www/domain1.com/htdocs; <br> } <br><br /> <br> server { <br> server_name www.domain2.com; <br> access_log logs/domain2.access.log main; <br><br /> <br> root /var/www/domain2.com/htdocs; <br> } <br>} <br></pre>

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. <br> <br>However, another tutorial i read (not on DO) says that … <br> <br>“When adding new virtual hosts in your nginx configuration file”, <br> <br>Therefore, this seems to suggest that the virtual hosts go in one file. <br> <br>Which is it? <br> <br>

Hi Pablo, <br> <br>1) yes, I created a different root for each virtual host. The first is <br> <br> root /var/www/example.com/public_html; <br> <br>with the server name set to the ip address <br> server_name 192.241.133.226; <br> <br>the second is <br> <br>root /var/www/domain.com/public_html; <br> index index.html index.htm; <br> <br>with the server name set to my domain name <br> <br> server_name domain.com; <br> <br> <br>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? <br> <br> <br>3. If I’m runnining into this issue with only two virtual hosts, will i have more problems with many virtual hosts set up? <br>

Did you create a different root directory for each virtual host? In your <br><code>/etc/nginx/nginx.conf</code> file, do you have a line that reads <code>server_names_hash_bucket_size</code>?