Nginx redirect http://www and naked http/https to https://www

前端 未结 4 1021
暖寄归人
暖寄归人 2021-01-02 12:16

I would like to redirect all traffic from the following domains:

  • http://domain.com
  • http://www.domain.com
  • http
4条回答
  •  抹茶落季
    2021-01-02 13:15

    I had similar kind of scenario and this is how I solved the redirection from

    https://domain.com -----> https://www.domain.com

       server {
          listen        443;
           server_name    domain.com;
             if ($host = domain.com) {
            rewrite ^(.*) https://www.domain.com:443$request_uri? permanent;
        }
    

    Hope this helps!

    Using if condition in nginx

    Directive if has problems when used in location context, in some cases it doesn't do what you expect but something completely different instead. In some cases it even segfaults. It's generally a good idea to avoid it if possible. The only 100% safe things which may be done inside if in location context are: return ...; rewrite ... last;

提交回复
热议问题