Question

nginx https redirect www to non-www

Hi i was config my nginx block to redirect all www to non-www, this is my config:

server {
    listen       80;
    server_name  mysite.com www.mysite.com;
    return       301 https://mysite.com$request_uri;
}

server {
    listen       443 ssl http2;
    server_name  mysite.com;
    root   /usr/share/nginx/html;
	
	ssl on;
	ssl_certificate /ssl/ssl-bundle.crt;
	ssl_certificate_key /ssl/mysite.key;
	ssl_session_cache shared:SSL:20m;
	ssl_session_timeout 10m;
	ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
	ssl_prefer_server_ciphers on;
    ssl_ciphers ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:!DSS;
	ssl_buffer_size 8k;
	ssl_session_tickets off;
	
	ssl_stapling on;
	ssl_stapling_verify on;
	ssl_trusted_certificate /ssl/ssl-trusted.crt;
	resolver 8.8.8.8 8.8.4.4 valid=300s;
	resolver_timeout 10s;
	
	#add_header X-Content-Type-Options "nosniff";

    location / {
        index index.php index.html index.htm;
	try_files $uri $uri/ /index.php?$uri&$args;
    }
	
	location /internal_data/ {
        internal;
        allow 127.0.0.1;
        deny all;
        }

        location /library/ {
        internal;
        allow 127.0.0.1;
        deny all;
        }

    location ~ \.php$ {
	try_files $uri =404;
	fastcgi_pass 127.0.0.1:9000;
	fastcgi_index index.php;
	fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
	include fastcgi_params;
    }
	
    location ~* \.(?:ico|css|gif|jpe?g|js|png|svg|svgz|swf)(\?.+)?$ {
        access_log              off;
        log_not_found           off;
        expires                 1y;
    }

location ~ \.(jpe?g|png|gif)$ {
     valid_referers none blocked mysite.com *.mysite.com;
     if ($invalid_referer) {
        return   403;
        }
    }
}

working fine just for non-www to https but not work from www to non-www, this is result from curl -I http://www.mysite.com :

curl: (6) Couldn't resolve host 'www.mysite.com'

is there any iam miss? thank you.


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.

Accepted Answer

Check your DNS settings. You must have CNAME or A entry “www”.

www	A	_your_droplet_ip_

or

www	CNAME	domain.com

This didn’t solve my problem, so hopefully my solution will help others:

  1. Go to EC2 panel, and click on Load Balancer tab, and then on your load balancer;
  2. Make sure you have 2 listeners: one for HTTP and one for HTTPS (otherwise create those);
  3. Edit the rules for each one of them, adding a new rules for each that specifies your (www)(dot)<website>(dot)com to go to port 443 and to be redirected to <website>(dot)com;
  4. Having the rules for both HTTP and HTTPS to be redirected to 443 with no “WWW” will be working immediately after that.

Hopefully this helped you.

PS: Modifications in the EC2 will not be persisted as well since Elastic Beanstalk can instantiate new ones for your app, so keep this in mind.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel