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;
    return       301$request_uri;

server {
    listen       443 ssl http2;
    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_buffer_size 8k;
	ssl_session_tickets off;
	ssl_stapling on;
	ssl_stapling_verify on;
	ssl_trusted_certificate /ssl/ssl-trusted.crt;
	resolver 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/ {
        deny all;

        location /library/ {
        deny all;

    location ~ \.php$ {
	try_files $uri =404;
	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 *;
     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 :

curl: (6) Couldn't resolve host ''

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_



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

Featured on Community

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