Redirect www and http requests to https://, catching all domains, in nginx Redirect www and http requests to https://, catching all domains, in nginx nginx nginx

Redirect www and http requests to https://, catching all domains, in nginx


nginx manual: A redirect to a main site

server {    listen   80;    listen   [::]:80;    server_name www.example.com;    return 301 https://example.com$request_uri;}server {    listen   443 default_server ssl;    server_name www.example.com example.org;    ssl_certificate        /path/to/my/cert;    ssl_certificate_key  /path/to/my/key;}

don't lose “everything else”:

server {    listen       80 default_server;    server_name  _;    return       301 http://example.com$request_uri;}

more efficient, by only running the rewrite on the http protocol it avoids having to check the $scheme variable on every request (IfIsEvil)

Maybe you can try use in one place 80/443:

server {    listen   80;    listen   [::]:80;    listen   443 default ssl;    server_name www.example.com;    ssl_certificate        /path/to/my/cert;    ssl_certificate_key  /path/to/my/key;    if ($ssl_protocol = "") {       rewrite ^   https://$server_name$request_uri? permanent;    }}