Question

Redirecting https www domain to non-www domain with NGINX.

I have been looking but I am unable to find a solution. I have set up SSL certificates for my domain and the http traffic redirects to https. What I would like is https://www.example.com to redirect to https://example.com.

Any help would be appreciated.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

You could achieve that with a configuration similar to below. In this case the final destination is passed to a back-end server.

server {
	listen 80;
	server_name example.com;
	
	location / {
		return 301 https://example.com/$request_uri;
	}
	
	location ~ /.well-known/acme-challenge/ {
    		default_type "text/plain";
    		root /usr/share/nginx/html;

    		allow all;
	}	
}

server {
	listen 80;
	server_name www.example.com;
	
	location / {
		return 301 https://example.com/$request_uri;
	}
	
	location ~ /.well-known/acme-challenge/ {
    		default_type "text/plain";
    		root /usr/share/nginx/html;

    		allow all;
	}	
}

server {
	listen 443 ssl http2;
	server_name www.example.com;
	
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_prefer_server_ciphers on;
	ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
	ssl_ecdh_curve secp384r1;
	
	ssl_dhparam /etc/nginx/dhparam.pem;
	
	ssl_session_cache shared:SSL:10m;
	ssl_session_tickets off;
	
	ssl_stapling on;
	ssl_stapling_verify on;
	
	resolver 1.1.1.1 8.8.8.8 8.8.4.4 valid=300s;
	resolver_timeout 5s;
	
	add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
	add_header X-Frame-Options SAMEORIGIN;
	add_header X-Content-Type-Options nosniff;

    
	location / {
		return 301 https://example.com/$request_uri;
	}		
}

server {
	listen 443 ssl http2;
	server_name example.com;
	
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_prefer_server_ciphers on;
	ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
	ssl_ecdh_curve secp384r1;
	
	ssl_dhparam /etc/nginx/dhparam.pem;
	
	ssl_session_cache shared:SSL:10m;
	ssl_session_tickets off;
	
	ssl_stapling on;
	ssl_stapling_verify on;
	
	resolver 1.1.1.1 8.8.8.8 8.8.4.4 valid=300s;
	resolver_timeout 5s;
	
	add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";
	add_header X-Frame-Options SAMEORIGIN;
	add_header X-Content-Type-Options nosniff;
    
	location / {
		proxy_set_header        Host $host;
		proxy_set_header 		X-Real-IP $remote_addr;
		proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header        X-Forwarded-Proto $scheme;
		proxy_set_header 		Upgrade $http_upgrade;
        proxy_set_header 		Connection 'upgrade';
		
		proxy_pass http://xxx.xxx.xxx.xxx;
	}		
}