non-www redirect not working

March 31, 2017 496 views
Nginx LEMP Ubuntu 16.04

Non-www redirect to www is not working on my server. Here is the following nginx configuration that I'm using. When I try on mobile or on a desktop browser it doesn't connect and says webpage is unavailable. In mobile gives an DNS look-up failed.

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name example.ca;
        return 301 https://www.example.ca$request_uri;
}

server {

        server_name www.example.ca;


         listen 443 ssl http2 default_server;
         listen [::]:443 ssl http2 default_server;
         include snippets/ssl-www.example.ca.conf;
         include snippets/ssl-params.conf;

.
.


1 Answer

Have you setup a DNS an A-record for non-www also known as @ ?
And since you're using HTTPS, then you might want to redirect non-www for HTTPS too. If your certificate allows the non-www of course. And then we want to redirect www to HTTPS as well.

server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name example.ca www.example.ca;
        return 301 https://www.example.ca$request_uri;
}
server {
        listen 443 ssl http2 default_server;
        listen [::]:443 ssl http2 default_server;
        include snippets/ssl-www.example.ca.conf;
        include snippets/ssl-params.conf;
        server_name example.ca;
        return 301 https://www.example.ca$request_uri;
}
Have another answer? Share your knowledge.