By ariziragoran
What i want to do:
Desktop version: mysite.com Mobile version: m.mysite.com
/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
mysite.com --> mysite.com (GOOD)
www.mysite.com --> mysite.com (GOOD)
test.mysite.com --> mysite.com (GOOD)
test.www.mysite.com --> mysite.com (GOOD)
www.test.mysite.com --> mysite.com (GOOD)
m.mysite.com/ --> http://m.mysite.com/ (GOOD)
www.m.mysite.com --> m.mysite.com (GOOD)
test.m.mysite.com --> mysite.com (WRONG), should redirect to m.mysite.com
test.www.m.mysite.com --> mysite.com (WRONG), should redirect to m.mysite.com
www.test.m.mysite.com --> mysite.com (WRONG), should redirect to m.mysite.com
test.m.test.mysite.com -> m.mysite.com (GOOD)
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.
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!
Accepted Answer
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.
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.