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.
Asked
Active
Viewed 1,245 times
0
-
Each redirect needs the site name, so you have to duplicate at least some of the config. You can use include for some parts. – Tim Oct 25 '17 at 23:41
-
not true @Tim see answer – Jacob Evans Oct 25 '17 at 23:41
1 Answers
2
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;
}
}

Jacob Evans
- 7,886
- 3
- 29
- 57
-
-
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. – Tim Oct 26 '17 at 00:03
-
Oh I agree, I 444 anyone hitting things like the IP Address directly, etc. – Jacob Evans Oct 26 '17 at 00:05
-
-
-
I would suggest using the second option, naming URLs, rather than the catchall. – Tim Oct 26 '17 at 00:25