DNS Networking redirects with Nginx to catch wildcards

May 18, 2017 891 views
Networking DNS Nginx Ubuntu 16.04

What i want to do:

  1. Have two versions of the site, mobile and desktop.
  2. Force naked URI, no www visible and redirect to appropriate mobile/desktop version.
  3. Wildcard to catch all other gibberish and redirect to appropriate mobile/desktop version.

Desktop version: mysite.com
Mobile version: m.mysite.com

Network DNS Screenshot

/etc/nginx/sites-available/mysite.com

server {
        listen 80;
        listen [::]:80;
        server_name www.mysite.com;

        return 301 http://mysite.com;
}

server {
        listen 80;
        listen [::]:80;

        root /var/www/mysite.com/html;

        index index.html index.htm index.nginx-debian.html;

        server_name mysite.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

/etc/nginx/sites-available/m.mysite.com

server {
        listen 80;
        listen [::]:80;
        server_name www.m.mysite.com;

        return 301 http://m.mysite.com;
}

server {
        listen 80;
        listen [::]:80;

        root /var/www/m.mysite.com/html;

        index index.html index.htm index.nginx-debian.html;

        server_name m.mysite.com;

        location / {
                try_files $uri $uri/ =404;
        }
}

What I have so far

So i've mostly got it working, except there seems to be some issue with the wildcard redirects for the mobile version. I'd appreciate any help/feedback.

1 Answer
jtittle1 May 18, 2017
Accepted Answer

@ariziragoran

When it comes to DNS WildCards, you'll need to set them up using an A entry, which would look like:

A          *          DROPLET_IP

or

A          *.m        DROPLET_IP

The first DNS entry catches anything at the first level -- www, test, forum.domain.com, etc. The second works much like the first, though it catches anything targeting m.domain.com, so:

test01.m.domain.com
test02.m.domain.com
etc

With NGINX, you would then specify the WildCard in the server_name, for example:

server_name domain.com www.domain.com *.domain.com

or

server_name m.domain.com www.m.domain.com *.m.domain.com;

The * entry tells NGINX to handle the WildCard requests, but it will only do so if the DNS is properly setup as noted above. Without the WildCard DNS, NGINX won't handle the requests as expected.

  • Thanks for the reply! I'll try that right now, but then doesn't that mean that the digital ocean tutorial is wrong?

    How To Set Up a Host Name with DigitalOcean

    This article walks you through setting up the DNS for your site. The tutorial includes steps to set up an A records, point your nameservers, and provides some information about MX records.
  • Got it working, thanks a lot! I have one last question please. I have a domain.com and a domain.ca

    Currently form google domains management, my domain.ca is set to forward/redirect to domain.com. I was wondering, how do i get test.domain.ca to redirect properly to domain.com and test.m.domain.ca to redirect to domain.com.

    Would i add a new domain to the Network tab and redirect from there?

    • @ariziragoran

      In regards to the tutorial you linked to, no, not specifically.

      When it comes to DNS, you'll need to use a WildCard for every level you want to handle in the way you requested.

      That means if, for some reason, you wanted to also handle:

      test.sub.m.domain.com
      

      You'd need an A entry to handle that third level too, which would look like:

      A          *.*.m      DROPLET_IP
      

      You'd also have to create another NGINX server block that would handle the extra case.

      server_name *.*.m.domain.com;
      

      etc.

      As far as the CNAME entries go, you can use a CNAME for www or you can use an A entry. Both are valid, both will work. The main difference is that an A entry needs to point to an IP where a CNAME needs to point to a host, which is why www points to your domain instead of your Droplet IP.

      ...

      As far as handling the second domain, you'd need to change the DNS on the domain to use DigitalOcean, add it via the networking tab, and then proceed to set the domains and/or sub-domains to the same IP of the Droplet that you're using for the other domain.

      You'd then create the DNS entries as required and setup server blocks to handle requests coming from the new domain.

Have another answer? Share your knowledge.