NGINX Redirect http to https and non-www to ww

前端 未结 4 1427
闹比i
闹比i 2020-11-29 23:43

I\'m setting up an nginx server with an SSL.

The domain with the ssl is www.mydomain.com

I want to redirect all requests from:

http://mydomain.com, h

4条回答
  •  無奈伤痛
    2020-11-30 00:26

    I am late, But you can do like this

    server{
      listen 443 ssl;
      server_name www.mydomain.com;
      root /www/mydomain.com/;
    
      ssl    on;
      ssl_certificate /ssl/domain.crt;
      ssl_certificate /ssl/domain.key;
      .
      . 
      .
    }
    
    server{
      listen 80;
      server_name www.mydomain.com mydomain.com;
      return 301 https://$server_name$request_uri;
    }
    
    server{
      listen 443;
      server_name mydomain.com;
      return 301 https://www.$server_name$request_uri;
    }
    

    Or you can replace return 301 https://www.$server_name$request_uri; with rewrite ^ http://www.$server_name$request_uri? permanent;, both will work.

    You also need to set this in google webmaster for better SEO.

提交回复
热议问题