nginx server_name 的含义

作用

是对请求同一个主机端口,根据请求头中不同host分发到对应虚拟主机(server_name \ host)

示例:

server {
	listen 80;
	server_name www.a.com;
	location / {
		default_type text/html;
		content_by_lua '
			ngx.say("<p>first</p>")
		';
	}
}

 

server {
	listen  80;
	server_name www.b.com;
	location / {
		default_type text/html;
		content_by_lua '
			ngx.say("<p>second</p>")
		';        
	}
}

这样分别访问www.a.com 和www.b.com,其实访问127.0.0.1 的80端口,不同的host,nginx中配置server_name和请求中host头匹配,对应分发到location中。

 

posted @ 2024-01-20 15:56  羊脂玉净瓶  阅读(35)  评论(0)    收藏  举报