Question

Nginx redirects. Http to https and www to non-www

I need some help with my nginx config so all my http traffic goes to https and also www changes to non-www. My server is running ubuntu 14.04 and nginx. Also my site has cloudflare activated. My goal is that everyone goes to this url https://nicholaschedid.com because my cert if for nicholaschedid.com

here is my sites-available .conf file

server {
        server_name nicholaschedid.com;
        return 301 https://$server_name$request_uri;
}

server {
        listen 443 ssl;
        listen [::]:443 ssl;
        server_name nicholaschedid.com;

        ssl on;
        ssl_certificate /etc/nginx/ssl/bundle.crt;
        ssl_certificate_key /home/worker/nicholaschedid.com.key;

        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        ssl_ciphers AES256+EECDH:AES256+EDH:!aNULL;

        root /var/www/nicholaschedid.com/html/_site;
        index index.php index.html index.htm;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
                # Uncomment to enable naxsi on this location

                [. . .]

So far

I tried so many different configuration but none worked, from both google and here.

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.

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.

Try something like this: Create a new conf file like redirec.conf and add:

server {
    listen      80;
    server_name www.domain.com; #or server_name .domain.com; for wildcard subdomains

    location / {
        if ($host !~* ^(www)) {
          rewrite ^/(.*)$ https://www.domain.com/$1 permanent;
        }
    }
}

Hello! I this should work :) Feel free to tell me if you don’t understand something of this configuration file! I prefer using rewrites for stuff like that, but there’s not a really big difference - in this case -between using rewrite or return.

server {
    listen 80;

    server_name nicholaschedid.com www.nicholaschedid.com;
    rewrite ^/(.*) https://nicholaschedid.com/$1 permanent;
}

server {
    listen 443 ssl;

    server_name www.nicholaschedid.com;
    ssl_certificate /etc/nginx/ssl/bundle.crt;
    ssl_certificate_key /home/worker/nicholaschedid.com.key;

    rewrite ^/(.*) https://nicholaschedid.com/$1 permanent;
}

server {
    listen 443 ssl;

    server_name nicholaschedid.com;


    ssl_certificate /etc/nginx/ssl/bundle.crt;
    ssl_certificate_key /home/worker/nicholaschedid.com.key;

    [ stuff goes here ]
}

Have a nice day, Alfio.

@nicholasfc Mhh, that’s strange. Can you try to temporally disable Cloudflare?