Question

Cannot configure HTTPS for www subdomain using nginx server blocks

I want to stop this from happening https://www.site.com/articles/abc This should be redirected (to remove www) as in my server block configuration. I don’t understand why however. I have looked a lot at similar solutions on server fault and wonder if there is something more specific for DO.

here is the config thank you for help.

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

server {
        listen 443 ssl;
        listen [::]:443 ssl http2;
        include snippets/ssl-site.com.conf;
        include snippets/ssl-params.conf;

        root /var/www/site.com/html;

        index index.html index.htm index.nginx-debian.html index.php;

        server_name site.com www.site.com;
        #rewrite ^ https://site.com$request_uri? permanent;
        #return 301 https://$server_name$request_uri;

        add_header X-Frame-Options "SAMEORIGIN";

        rewrite ^(.*)/(\d+)-([^/]+)(.*)$ $1/$3 permanent;
        rewrite ^.*\.(pptx)$ https://caribbean-diving.com permanent;

        location ~* \.(?:css|js|gif|jpeg|jpg|png)$ {
                expires max;
                add_header Cache-Control public;
        }

        error_page 404 /404.html;
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
                root /usr/share/nginx/html;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }

        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        location ~ /.well-known {
                allow all;
        }

}
Show comments

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.

Kamal Nasser
DigitalOcean Employee
DigitalOcean Employee badge
February 20, 2018
Accepted Answer

This question was answered by @landed:

So now I can see I get a padlock for the www version as well. However the redirection still has issues- ie doesn’t actually redirect. So there is an seo problem rather than a nasty SSL problem but I want to solve the reason why the www is not redirected properly. I have another domain that is working sweet and the config looks identical so it is confusing.

Edit that other domain was using .htaccess to redirect!

So have now fixed them both with a separate server block to catch https://www variants…

View the original comment

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