Skip to content

Instantly share code, notes, and snippets.

@HendrikPetertje
Last active December 31, 2015 22:19
Show Gist options
  • Save HendrikPetertje/8052705 to your computer and use it in GitHub Desktop.
Save HendrikPetertje/8052705 to your computer and use it in GitHub Desktop.
Dit mini gidsje (engels) helpt je met het implementeren van wildcard DNS op je rails server met pPassenger en nginx

#Intro Sometimes it can happen that you need a subdomain on your website were you test new features of the site. If this subdomain points to the same server you feel that you wasted time by going trough all those DNS stuff. guess what.. you are. many websites (including Wordpress) have this neat function that when you misstype a subdomain it points to a page 404. Are they making more domains while you are visiting that broken link, or did they in fact register all possible names? no. Meet "wildcard DNS" a system that let's your server create the subdomains and not DNS.

#The steps This guide will help you setup wildcard DNS on your Rails server with Passenger and Nginx.

##1. Domain settings First of all, let's go to the DNS settings within your domain settings on the website were you bought your Domain. I'm with TransIP and have to click this button after clicking my domain-name: enter image description here

Make a new record in your DNS with the following settings: enter image description here

This will point all prefixes before the domain name (sub-domains) to your server excluding the ones that you define in the same list. Make sure you fill in the whole server address in the value bar, not just the @ placeholder.

You can now (or in fact within a few hours, since your DNS needs to send your new configuration around) start to point new addresses in your Nginx.conf. Just write a new server { } block within /opt/nginx/conf/nginx.conf and include site_name yourdomain.yoursite.com Follow the bonus steps below for awesomeness!

#2. Bonus redirect non existing subdomains Want to point non-existing domains to your main site and have them see 404 pages when the content after the .com was wrong? edit your nginx settings in /opt/nginx/conf/nginx.conf with the following lines between http {}:

server {
    server_name *.yoursite.com
    return 301 http://www.yoursite.com$request_uri;
}

server {
    listen 80;
    server_name www.yoursite.com;
    ...
}

make sure you have a server_name www.yoursite.com in your options file were the redirect can point to. otherwise you would get a loop on your server...

So let's go over these things: server_name *.yoursite.com catches all incoming trafic with sub-domains except for the ones defined in the rest of your nginx.conf. return 301 http://www.yoursite.com$request_uri; will redirect the browser to www.yoursite.com and add the rest of the text /modelname/pagename behind it for evaluation on the Rails-app's routes.

Now safe the file and restart Nginx on your server to load the new settings. Try it out by messing with some sub-domain names

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment