Default Server Block

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;

}

server {
listen 443;
server_name mydomain.com;

ssl on;
ssl_certificate certs/mydomain.crt;
ssl_certificate_key certs/mydomain.key;


}

server {
listen 80;
server_name myotherdomain.com;

}

server {
listen 443;
server_name 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 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;

}

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 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:
http://nginx.org/en/docs/http/request_processing.html


Igor S.
http://sysoev.ru/en/