How to redirect every vhost to https but without duplicating configuration. I have 4 websites on the same vps running Nginx and I want to redirect everything to https.
1 Answer
Like this...
Redirect ALL requests to https In catch-all server examples the strange name “_” can be seen
server {
listen 80;
listen [::]:80;
server_name _;
access_log /var/log/nginx/www-301_access.log;
error_log /var/log/nginx/www-301_error.log;
location / {
return 301 https://$host$request_uri;
}
}
Redirect specific domain names
server {
listen 80;
listen [::]:80;
server_name example.com *.example.com example.org *.example.org;
access_log /var/log/nginx/www-301_access.log;
error_log /var/log/nginx/www-301_error.log;
location / {
return 301 https://$host$request_uri;
}
}
-
-
I prefer not to use the default server for this. I return an error status code so that only requests for configured domains are serviced. This is a solution, and there's nothing wrong with it, but it's not one I'd use personally.– TimOct 26, 2017 at 0:03
-
Oh I agree, I 444 anyone hitting things like the IP Address directly, etc. Oct 26, 2017 at 0:05
-
-