Name-based virtual servers
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;
...
}
nginx获得request的header中的"Host",来决定哪个"server"来处理.如果没有匹配成功则会使用默认的server;在上面的代码中,第一个就是默认的server,也可以添加default_server来指定
server {
listen 80 default_server; # 第一个server默认有此参数
server_name example.net www.example.net;
...
}
How to prevent processing requests with undefined server names
如果requests没有"Host",将不会被通过,可以用以下代码处理:
server {
listen 80;
server_name ""; # 对应没有host
return 444;
}
Mixed name-based and IP-based virtual servers
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;
...
}
In this configuration, nginx first tests the IP address and port of the request against the listen directives of the server blocks. It then tests the “Host” header field of the request against the server_name entries of the server blocks that matched the IP address and port. If the server name is not found, the request will be processed by the default server. For example, a request for www.example.com received on the 192.168.1.1:80 port will be handled by the default server of the 192.168.1.1:80 port, i.e., by the first server, since there is no www.example.com defined for this port.
参考:
http://nginx.org/en/docs/http/request_processing.html