# Regular expression used in server\_name directive

**URL:** <https://www.ruby-forum.com/t/regular-expression-used-in-server-name-directive/227079>\
**Category:** NGINX\
**Created:** [March 3, 2013, 6:44pm UTC](https://www.ruby-forum.com/t/regular-expression-used-in-server-name-directive/227079 "2013-03-03T18:44:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [March 3, 2013, 6:44pm UTC](https://www.ruby-forum.com/t/regular-expression-used-in-server-name-directive/227079/1 "2013-03-03T18:44:44Z")

</div>

I am using a regular expression with a capture group in my server\_name  
directive. It looks like:

```
server_name (?<account>.+)\.mydomain\.com$

```

The problem is that I want to expand it slightly and say anything except  
[web3.mydomain.com](http://web3.mydomain.com). I.E.

[something.mydomain.com](http://something.mydomain.com) matches, but [web3.mydomain.com](http://web3.mydomain.com) does not match.

How is this possible?

Posted at Nginx Forum:

> **[Regular expression used in server\_name directive](https://forum.nginx.org/read.php?2,236870,236870#msg-236870)**
>
> I am using a regular expression with a capture group in my server\_name directive. It looks like: server\_name (?\<account\>.+)\\.mydomain\\.com$ The problem is that I want to expand it slightly and say anything except web3.mydomain.com. I.E. somet

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [March 3, 2013, 8:13pm UTC](https://www.ruby-forum.com/t/regular-expression-used-in-server-name-directive/227079/2 "2013-03-03T20:13:31Z")

</div>

On Sun, Mar 03, 2013 at 12:43:57PM -0500, justin wrote:

Hi there,

> I am using a regular expression with a capture group in my server\_name  
> directive. It looks like:
> 
> ```
> server_name (?<account>.+)\.mydomain\.com$
> 
> ```

[Module ngx\_http\_core\_module](http://nginx.org/r/server_name) for details of how the matching server{}  
block is chosen.

> The problem is that I want to expand it slightly and say anything except  
> [web3.mydomain.com](http://web3.mydomain.com). I.E.
> 
> [something.mydomain.com](http://something.mydomain.com) matches, but [web3.mydomain.com](http://web3.mydomain.com) does not match.
> 
> How is this possible?

Make a different server{} block that matches [web3.mydomain.com](http://web3.mydomain.com).

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [March 3, 2013, 8:21pm UTC](https://www.ruby-forum.com/t/regular-expression-used-in-server-name-directive/227079/3 "2013-03-03T20:21:43Z")

</div>

I actually have a working regular expression which should work now:

server\_name ^(?!web3.)(?.+).mydomain.com$;

But for some odd reason when I restart nginx, I am getting:

nginx: [emerg] unknown “account” variable

This should work though, right?

Posted at Nginx Forum:

> **[Re: Regular expression used in server\_name directive](https://forum.nginx.org/read.php?2,236870,236876#msg-236876)**
>
> I am using a regular expression with a capture group in my server\_name directive. It looks like: server\_name (?\<account\>.+)\\.mydomain\\.com$ The problem is that I want to expand it slightly and say anything except web3.mydomain.com. I.E. somet

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [March 3, 2013, 9:05pm UTC](https://www.ruby-forum.com/t/regular-expression-used-in-server-name-directive/227079/4 "2013-03-03T21:05:32Z")

</div>

On Sun, Mar 03, 2013 at 02:21:10PM -0500, justin wrote:

Hi there,

> I actually have a working regular expression which should work now:
> 
> server\_name ^(?!web3.)(?.+).mydomain.com$;

> **[Module ngx\_http\_core\_module](http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name)**
>
> This directive appeared in version 1.11.8.

Pay particular attention to the line that begins “It is also possible  
to use regular expressions in server names”.

> But for some odd reason when I restart nginx, I am getting:
> 
> nginx: [emerg] unknown “account” variable

Did you get that message when you tested with your initial version too?

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [March 3, 2013, 9:16pm UTC](https://www.ruby-forum.com/t/regular-expression-used-in-server-name-directive/227079/5 "2013-03-03T21:16:37Z")

</div>

No, simply doing

server\_name ~^(?.+).mydomain.com$;

Works. This may be a bug with nginx? The new regular expression is valid  
and  
should work.

Posted at Nginx Forum:

> **[Re: Regular expression used in server\_name directive](https://forum.nginx.org/read.php?2,236870,236881#msg-236881)**
>
> I am using a regular expression with a capture group in my server\_name directive. It looks like: server\_name (?\<account\>.+)\\.mydomain\\.com$ The problem is that I want to expand it slightly and say anything except web3.mydomain.com. I.E. somet

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [March 3, 2013, 10:39pm UTC](https://www.ruby-forum.com/t/regular-expression-used-in-server-name-directive/227079/6 "2013-03-03T22:39:37Z")

</div>

On Sun, Mar 03, 2013 at 03:16:09PM -0500, justin wrote:

Hi there,

> server\_name ~^(?.+).mydomain.com$;
> 
> Works.

Correct.

Now compare that line with the server\_name lines in the previous mails.

> This may be a bug with nginx?

No.

> The new regular expression is valid and should work.

Yes; nginx is happy with the regular expression, provided that it knows  
that it is intended to be a regular expression.

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![justin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@justin](https://www.ruby-forum.com/u/justin)\
**Post date:** [March 4, 2013, 12:17am UTC](https://www.ruby-forum.com/t/regular-expression-used-in-server-name-directive/227079/7 "2013-03-04T00:17:46Z")

</div>

My bad, stupid mistake, forgot the ^.

Working fine now. Thanks.

Posted at Nginx Forum:

> **[Re: Regular expression used in server\_name directive](https://forum.nginx.org/read.php?2,236870,236887#msg-236887)**
>
> I am using a regular expression with a capture group in my server\_name directive. It looks like: server\_name (?\<account\>.+)\\.mydomain\\.com$ The problem is that I want to expand it slightly and say anything except web3.mydomain.com. I.E. somet
