Report this

What is the reason for this report?

DNS Networking redirects with Nginx to catch wildcards

Posted on May 18, 2017

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.

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.