Search code examples
nginxhttpsurl-rewritingforge

Redirect https:// to https://www


I have a ubuntu server with DigitalOcean which is managed using forge.laravel.com

I have tried numerous different methods in the nginx.conf file to redirect all the possible scenarios to my domain https://www.domain.com

https://www.domain.com = https://www.domain.com -- GREAT
http://domain.com = https://www.domain.com -- GREAT
http://www.domain.com = https://www.domain.com -- GREAT
https://domain.com = https://domain.com -- This should redirect to the www.

heres part of my nginx.conf

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

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

    server {
        server_name domain.com;
        return 301 $scheme://www.domain.com$request_uri; 
    }

    server {
        listen 443 ssl;
        server_name domain.com;
        root /home/forge/default/public;

.... other nginx.conf configuration

Can anyone tell me what I'm doing wrong? I've tried numerous combinations.


Solution

  • Replace

    server {
        server_name domain.com;
        return 301 $scheme://www.domain.com$request_uri; 
    }
    
    server {
        listen 443 ssl;
        server_name domain.com;
        root /home/forge/default/public;
    

    By

    server {
        listen 443 ssl;
        server_name domain.com;
        return 301 https://www.domain.com$request_uri; 
        #INSERT HERE CERTIFICATES DIRECTIVES
    
    }
    
    server {
        listen 443 ssl;
        server_name www.domain.com;
        root /home/forge/default/public;