1.server_name指令
http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name
Sets configuration for a virtual server. There is no clear separation between IP-based
(based on the IP address) and name-based (based on the “Host” request header field) virtual
servers. Instead, the listen directives describe all addresses and ports that should accept
connections for the server, and the server_name directive lists all server names. Example
configurations are provided in the “How nginx processes a request” document.
Syntax: server_name name ...;
Default:
server_name "";
Context: server
Sets names of a virtual server, for example:
server {
server_name example.com www.example.com;
}
Server匹配顺序:
(1)精确匹配
(2)*在前的泛域名
(3)*在后的泛域名
(4)按文件中的顺序匹配正则表达式域名
(5)default server:第1个,listen指定default
2. server_name_in_redirect
http://nginx.org/en/docs/http/ngx_http_core_module.html#server_name_in_redirect
作用:
Enables or disables the use of the primary server name, specified by the server_name directive,
in absolute redirects issued by nginx. When the use of the primary server name is disabled,
the name from the “Host” request header field is used. If this field is not present, the IP address
of the server is used.
配置示例:
Syntax: server_name_in_redirect on | off;
Default: server_name_in_redirect off;
Context: http, server, location
server_name指令后可以跟多个域名,第1个是主域名,
server_name_in_redirect来决定主域名是否生效,
当配置"server_name_in_redirect off;"时,主域名是不生效的.
curl second.taohui.tech -I 可以显示响应的头部
server {
server_name primary.taohui.tech second.taohui.tech;
server_name_in_redirect off;
return 302 /redirect;
}
测试:
curl second.taohui.tech -I
测试结果:
此时返回的location信息是:
http://second.taohui.tech/redirect
server {
server_name primary.taohui.tech second.taohui.tech;
server_name_in_redirect on;
return 302 /redirect;
}
测试:
curl second.taohui.tech -I
测试结果:
此时返回的location信息是:
http://primary.taohui.tech/redirect
我的测试:
我的配置文件:
server {
listen 8081;
#server_name localhost;
server_name 192.168.118.174 192.168.118.175;
#server_name_in_redirect on;// 第一次启用
server_name_in_redirect off;// 第二次启用
return 302 /redirect;
}
3.我的问题
为什么要配置多个域名呢?
多个域名是如何与IP对应的?域名和IP的映射关系是怎样的?