[NGINX]无法让 proxy_pass 和 robots.txt 工作

[NGINX]Can't get proxy_pass and robots.txt to work

我是NGINX背后的运行 Kibana,我想补充一个robots.txt。我将 robots.txt 添加到 /var/www/example.com,但无论我尝试什么,当我访问 example.com/robots.txt 时,它都会重定向到 Kibana。

server {

        server_name example.com www.example.com;

location = /robots.txt {
    root /var/www/example.com/;
}

location / {

  if ($request_uri ~* "^/robots.txt") {
    rewrite ^/robots.txt /robots.txt last;
  }

        proxy_pass http://localhost:5601;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

~

}

server {
    if ($host = www.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    if ($host = example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


        listen 80;
        listen [::]:80;

        server_name example.com www.example.com;
    return 404; # managed by Certbot
}

我试过以下方法:

        root /var/www/example.com/html;

位置/

location = /robots.txt {
    alias /var/www/example/com/html/robots.txt ;
}

在服务器下。

我能得到的最接近的是根目录,它将 example.com/robots.txt 重定向到 example.com/var/www/example.com/html/robots.txt.

如果/robots.txt的路径是/var/www/example.com/html/robots.txt,使用:

server {
    ...

    location = /robots.txt {
        root /var/www/example.com/html;
    }

    location / {
        ...
    }
}

完全匹配 location 块将始终优先。有关详细信息,请参阅 this document