Nginx: Redirect both http and https root to subdirectory
This configuration will do what you want:
server {
listen 80;
server_name example.com;
return 301 https://$server_name/subdirectory;
}
server {
listen 443;
server_name example.com;
location = / {
return 301 https://$server_name/subdirectory;
}
}
The = /
specifiers means a full match, so it matches only the exact root URI of the virtual server.