I have a website, that could have unknown amount of CNAME DNS records linked to it. What I need is to have ANY http requests to be redirected to https. We will be using a default certificate. No matter if it IP address or DNS name typed in the browser - all need to be forwarded to https
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
return 301 https://$host$request_uri;
I have tried $server_name
as well.. If I go to http:// it redirects me to "https://_". Is there any equivalent to RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
in Apache?
It seems that nginx is having equivalent to HTTP_HOST in Apache. So this works for me:
The proper way to do a Nginx redirect is with HTTP 301, not a rewrite. See here.
As far as your server setup goes, see mine below. I define the server name on port 80, then redirect to HTTPS on port 443. Everything (SSL settings, root, index, etc...) is applied under this HTTPS server block.