# Listen directive

**URL:** <https://www.ruby-forum.com/t/listen-directive/235936>\
**Category:** NGINX\
**Created:** [July 25, 2013, 9:24am UTC](https://www.ruby-forum.com/t/listen-directive/235936 "2013-07-25T09:24:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [July 25, 2013, 9:24am UTC](https://www.ruby-forum.com/t/listen-directive/235936/1 "2013-07-25T09:24:10Z")

</div>

Hi.

I’ve noticed that in configuratuion like

http {  
server {  
server\_name some.domain.tld;  
listen 1.1.1.1;  
}  
server {  
server\_name another.domain.tld;  
listen 1.1.1.1;  
}  
server {  
server\_name one\_more.domain.tld \*.domain.tld;  
listen 1.1.1.1;  
}  
server {  
server\_name last.domain.tld;  
listen 80;  
}  
}

weird thing happens: this somehow is similar to the apache directives,  
where  
you should use exactly the same “address:port” in “VirtualHost” clause  
as  
you used in “NameVirtualHost address:port” directive. For example if  
you  
used \*, you should use it everywhere, otherwise namevhost won’t work. I  
found that at least on 1.2.1 similar thing happens: vhost  
“last.domain.tld”  
may not get any requests at all, they will be served by the  
\*.domain.tld,  
and, if removed, by the default vhost. So I changed everything to the  
“listen 80” and it resolved the situation.

Is it some bug I stepped on, or is this normal, or, may be I just see  
things  
that aren’t there ?

Thanks.

Posted at Nginx Forum:

> **[listen directive](https://forum.nginx.org/read.php?2,241215,241215#msg-241215)**
>
> Hi. I've noticed that in configuratuion like http { server { server\_name some.domain.tld; listen 1.1.1.1; } server { server\_name another.domain.tld; listen 1.1.1.1; }

---

<div class="post-metadata">

**Author:** ![drook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@drook](https://www.ruby-forum.com/u/drook)\
**Post date:** [July 25, 2013, 9:30am UTC](https://www.ruby-forum.com/t/listen-directive/235936/2 "2013-07-25T09:30:37Z")

</div>

On Jul 25, 2013, at 11:23 , drook wrote:

> ```
> server_name another.domain.tld;
> 
> ```
> 
> }  
> Is it some bug I stepped on, or is this normal, or, may be I just see things  
> that aren’t there ?

> **[How nginx processes a request](http://nginx.org/en/docs/http/request_processing.html#mixed_name_ip_based_servers)**
>
> nginx first decides which server should process the request. Let’s start with a simple configuration where all three virtual servers listen on port \*:80:

–  
Igor S.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

---

<div class="post-metadata">

**Author:** ![drook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@drook](https://www.ruby-forum.com/u/drook)\
**Post date:** [July 25, 2013, 9:50am UTC](https://www.ruby-forum.com/t/listen-directive/235936/3 "2013-07-25T09:50:29Z")

</div>

Thanks, I have read this carefully, but this hasn’t become more clear  
for me

- all the examples contain the IP defined, what about undefined IP  
(which,  
as I understand, means “listen to this port everywhere”) ? Does it  
receive  
less priority ? Plus, in my case server\_name is defined too. I’m not  
complaining, I’m trying to understand better.

Posted at Nginx Forum:

> **[Re: listen directive](https://forum.nginx.org/read.php?2,241215,241218#msg-241218)**
>
> Hi. I've noticed that in configuratuion like http { server { server\_name some.domain.tld; listen 1.1.1.1; } server { server\_name another.domain.tld; listen 1.1.1.1; }

---

<div class="post-metadata">

**Author:** ![drook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@drook](https://www.ruby-forum.com/u/drook)\
**Post date:** [July 25, 2013, 10:35am UTC](https://www.ruby-forum.com/t/listen-directive/235936/4 "2013-07-25T10:35:28Z")

</div>

Thanks a lot, this is much clearer. Actually, ‘absent “listen”, or  
“listen”  
without both a port and address, assume  
the default values for the missing parts’ explains everything, and I  
think  
the manual would be more clear mentioning this explicitly (still imo).

Posted at Nginx Forum:

> **[Re: listen directive](https://forum.nginx.org/read.php?2,241215,241220#msg-241220)**
>
> Hi. I've noticed that in configuratuion like http { server { server\_name some.domain.tld; listen 1.1.1.1; } server { server\_name another.domain.tld; listen 1.1.1.1; }

---

<div class="post-metadata">

**Author:** ![drook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@drook](https://www.ruby-forum.com/u/drook)\
**Post date:** [July 25, 2013, 10:01am UTC](https://www.ruby-forum.com/t/listen-directive/235936/5 "2013-07-25T10:01:00Z")

</div>

On Thu, Jul 25, 2013 at 03:50:02AM -0400, drook wrote:

Hi there,

> Thanks, I have read this carefully, but this hasn’t become more clear for me
> 
> - all the examples contain the IP defined, what about undefined IP (which,  
> as I understand, means “listen to this port everywhere”) ? Does it receive  
> less priority ? Plus, in my case server\_name is defined too. I’m not  
> complaining, I’m trying to understand better.

A request comes in on an ip:port.

The “listen” directives in each server{} block are considered, to find  
the best-match one for that ip:port. Only server{} blocks with that  
“listen” directive are considered for the next stage.

(Absent “listen”, or “listen” without both a port and address, assume  
the default values for the missing parts.)

After that, the server\_name is considered, and the one best-match  
server\_name from the many best-match “listen” servers is chosen.

Is that any clearer? Can you suggest a wording for the manual that might  
have made it clearer to you on first reading?

A consequence is that if you want every server{} to be potentially  
considered for server\_name matching for all requests, you must include  
the same best-match “listen” directive in each server{}.

Thanks,

## f

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

---

<div class="post-metadata">

**Author:** ![drook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@drook](https://www.ruby-forum.com/u/drook)\
**Post date:** [July 25, 2013, 11:14am UTC](https://www.ruby-forum.com/t/listen-directive/235936/6 "2013-07-25T11:14:04Z")

</div>

On Thu, Jul 25, 2013 at 04:34:53AM -0400, drook wrote:

> Thanks a lot, this is much clearer. Actually, ‘absent “listen”, or “listen”  
> without both a port and address, assume  
> the default values for the missing parts’ explains everything, and I think  
> the manual would be more clear mentioning this explicitly (still imo).

The documentation

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

says, in particular:

: If only _address_ is given, the port 80 is used.  
:  
: If directive is not present then either the \*:80 is used if nginx  
: runs with superuser privileges, or \*:8000 otherwise.

---

<div class="post-metadata">

**Author:** ![drook](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a698b9/32.png) [@drook](https://www.ruby-forum.com/u/drook)\
**Post date:** [July 26, 2013, 6:33am UTC](https://www.ruby-forum.com/t/listen-directive/235936/7 "2013-07-26T06:33:20Z")

</div>

Actually, I’ve read carefully this part of the manual before creating  
this  
topic. I still think the quoted passage explains nothing about  
virtualhost  
handling. It doesn’t explain that skipping address or port will result  
in  
what I prefer to call “less prioritized vhosts”. May be it’s obvious  
from  
the parts you and Igor pointed at, but not to me, sorry.

I undrestood the concept (at least I think I did), so no further  
questions  
left - thanks. I think you don’t indend to discuss how to write the  
documantation. 🙂

Posted at Nginx Forum:

> **[Re: listen directive](https://forum.nginx.org/read.php?2,241215,241278#msg-241278)**
>
> Hi. I've noticed that in configuratuion like http { server { server\_name some.domain.tld; listen 1.1.1.1; } server { server\_name another.domain.tld; listen 1.1.1.1; }
