3

I've got a special server {} block for the case of people who use an IP address in a host header (server_name ~^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+;) and that works fine. I'd like a different server block for one when no host header at all is provided. I've been testing with:

telnet localhost 80
GET /foobar HTTP/1.0

And I see from my logging (which has "$http_host" in the log_format) this shows up as "-" (hyphen not underscore). But this server block:

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;
        }
}

Never gets used, the request instead goes to default which has server_name _; (underscore not hyphen).

My use case is similar to a domain parking site, I want a lot of hostnames to be handled, and all legit ones should be looked up in a database, but I want to screen certain illegitimate ones early.

Cupcake Protocol
  • 661
  • 3
  • 10

1 Answers1

1

The answer is in the documentation: https://nginx.org/en/docs/http/server_names.html
Search for the word "empty"

Fix: server_name "";

Barmar

Fabian N.
  • 3,807
  • 2
  • 23
  • 46
Cupcake Protocol
  • 661
  • 3
  • 10