Serving 404s for Old Subdomains with NGINX on Laravel Forge

Originally published at www.jamesfairhurst.co.uk.

Previously my old site hosted quite a number of subdomains for old projects and scripts, when moving to Laravel Forge and setting up SSL everything now gets redirected. I wanted to serve 404s for those old subdomains as it’s more of a correct response than a 301 to the homepage.

I’m not familiar with NGINX but the configs are nice and clean, Forge uses a before include which has a ssl_redirect.conf file which deals with redirecting non https traffic to it's https counterpart.

# FORGE CONFIG (DO NOT REMOVE!)
include forge-conf/www.example.co.uk/before/*;
server {
...

I created a new file in the /etc/nginx/forge-conf/www.example.co.uk/before/ directory that listens for specific subdomains and simply returns a default 404 response. I named that file a.conf to ensure that it loads before any existing conf files.

server {
listen 80;
listen [::]:80;
server_name a.example.co.uk b.example.co.uk;
return 404;
}

Finally restart NGINX (in the Forge admin) to ensure that the new config file comes into play and voila!


Originally published at www.jamesfairhurst.co.uk.