Nginx 处理请求的顺序问题

对于Name-Based virtual servers, nginx 只会测试request 中的header field “Host” 来决定把这个请求route 给谁. 如果这个请求不存在Host这个field, 或者Host 的值不能match 任何一个server_name, 那么nginx就会把这个请求route 给这个port 的default server. 如果我们没有在conf中指明哪个server block 为default_server, 那么nginx 就会把第一个server block认为是default server.

name-based virtual server 的例子如下:

server {
 listen 80;
 server_name example.org www.example.org;
 ...
}

server {
 listen 80;
 server_name example.net www.example.net;
 ...
}

server {
 listen 80;
 server_name example.com www.example.com;
 ...
}

default_server 的例子如下:

server {
 listen 80 default_server;
 server_name example.net www.example.net;
 ...
}

 

对于mixed name-based and ip-based virtual server, 例如:

server {
 listen 192.168.1.1:80;
 server_name example.org www.example.org;
 ...
}

server {
 listen 192.168.1.1:80;
 server_name example.net www.example.net;
 ...
}

server {
 listen 192.168.1.2:80;
 server_name example.com www.example.com;
 ...
}

nginx 的处理顺序是先ip和port 的组合,然后才是server_name.

举个例子就是,如果在192.168.1.1:80上收到了一个www.example.com 的请求的话,那么这个请求就会被发到192.168.1.1:80 的default server, 因为对于这个ip和port 的组合,里面没有这个server_name.

 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.