Building on top of the question here:
https://www.digitalocean.com/community/questions/dokku-and-preventing-random-domains
I figured out how to have a landing page for my main host as well as still serve my vhosts, e.g.
example.com -> index.html
app1.example.com -> app1
Those two things work OK, but I’m curious about how to setup a 404 page for invalid vhosts, e.g.
NonExistentApp.eample.com -> 404
instead of
NonExistentApp.example.com -> example.com
The location section of my /etc/nginx/sites-enabled/default does look like its supposed to serve a 404 if it cant find what the URL is requesting, but I think there’s some issue where it still tries to serve the main index.html, even if there’s an invalid vhost
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
Any help would be appreciated!
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Just return 404 in whatever block you want.
server {
listen 80 default_server;
return 404;
}
Thanks to help from @memnochxx, I’ve been able to get a working dokku nginx configuration that can deliver 404 pages for both http and https requests for any unknown pages or vhost’s (this is on Ubuntu 14.04 running Dokku v0.5.5).
Firstly, the /etc/nginx/nginx.conf is the file that needs to be modified in order to return 404. As explained in the dokku nginx documentation, you’ll need to place the 404 return before the virtual hosts config in the primary http block. The result should look like this:
##
# Custom 404 return for requests made to any URL or vhost
# that doesn't exist or match a dokku application.
#
# Make sure that this goes above "Virtual Host Configs"
##
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
listen 443 default_server ssl;
ssl_certificate /etc/nginx/ssl/fake.crt;
ssl_certificate_key /etc/nginx/ssl/fake.pem;
return 404;
}
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
We’ll also need to generate some dummy self-signed SSL certificates to get the https 404 working. We’ll throw those in the /etc/nginx/ssl directory. Run the following commands to do that:
cd /etc/nginx
mkdir ssl
cd ssl
openssl req -x509 -newkey rsa:2048 -keyout fake.pem -out fake.crt -days 365
The days for the openssl command don’t really matter, as the certificate will be invalid anyway. Just run through the default options and don’t worry about the content of the certificate. We’re only putting them there to make nginx happy because it requires a certificate and an associated key to listen on 443. Hope this helped, and thank you again @memnochxx for your help earlier!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.