简体   繁体   中英

Nginx redirect https://www to https://

Thanks Richard for the Answer!

I have just set up my SSL certificates from Comodo on a Digitalocean Ubuntu rails server with Nginx.

At the moment http:// www.mydomain and http:// mydomain redirect to https:// mydomain

I also want https:// www.mydomain to redirect to https:// mydomain

I am very new to this and this is the current setup, I have 2 /etc/nginx/sites-enabled/ files( a .com and a .com.ssl file):

.com file

server {
        listen 80;
        server_name dspencer-webdesign.com;
        return 301 https://dspencer-webdesign.com$request_uri;
    }


server {
    listen 80;
    server_name dspencer-webdesign.com;


    passenger_enabled on;
    rails_env production;
    root /home/abfahren/david/current/public;

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

.com.ssl file

server {
        server_name www.dspencer-webdesign.com;
        return 301 $scheme://dspencer-webdesign.com$request_uri;
    }
    server {
        listen 443 ssl;
        server_name dspencer-webdesign.com;


        ssl_certificate /etc/ssl/ssl-bundle.crt;
        ssl_certificate_key /etc/ssl/dspencer-webdesign.com.key;

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


        passenger_enabled on;
        rails_env production;
        root /home/abfahren/david/current/public;

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

I am not sure how to get the functioning https:// www to redirect to https://dspencer-webdesign.com

Any help would be greatly appreciated Cheers

Your configuration is a little confusing. You have three non-SSL server containers, two of which have the same server_name . #1 redirects to SSL, #2 is never used, and #3 redirects to #1.

Your 4th server container is your one and only SSL server container, which makes it your default server for any connections to port 443.

I haven't tested this, but if you place this near the top of the SSL server block it should redirect https://www.dw.c to https://dw.c :

server {
  listen 443 ssl;
  server_name dspencer-webdesign.com;
  if ($server_name ~* \.dspencer-webdesign.com$) {
    return 301 $scheme://dspencer-webdesign.com$request_uri;
  }
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM