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

会有一股神秘感。 提交于 2019-11-30 20:31:59

If you don't have a certificate for domain.com redirecting from https://domain.com to https://www.domain.com will not work, because before the browser gets the redirect it has to successfully establish the SSL connection (https is http inside SSL) and this fails because the certificate does not match.

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;

Provide a specific server block for the naked domain along with a general default. The more-specific ones will be used first.

    server {
            listen 80;
            listen [::]:80;
            listen 443 ssl http2;
            listen [::]:443 ssl http2;
            server_name example.com;

            return 301 https://www.$host$request_uri;
    }
    server {
            listen 80 default_server;
            listen [::]:80 default_server;

            return 301 https://$host$request_uri;
    }
    server {
            listen 443 ssl http2;
            listen [::]:443 ssl http2;
            server_name www.example.com;
            # omitting the rest for https://www.example.com
    }

I use Let's Encrypt for my certificates so something like the following for default_server prevents redirecting the ACME challenges (note the second wildcard server_name for handling all https://*.example.com which don't have their own server block).

    # omit server_name example.com block, same as above
    server {
            listen 80 default_server;
            listen [::]:80 default_server;

            location ~ ^/\.well-known/acme-challenge {
                # LetsEncrypt
                add_header Content-Type text/plain;
                expires 0;
                alias /var/www/html/acme/$host;
                break;
            }
            location ~ ^/(?!\.well-known/acme-challenge) {
                return 301 https://$host$request_uri;
            }
    }
    server {
            listen 443 ssl http2;
            listen [::]:443 ssl http2;
            server_name *.example.com;
            # omitting the rest for https://*.example.com
    }

Setup certificates for the naked example.com, www.example.com and any others:

sudo certbot certonly --manual -d example.com -d www.example.com -d abc.example.com

Following will help.

server {
            listen 80;
            listen [::]:80;
            server_name example.com www.example.com;


            return 301 https://www.example.com$request_uri;
    }

  server {
            listen 443 ssl http2;
            listen [::]:443 ssl http2;
            server_name example.com;
            return 301 https://www.example.com$request_uri;
   }

server {
            listen 443 ssl http2;
            listen [::]:443 ssl http2;
            server_name www.example.com;

   ====>>> Your site configuratioin Goes here <<======
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!