DNS Records Redirect www to non-www DNS Records Redirect www to non-www nginx nginx

DNS Records Redirect www to non-www


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.


You can direct www to non-www by adding a new server block to your nginx configuration file.

Step 1: Add the following server block to your nginx configuration file.

server {    server_name www.example.com;    return 301 $scheme://example.com$request_uri;}

Step 2: Restart nginx.

sudo systemctl restart nginx