# How to add virtualhosts in nginx?

**URL:** <https://www.ruby-forum.com/t/how-to-add-virtualhosts-in-nginx/195361>\
**Category:** NGINX\
**Created:** [September 22, 2010, 7:46am UTC](https://www.ruby-forum.com/t/how-to-add-virtualhosts-in-nginx/195361 "2010-09-22T07:46:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![marcelo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@marcelo](https://www.ruby-forum.com/u/marcelo)\
**Post date:** [September 22, 2010, 7:46am UTC](https://www.ruby-forum.com/t/how-to-add-virtualhosts-in-nginx/195361/1 "2010-09-22T07:46:10Z")

</div>

Hello All,

Following the excellent guide posted in cyberciti.biz  
([http://www.cyberciti.biz/faq/rhel-fedora-install-configure-nginx-php5/](http://www.cyberciti.biz/faq/rhel-fedora-install-configure-nginx-php5/))  
I istalled nginx in centos 5.5 successfully, but I fail to configure  
VirtualHosts.

I tried different combinations but it does not work.

Could someone please help me?

I just need an example of a working configuration.

I really appreciate it.

Best Regards,  
Marcelo .-

---

<div class="post-metadata">

**Author:** ![marcelo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@marcelo](https://www.ruby-forum.com/u/marcelo)\
**Post date:** [September 22, 2010, 8:00am UTC](https://www.ruby-forum.com/t/how-to-add-virtualhosts-in-nginx/195361/2 "2010-09-22T08:00:48Z")

</div>

On 22 Set 2010 06h45 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I just need an example of a working configuration.

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

— appa

---

<div class="post-metadata">

**Author:** ![marcelo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@marcelo](https://www.ruby-forum.com/u/marcelo)\
**Post date:** [September 22, 2010, 8:21am UTC](https://www.ruby-forum.com/t/how-to-add-virtualhosts-in-nginx/195361/3 "2010-09-22T08:21:52Z")

</div>

Thank you António,

I saw this example before.

I saw many examples, but does not work.

I get always nginx default html page or 404…

2010/9/22, António P. P. Almeida [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

–  
¿Por qué sufrimos? [http://www.youtube.comwatch?v=FK53c2jW2VU](http://www.youtube.comwatch?v=FK53c2jW2VU)

---

<div class="post-metadata">

**Author:** ![marcelo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@marcelo](https://www.ruby-forum.com/u/marcelo)\
**Post date:** [September 22, 2010, 8:34pm UTC](https://www.ruby-forum.com/t/how-to-add-virtualhosts-in-nginx/195361/4 "2010-09-22T20:34:22Z")

</div>

Hola AntÃ³nio and everybody,

Thanks for the help, I really apreciate it!

The VirtualHosts configuration file is located in:  
/etc/nginx/conf.d/virtual.conf

example:

http {

### vhost1 - [www.prueba.com](http://www.prueba.com)

server {  
listen 80;  
server\_name [www.prueba.com](http://www.prueba.com);  
access\_log logs/prueba.access.log main;

index index.html;  
root /var/www/virtuales/prueba.com/htdocs;  
}

2010/09/22 13:21:22 [info] 24429#0: the configuration file  
/etc/nginx/nginx.conf syntax is ok  
2010/09/22 13:21:22 [emerg] 24429#0: open()  
“/usr/share/nginx/logs/prueba.access.log” failed (2: No such file or  
directory)  
2010/09/22 13:21:22 [emerg] 24429#0: the configuration file  
/etc/nginx/nginx.conf test failed

Â¿the logs file must be created maually everytime I create a domain?

I remove the “access\_log logs/prueba.access.log main;” line and  
works,  
but when I browse “[www.prueba.com](http://www.prueba.com)” I get nginx default html page, and  
when I  
browse “[Apache2 Ubuntu Default Page: It works](http://www.prueba.com/index.html)” the index.html placed in  
/var/www/virtuales/prueba.com/html is showed.

is possible show index.html directly when “[prueba.com](http://prueba.com)” or  
“[www.prueba.com](http://www.prueba.com)”  
is entered in the browser?

Maybe some redirection in the VirtualHost configuration file

what about the error logs files?

Thank you very much!

2010/9/22 AntÃ³nio P. P. Almeida [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![marcelo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@marcelo](https://www.ruby-forum.com/u/marcelo)\
**Post date:** [September 22, 2010, 9:15am UTC](https://www.ruby-forum.com/t/how-to-add-virtualhosts-in-nginx/195361/5 "2010-09-22T09:15:09Z")

</div>

On 22 Set 2010 07h21 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you AntÃ³nio,
> 
> I saw this example before.
> 
> I saw many examples, but does not work.
> 
> I get always nginx default html page or 404…

Then there’s something wrong with your configuration. Perhaps the root  
directive or a/the location block. This is guessing. It’s much better  
if you place the config here, so that we can help you debug it.

— appa

---

<div class="post-metadata">

**Author:** ![marcelo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@marcelo](https://www.ruby-forum.com/u/marcelo)\
**Post date:** [September 22, 2010, 9:40pm UTC](https://www.ruby-forum.com/t/how-to-add-virtualhosts-in-nginx/195361/6 "2010-09-22T21:40:32Z")

</div>

On 22 Set 2010 19h32 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ### vhost1 - [www.prueba.com](http://www.prueba.com)
> 
> 2010/09/22 13:21:22 [info] 24429#0: the configuration file  
> /etc/nginx/nginx.conf syntax is ok  
> 2010/09/22 13:21:22 [emerg] 24429#0: open()  
> “/usr/share/nginx/logs/prueba.access.log” failed (2: No such file or  
> directory)  
> 2010/09/22 13:21:22 [emerg] 24429#0: the configuration file  
> /etc/nginx/nginx.conf test failed
> 
> Â¿the logs file must be created maually everytime I create a domain?

No. You can specify the log file in a server context and use the $host  
variable.

access\_log /var/log/nginx/$host/access.log

Although in the wiki such practice is discouraged:  
[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/NginxHttpLogModule#access_log) So yes the best  
practice is to set an access log for each domain.

You have to specify a full path otherwise nginx will use a default  
path. You can specify a default log file in the --http-log-path  
configure option.

> I remove the “access\_log logs/prueba.access.log main;” line and  
> works, but when I browse “[www.prueba.com](http://www.prueba.com)” I get nginx default html  
> page, and when I browse “[Apache2 Ubuntu Default Page: It works](http://www.prueba.com/index.html)” the index.html  
> placed in /var/www/virtuales/prueba.com/html is showed.
> 
> is possible show index.html directly when “[prueba.com](http://prueba.com)” or  
> “[www.prueba.com](http://www.prueba.com)” is entered in the browser?

Yes.

> Maybe some redirection in the VirtualHost configuration file

Nope. It’s the access log error.

> what about the error logs files?

See above.

Fix the access log error. Nginx isn’t loading the configuration. It’s  
using a  
previous one.

See [GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com · GitHub](http://wiki.nginx.org/CommandLine), the HUP signal handling to be  
specific.

— appa

---

<div class="post-metadata">

**Author:** ![marcelo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/f17d59/32.png) [@marcelo](https://www.ruby-forum.com/u/marcelo)\
**Post date:** [September 23, 2010, 7:54am UTC](https://www.ruby-forum.com/t/how-to-add-virtualhosts-in-nginx/195361/7 "2010-09-23T07:54:27Z")

</div>

Hello,

On Wed, 22 Sep 2010 15:32:55 -0300  
Marcelo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ### vhost1 - [www.prueba.com](http://www.prueba.com)
> 
> 2010/09/22 13:21:22 [info] 24429#0: the configuration file  
> /etc/nginx/nginx.conf syntax is ok  
> 2010/09/22 13:21:22 [emerg] 24429#0: open()  
> “/usr/share/nginx/logs/prueba.access.log” failed (2: No such file or  
> directory)  
> 2010/09/22 13:21:22 [emerg] 24429#0: the configuration file  
> /etc/nginx/nginx.conf test failed
> 
> Â¿the logs file must be created maually everytime I create a domain?

is the directory really existing ? Nginx will only create the file, not  
the path.

Cheers,

C.
