0

I'm trying to get my site so that every variation of domain.com will redirect to https://www.domain.com. I'm using a combination of tools to do so .. One is the NGINX config file which begins with:

server {
            listen  80;
            server_name domain.com;
            return       301 https://www.domain.com$request_uri;
    }
server {
            listen          80;
            server_name     localhost www.domain.com;
            root            /usr/local/nginx/html;

I've also got a rule set up in my CDN to proxy all content through HTTPS:

if ( $scheme = http ) { 
  REWRITE RULE ^ https://$http_host$request_uri permanent 
} 

Almost every variation works, except https://domain.com .. it should redirect to https://www.domain.com, but it doesn't load.

heatherthedev
  • 177
  • 1
  • 2
  • 12

2 Answers2

0

An HTTP connection using another port rather than the standard 80, should listen the port 443. From wikipedia:

HTTPS URLs begin with "https://" and use port 443 by default, whereas HTTP URLs begin with "http://" and use port 80 by default.

See also this: nginx HTTPS serving with same config as HTTP. From this page:

server {
    listen 80;
    listen 443 default_server ssl;

    # other directives
}

Also this other page has a http / https solution: Nginx no-www to www and www to no-www

Community
  • 1
  • 1
dawez
  • 2,714
  • 2
  • 29
  • 50
0

Something like this may help:

server {  # redirect _all_ http requests to https server
    listen 80;
    return  301 https://www.domain.com$request_uri;
}

server {
    listen 443 ssl;
    server_name localhost .domain.com ;
    root    /usr/local/nginx/html;

    if ($host = domain.com) {
        return  301 https://www.domain.com$request_uri;
    }
}
Cole Tierney
  • 9,571
  • 1
  • 27
  • 35