DNS Records Redirect www to non-www

我是研究僧i 提交于 2019-11-28 01:55:00

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!