【Nginx】如何找到处理请求的server指令块

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的映射关系是怎样的?

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值