# Default Server Block

**URL:** <https://www.ruby-forum.com/t/default-server-block/188165>\
**Category:** NGINX\
**Created:** [May 6, 2010, 7:20pm UTC](https://www.ruby-forum.com/t/default-server-block/188165 "2010-05-06T19:20:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [May 6, 2010, 7:20pm UTC](https://www.ruby-forum.com/t/default-server-block/188165/1 "2010-05-06T19:20:03Z")

</div>

The wiki says:

“If the directive has the default parameter, then the enclosing server  
{…} block will be the default server for the address:port pair. This  
is useful for name-based virtual hosting where you wish to specify the  
default server block for hostnames that do not match any server\_name  
directives. If there are no directives with the default parameter,  
then the default server will be the first server block in which the  
address:port pair appears.”

I have multiple server blocks listening on both 80 and 443:

server {  
listen 80;  
server\_name [mydomain.com](http://mydomain.com);  
…  
}

server {  
listen 443;  
server\_name [mydomain.com](http://mydomain.com);

ssl on;  
ssl\_certificate certs/mydomain.crt;  
ssl\_certificate\_key certs/mydomain.key;

…  
}

server {  
listen 80;  
server\_name [myotherdomain.com](http://myotherdomain.com);  
…  
}

server {  
listen 443;  
server\_name [myotherdomain.com](http://myotherdomain.com);

ssl on;  
ssl\_certificate certs/myotherdomain.crt;  
ssl\_certificate\_key certs/myotherdomain.key;

…  
}

Does this mean if a request with either an unmatched Host header or no  
Host header, will go to port 80 for [mydomain.com](http://mydomain.com) because it is the  
first block, or will this request get dropped because it does not  
match any server\_name directives?

If it is being served by the first block, would I catch this with a  
server\_name of “” and a default directive?

# Catch-all, redirect somewhere useful.

server {  
listen 80 default;  
server\_name “”;

# Do something useful with this traffic;

}

---

<div class="post-metadata">

**Author:** ![WSandrew\_L](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/d07c76/32.png) [@WSandrew\_L](https://www.ruby-forum.com/u/WSandrew_L)\
**Post date:** [May 6, 2010, 7:27pm UTC](https://www.ruby-forum.com/t/default-server-block/188165/2 "2010-05-06T19:27:05Z")

</div>

On Thu, May 06, 2010 at 10:19:07AM -0700, W. Andrew Loe III wrote:

> I have multiple server blocks listening on both 80 and 443:
> 
> …  
> …  
> }
> 
> Does this mean if a request with either an unmatched Host header or no  
> Host header, will go to port 80 for [mydomain.com](http://mydomain.com) because it is the  
> first block, or will this request get dropped because it does not  
> match any server\_name directives?

It will go to the first block.

> If it is being served by the first block, would I catch this with a  
> server\_name of “” and a default directive?
> 
> # Catch-all, redirect somewhere useful.
> 
> server {  
> listen 80 default;  
> server\_name “”;
> 
> # Do something useful with this traffic;
> 
> }

It is catched by “default” server for listen port.  
Please read this:

> **[How nginx processes a request](http://nginx.org/en/docs/http/request_processing.html)**

–  
Igor S.

> **[Igor Sysoev](http://sysoev.ru/en/)**
