nginx虚拟主机没有听正确的主机名

我是nginx的新手,但是我遇到了一个对我来说完全不明显的基本问题。

我在我的Mac上使用brew安装了nginx。 这工作得很好,这个基本的,不变的默认configuration:

server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } 

现在,我是一个开发多个项目的开发人员。 以前我在apache中为每一个虚拟主机configuration了一个虚拟主机,与myproject1.local这样的自定义主机条目结合在一起。

所以,为了用nginxtesting这个,我编辑了我的/ etc / hosts文件并添加了:

 # /etc/hosts 127.0.0.1 mytestproject.local 

然后,在nginxconfiguration中,我添加了以下内容:

 server { listen 8080; listen mytestproject.local:80; server_name mytestproject.local; location / { root /Users/MyUserName/Projects/mytestproject/Web; index index.html; } } 

就这样,我预计

  • http://localhost应显示默认网站
  • http://localhost:8080http://mytestproject.local应显示来自mytestproject的网站。

相反,这是发生了什么事情:

  • http://localhost:8080http://mytestproject.local正确显示来自mytestproject的网站。
  • 但是, http://localhost 提供了testing项目中的文件,而不是像我预期的那样来自默认网站的文件

所以,这是我的问题:

我错在哪里? 我需要configuration本地主机显示默认网站,而其他端口和其他自定义域(通过/ etc / hosts)上的其他内容显示其相应的内容,而不是混合起来?

提前致谢。

阅读nginx如何select服务器块http://nginx.org/en/docs/http/request_processing.html#mixed_name_ip_based_servers

在你的情况下,第一个服务器监听*:80 ,第二个监听127.0.0.1:80 ,当你去http://localhost/你的浏览器连接到127.0.0.1:80和nginxselect第二个服务器块,因为它更好地匹配请求,然后testingHost头。

所以简单的解决scheme是取代listen mytestproject.local:80; 用简单的listen 80;

我有一种奇怪的感觉,Bonjour服务有一些东西。 Bonjour服务为.local域做组播dns https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/NetServices/Articles/domainnames.html#//apple_ref/doc/uid/20001072-202537