Serving 404s for Old Subdomains with NGINX on Laravel Forge

James Fairhurst
Mar 22, 2019 · 1 min read

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.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store