Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

nginx - DNS Records Redirect www to non-www

I'm using Namecheap Domains and Vultr Hosting.

I'm trying to redirect DNS www to non-www.

www.example.com to example.com


I contacted Vultr and asked how to do this with their DNS Manager, they said they would not help as it is self-managed. So I contacted Namecheap, they said they would not help becuase they don't have access to Vultr's DNS Manager, would not tell me if the records I showed them are correct, and I would need to contact Vultr. So I am in an endless support loop.


Vultr DNS Manager

I followed this answer on how to set up a CNAME to redirect to non-www.

Type???| Name?|?Data?????????|?Seconds
--------------------------------------
A????? |???? ?|?ipv4 address?|?300
AAAA  ?|???? ?| ipv6 address | 300
CNAME ?|?.?? ?|?example.com??|?300
CNAME ?| www  |?example.com??|?300

After waiting all night for it to propgate, the www can still be visited and does not redirect.

It does not allow me to make another A record, only CNAME. It says:

Unable to add record: A CNAME record is not allowed to coexist with any other data. 

NGINX

I followed this guide and tried redirecting it with sites-available config. Http and Https work, but www does not redirect to non-www.

server {
    # Redirect http to https
    listen 80;
    return 301 https://$host$request_uri;
}

server {
    # Redirect www to non-www
    server_name www.example.com;
    return 301 $scheme://example.com$request_uri;
}

server {
    listen 443 ssl default_server;

    ssl on;
    ssl_certificate /etc/nginx/ssl/cert_chain.crt;
    ssl_certificate_key /etc/nginx/ssl/example_com.key;
    ssl_protocols  TLSv1.1 TLSv1.2;

    server_name example.com;
    ...
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

DNS cannot redirect your www site to non-www. The only purpose of DNS is to point both www and non-www to your server's IP address using A, AAAA or CNAME records (it makes little difference). The nginx configuration is responsible for performing the redirect from www to non-www.

Your second server block is intended to redirect from www to non-www, but currently only handles http connections (on port 80).

You can move the default server and use that to redirect everything to the intended domain name. For example:

ssl_certificate /etc/nginx/ssl/cert_chain.crt;
ssl_certificate_key /etc/nginx/ssl/example_com.key;

server {
    listen 80 default_server;
    listen 443 ssl default_server;
    return 301 https://example.com$request_uri;
}

server {
    listen 443 ssl;
    server_name example.com;
    ...
}

Assuming that you have a common certificate for both the www and non-www domain names, you can move the ssl_ directives into the outer block and allow them to be inherited into both server blocks (as shown above).

See this document for more.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...