# Performance penalty when using regular expressions in server\_name?

**URL:** <https://www.ruby-forum.com/t/performance-penalty-when-using-regular-expressions-in-server-name/238809>\
**Category:** NGINX\
**Created:** [January 29, 2014, 8:52pm UTC](https://www.ruby-forum.com/t/performance-penalty-when-using-regular-expressions-in-server-name/238809 "2014-01-29T20:52:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![musicdenotation](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea666f/32.png) [@musicdenotation](https://www.ruby-forum.com/u/musicdenotation)\
**Post date:** [January 29, 2014, 8:52pm UTC](https://www.ruby-forum.com/t/performance-penalty-when-using-regular-expressions-in-server-name/238809/1 "2014-01-29T20:52:49Z")

</div>

Hi,

let assume that we run the following domains:

- [agoodexample.org](http://agoodexample.org)
- [agoodexmaple.com](http://agoodexmaple.com)
- [agoodexample.net](http://agoodexample.net)
- [a-good-example.org](http://a-good-example.org)
- [a-good-example.com](http://a-good-example.com)
- [a-good-example.net](http://a-good-example.net)

Our main site should be [www.a-good-example.com](http://www.a-good-example.com) – people coming from any  
other mentioned domain/combination should be redirected to the main  
site.

I could now write a single server block like

server {  
// …  
server\_name ~^(?:www.)?a[-]?good-example.(?:org|net)$  
~^ a[-]?good-example.com $;

```
 rewrite ^ $scheme://www.a-good-example.com $request_uri permanent;

```

}

I am using regular expressions as server\_name values, I think you get  
the  
point.

I could also avoid regular expressions:

server {  
// …  
server\_name [agoodexample.org](http://agoodexample.org)  
[a-good-example.org](http://a-good-example.org)  
[www.agoodexample.org](http://www.agoodexample.org)  
[www.a-good-example.org](http://www.a-good-example.org)  
[agoodexample.net](http://agoodexample.net)  
[a-good-example.net](http://a-good-example.net)  
[www.agoodexample.net](http://www.agoodexample.net)  
[www.a-good-example.net](http://www.a-good-example.net)  
[agoodexample.com](http://agoodexample.com)  
[www.agoodexample.com](http://www.agoodexample.com)  
[a-good-example.com](http://a-good-example.com);

```
 rewrite ^ $scheme://www.a-good-example.com $request_uri permanent;

```

}

Q1: Are both configurations equal or is there any performance  
difference?  
E.g. would you recommend one configuration more than the other?

Q2: When using regular expressions in server\_name, the regular  
expression  
will also appear in the error\_log. Is it possible to see the actual  
name instead the expression in the log?

I am using nginx-1.4.x with pcre-jit enabled.

Thanks.

–  
Regards,  
Igor

---

<div class="post-metadata">

**Author:** ![Igor\_Sverkos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b9bd4f/32.png) [@Igor\_Sverkos](https://www.ruby-forum.com/u/Igor_Sverkos)\
**Post date:** [January 29, 2014, 9:16pm UTC](https://www.ruby-forum.com/t/performance-penalty-when-using-regular-expressions-in-server-name/238809/2 "2014-01-29T21:16:17Z")

</div>

A different way would to use Lua and an alias table, for a dynamic site  
I  
use this to allow a site multiple aliases all directing to the same  
root,  
its a simple array with 2 lines for each main site, 1 as server\_name and  
line 2 endless aliases. Its fast and no need to edit nginx.conf just  
edit a  
plain ascii file.

Posted at Nginx Forum:

> **[Re: Performance penalty when using regular expressions in server\_name?](https://forum.nginx.org/read.php?2,246970,246973#msg-246973)**
>
> Hi, let assume that we run the following domains: - agoodexample.org - agoodexmaple.com - agoodexample.net - a-good-example.org - a-good-example.com - a-good-example.net Our main site should be www.a-good-example.com -- peopl

---

<div class="post-metadata">

**Author:** ![Igor\_Sverkos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/b9bd4f/32.png) [@Igor\_Sverkos](https://www.ruby-forum.com/u/Igor_Sverkos)\
**Post date:** [January 30, 2014, 7:53am UTC](https://www.ruby-forum.com/t/performance-penalty-when-using-regular-expressions-in-server-name/238809/3 "2014-01-30T07:53:39Z")

</div>

On Jan 29, 2014, at 23:52 , Igor Sverkos wrote:

> ```
> rewrite ^ $scheme://www.a-good-example.com $request_uri permanent;
> a-good-example.org
> rewrite ^ $scheme://www.a-good-example.com $request_uri permanent;
> 
> ```
> 
> }
> 
> Q1: Are both configurations equal or is there any performance difference?  
> E.g. would you recommend one configuration more than the other?

The second is faster but I believe performance is negligible.  
And it’s better to use

return 301 $scheme://www.a-good-example.com/$request\_uri;

> Q2: When using regular expressions in server\_name, the regular expression  
> will also appear in the error\_log. Is it possible to see the actual  
> name instead the expression in the log?
> 
> I am using nginx-1.4.x with pcre-jit enabled.

$host.

–  
Igor S.

> **[Advanced Load Balancer, Web Server, & Reverse Proxy - NGINX](https://www.nginx.com/)**
>
> NGINX accelerates content and application delivery, improves security, and facilitates availability and scalability for the busiest websites on the Internet.
