我正在运行nginx版本1.1.19,我有configuration服务器指令来提供不同的页面,取决于是否有人用主机名domain.com(我显示主网页)击中我的服务器,或者他们是否打我的服务器与其他任何(我显示“没有直接的匹配,但你仍然得到这个不错的网页”页面)。
的fileA
server { listen 80; server_name domain.com; root /home/ubuntu/http_resources/apex/public; location / { index index.html; }
FILEB
server { listen 80; server_name ""; #Should catch everything else. root /home/ubuntu/http_resources/otherwise/public; location / { index index.html; }
这两个服务器块configuration都在两个单独的文件中,符号链接到已启用网站。 这符合我的预期。
我想将www.domain.comredirect到domain.com,但是其他所有子域仍然会出现“No direct matches …”页面。 我创build一个文件包含
fileC:
server { listen 80; server_name www.domain.com; rewrite ^ http://domain.com$request_uri? permanent; }
并将该文件符号链接到启用网站,认为这将匹配www.domain.com并阻止www.domain.com去到fileB中服务器块的Web根目录。
这似乎开始将anything.domain.comredirect到domain.com几分钟,而domain.com似乎仍在运行。 (也许是由于caching或什么)。 几分钟后! 我的Amazon负载平衡器说,domain.com/index.html在应用程序服务器上不再处于活动状态(详细信息:负载平衡器检查裸域domain.com以查找index.html以确定stream量是否应该到达服务器;还有只是负载均衡器后面的一个应用程序服务器)。 然后负载平衡器停止路由到服务器,因为domin.com似乎不可用,所以当然没有domain.com相关的URL在我的浏览器工作,因为负载平衡器不指向任何东西。
我曾尝试运行'wget public_ip_of_server_behind_balancer / index.html,并得到一个index.html文件,基本上说
Site Closed Down This site has been closed by the service provider.
删除符号链接到fileC并重新启动nginx会导致负载均衡器重新连接到应用程序服务器,并且所有工作原来都是如此。 我在做什么错了?
我没有使用Amazon负载均衡器的经验。 所以,我的回答不会解决你提到的问题,但希望能帮助你缩小问题的范围。
对于fileB,根据server_name上的Nginx维基 , server_name ""
意味着这个服务器块将捕获没有“主机”头的请求。 所以,我build议在这样的listen指令中使用default_server
(如这里所述 )…
http { index index.html; server { listen 80 default_server; server_name _; # This is just an invalid value which will never trigger on a real hostname. access_log logs/default.access.log main; server_name_in_redirect off; root /var/www/default/htdocs; } }
对于fileC,因为你正在做永久redirect,我build议使用return指令,而不是return regex replacement permanent;
指令,我会删除?
在URL的结尾也是这样的…
server { listen 80; server_name www.domain.com; return 301 $scheme://domain.com$request_uri; }
希望这有助于一些延伸。