Nginx:在位置使用$ http_host

我在nginxconfiguration中有很多服务器别名

server { listen 80; server_name example.com site1.example.com site2.example.com site3.example.com ... 

如何我可以控制robots.txt文件的位置别名的具体文件夹,例如我的意思是:

 location ^~ $http_host/robots.txt { alias /home/web/public_html/static/$http_host/robots.txt; } 

以上build设不能工作。

你可以使用$host nginxvariables :

 location /robots.txt { alias /home/web/public_html/static/$host/robots.txt; } 

要么:

 location /robots.txt { root /home/web/public_html/static/$host; } 

默认情况下, $host代表: 按照以下优先顺序:来自请求行的主机名,或来自“主机”请求标头字段的主机名,或者与请求匹配的服务器名。

我不知道这是可能的。 如果是有人会提供另一个答案。

另一种方法是使用任何自定义位置为每个站点创build一个服务器块,然后将所有常见指令包含在通用的cile中。 像这样的东西

example1.conf

 server { listen 80; server_name site1.example.com; include common1.conf; location ^~ robots.txt { alias /home/web/public_html/static/example1/robots.txt; } } 

example2.conf

 server { listen 80; server_name site2.example.com; include common1.conf; location /abcd { //whatever } location ^~ robots.txt { alias /home/web/public_html/static/example2/robots.txt; } } 

common1.conf

 location /common/location { //whatever } 

您的location说明存在问题。 它只匹配从用户发送的规范化的URI,即域名之后的部分,可能在URL中和查询参数之前的端口。

此外,您不要在alias指令中指定实际的文件名,只是在哪里find文件。

因此您需要使用:

 location /robots.txt { alias /home/web/public_html/static/example1; } 

在你的configuration中。