Redirecting non-www to www with Nginx on Ubuntu


I prefer to make my website to www rather than non-www. To set it up is really easy. My server use Ubuntu and Nginx and running Let's Encrypt SSL.

I make a server block that handles redirection request. And then other server block to handle the real request.

Here is my config file:

server {                                                                                                                                             
    listen 80;                                                                                                                                       
    listen [::]:80;                                                                                                                                  
    listen 443 ssl;                                                                                                                                  
    server_name waysquare.com;                                                                                                                       
                                                                                                                                                     
    ssl_certificate /etc/letsencrypt/live/waysquare.com/fullchain.pem; # managed by Certbot                                                          
    ssl_certificate_key /etc/letsencrypt/live/waysquare.com/privkey.pem; # managed by Certbot                                                        
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot                                                                            
                                                                                                                                                     
    return 301 $scheme://www.$server_name$request_uri;                                                                                               
}                                                                                                                                                    

server {
    listen 80;
    listen [::]:80;

    server_name www.waysquare.com;
    root /var/www/waysquare.com/html/system/nginx-root;

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass http://127.0.0.1:2368;

    }

    location /shell {                                                                                                                                
        proxy_pass https://www.waysquare.com:6116;                                                                                                   
    }                                                                                                                                                
                                                                                                                                                     
    location ~ /.well-known {                                                                                                                        
        allow all;                                                                                                                                   
    }                                                                                                                                                
                                                                                                                                                     
    client_max_body_size 50m;                                                                                                                        
                                                                                                                                                     
    listen 443 ssl; # managed by Certbot                                                                                                             
    ssl_certificate /etc/letsencrypt/live/waysquare.com/fullchain.pem; # managed by Certbot                                                          
    ssl_certificate_key /etc/letsencrypt/live/waysquare.com/privkey.pem; # managed by Certbot                                                        
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot                                                                            
                                                                                                                                                     
                                                                                                                                                     
                                                                                                                                                     
    if ($scheme != "https") {                                                                                                                        
        return 301 https://$server_name$request_uri;                                                                                                    
    } # managed by Certbot                                                                                                                           
                                                                                                                                                     
                                                                                                                                                     
    # Redirect non-https traffic to https                                                                                                            
    # if ($scheme != "https") {                                                                                                                      
    #     return 301 https://www.$host$request_uri;                                                                                                  
    # } # managed by Certbot                                                                                                                         
                                                                                                                                                     
}                                                                                                                                                    

Then you can test it with this command:

curl -I http://waysquare.com

Below is the result. My site will be redirected to https://www.waysquare.com

curl -I http://waysquare.com
HTTP/1.1 301 Moved Permanently
Server: nginx/1.10.3 (Ubuntu)
Date: Sun, 17 Dec 2017 19:29:01 GMT
Content-Type: text/html
Content-Length: 194
Connection: keep-alive
Location: https://www.waysquare.com/
Redirecting non-www to www with Nginx on Ubuntu
Share this