【问题标题】:nginx: how to get a server for the case of no Host: header?nginx:在没有 Host: 标头的情况下如何获取服务器?
【发布时间】:2019-02-04 20:16:12
【问题描述】:

我有一个特殊的服务器 {} 块,用于在主机标头 (server_name ~^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+;) 中使用 IP 地址的人的情况下,它工作正常。当根本没有提供主机标头时,我想要一个不同的服务器块。我一直在测试:

telnet localhost 80
GET /foobar HTTP/1.0

我从我的日志记录中看到(在 log_format 中有 "$http_host"),这显示为 "-"(连字符不是下划线)。但是这个服务器块:

server {
        listen 80;
        listen [::]:80;

        # hyphen not underscore
        server_name ~^-*$;

        root /var/www/no-host;
        default_type text/plain;
        index foo.ey
        location / {
                try_files $uri /foo.ey;
        }
}

永远不会被使用,而是使用默认的server_name _;(下划线不是连字符)。

我的用例类似于域名停放站点,我希望处理大量主机名,并且应该在数据库中查找所有合法的主机名,但我想尽早筛选某些非法主机名。

【问题讨论】:

  • 尝试^$ 匹配一个空的服务器名称
  • ^-*$ 中添加-* 之前我就有了。
  • 答案在文档中:nginx.org/en/docs/http/server_names.html 搜索单词“empty”
  • server_name "";
  • 那行得通。谢谢@Barmar。

标签: nginx http-headers nginx-config


【解决方案1】:

答案在文档中:https://nginx.org/en/docs/http/server_names.html
搜索单词“空”

修复: server_name "";

——Barmar

【讨论】:

    猜你喜欢
    • 2021-05-04
    • 2019-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 2012-06-16
    相关资源
    最近更新 更多