Samer
Samer

Reputation: 988

Reject certain subdomains on port 80 on nginx

I have a few domains/subdomains, and I have a server block to properly redirect them to port 443. But what I'm also trying to do is for a couple of those subdomains, I don't want it to connect at all on port 80.

So below is an example of the values I'm redirecting to port 443.

server {
    listen       80;
    server_name ~^(?<subvar>sub1|sub2|sub3|sub3)\.example\.com$;

    return 301 https://$subvar.example.com$request_uri;
}

So I also have a sub4.example.com which I don't want to connect at all on port 80, but when I try to access it, I get the nginx 404 not found message, what I wanna achieve is a "server not found" sort of message.

Let me know if you'd like more information, or if I'm missing anything.

Upvotes: 4

Views: 851

Answers (1)

Xavier Lucas
Xavier Lucas

Reputation: 2632

  • Don't listen on port 80 for these domains
  • Return nginx's special HTTP code 444 in a default vhost.
server {
    listen 80 default_server;
    return 444;
}

server {
    listen 80;
    server_name ~^(?<subvar>sub1|sub2|sub3)\.example\.com$;
    return 301 https://$subvar.example.com$request_uri;
}

Upvotes: 4

Related Questions