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

Become a contributor for community

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

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.