# RE: Converting Apache configs to nginx, why is a NameVirtualHost workalike is a bad thing?

**URL:** <https://www.ruby-forum.com/t/re-converting-apache-configs-to-nginx-why-is-a-namevirtualhost-workalike-is-a-bad-thing/229793>\
**Category:** NGINX\
**Created:** [February 12, 2013, 4:00am UTC](https://www.ruby-forum.com/t/re-converting-apache-configs-to-nginx-why-is-a-namevirtualhost-workalike-is-a-bad-thing/229793 "2013-02-12T04:00:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Unai\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/7c8e57/32.png) [@Unai\_R](https://www.ruby-forum.com/u/Unai_R)\
**Post date:** [February 12, 2013, 4:00am UTC](https://www.ruby-forum.com/t/re-converting-apache-configs-to-nginx-why-is-a-namevirtualhost-workalike-is-a-bad-thing/229793/1 "2013-02-12T04:00:45Z")

</div>

Having one block per server is the best for performance because you only  
test a condition once; the way you’re doing it would require NGINX to  
evaluate hostnames twice or maybe more.

In my opinion config maintenance is important as well so either stick to  
what you have (unless those sites have thousands of requests per second)  
or generate the configs using Puppet, Chef or the like…

Sent from Samsung Mobile

-------- Original message --------  
From: Darren P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Date:  
To: removed\_email\_address@domain.invalid  
Subject: Converting Apache configs to nginx, why is a NameVirtualHost  
workalike is a bad thing?

I’m switching some servers from Apache 2.2.x to nginx 1.2.6. In Apache,  
I use NameVirtualHost. I also use Rewrite directives to manage www.  
prefixing via a 301 redirect. All the user has to is create  
/wwwroot/$hostname, upload the site files, and point DNS at my server.  
On my end, all I have to do is create the Apache config once as part of  
the new-user setup.

When I converted that to nginx, this is what I came up with:

root /www/user/wwwroot/$http\_host;

For the user who wants to drop the www.:

if ($http\_host ~ ^www.(.+)$) {  
return 301 http://$1$request\_uri;  
}

For the user who wants to always have the www.:

if ($http\_host !~ ^www.) {  
return 301 http://www.$http\_host$request\_uri;  
}

Which, in testing, works a treat. Various articles say this style of  
configuration is bad. Instead I should:

1. have per-domain server blocks;
2. have a server block for [www.example.com](http://www.example.com) that redirects to  
[example.com](http://example.com). or vice versa.

In other words, for the user who has 37 domains, I’ll need 74 server  
blocks in their nginx config. That’s a significant regression in terms  
of workload and simplicity.

If it’s bad, ok, I won’t do that; however, I can’t seem to find an  
explaination _why_ it’s bad. Would someone please clarify that point?
