aharown07
aharown07

Reputation: 166

Stop nginx from ignoring specific subdomain in server block

I've been using nginx on Ubuntu 20 for a few years now mostly without problems, but on a newly deployed server, I can't seem to get this right.

I have a serverblock in a file named alpha in /sites_available. The server block has server_name alpha.example.com set up with a document root = /var/www/alpha.

I have a serverblock in a file named beta in /sites_available. The server block has server_name beta.example.com set up with a document root = /var/www/beta.

DNS a records exist for both alpha.example.com and beta.example.com.

What happens:

URL http://beta.example.com displays http://beta.example.com in the browser address, but pulls content from /var/www/alpha.

By experimenting, I've found that nginx is consistently processing the file that comes first alphabetically in sites_available, regardless of the subdomain in the URL.

May questions are, Why does it behave this way? and How can I turn that off?

The behavior I want is for each server block to route a single subdomain to a specific document root and ignore everything else. So... http://beta.example.com doesn't even try to go to /var/www/alpha

Here's an example of one of the server block file contents

server {
  listen 80;
  listen [::]:80;
  server_name alpha.example.com;
  root /var/www/alpha;
  index index.php index.html index.htm index.nginx-debian.html;

  access_log /var/log/nginx/alpha_access.log;
  error_log /var/log/nginx/alpha_error.log;

  location / {
    try_files $uri $uri/ /index.php;
  }

  location ~ ^/(doc|sql|setup)/ {
    deny all;
  }

  location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    include snippets/fastcgi-php.conf;
  }

  location ~ /\.ht {
    deny all;
  }
}

So, to say it another way: What do I need to put in there to tell nginx "If the subdomain isn't alpha, ignore all this... this is only for alpha.example.com"?

Upvotes: 0

Views: 533

Answers (1)

aharown07
aharown07

Reputation: 166

Turned out my problem was a typo in the server name in one of the blocks. With my attention on the sobdomain piece, I failed to notice the domain name itself was wrong (e.g. alpha.exampple.com would do it).

The results of this are counterintuitive and can send you barking up all sorts of wrong trees trying to figure out what's wrong.

Part of the answer, too, though, is that nginx is apparently designed with a strong bias toward finding a server block that can respond to the request... even if the subdomain in the url doesn't match anything.

The key to tightening that up is probably writing a good default server block . . . which I'm still working on.

For example, I have http://example.com going to the default, but in the scenario I described above, with server blocks for alpha.example.com and beta.example.com, keying in http://x.example.com doesn't land at my default.

So some work to do there. Advice welcome.

(Edit: I'm pretty sure that a catch-all default server for anyrandomcharacters.example.com can't happen. There would have to be DNS routing the subdomain to your server before nginx can do anything with the subdomain. Maybe wild card DNS can serve that purpose.)

Upvotes: 1

Related Questions