I would like it so that when a user types task-manager.example.com
, they are automatically redirected to location /api/
without having to type out the full path of task-manager.example.com/api
. Only task-manager.example.com
should get redirected to location /api/
For example.com
and www.example.com
, I want all requests to be directed to location /
regardless of whether the user types example.com/api
or not. Any requests to example.com/api
or www.example.com/api
should be automatically redirected to example.com/
or www.example.com/
.
Can Nginx be configured to achieve this functionality?
Here is my current configuration:
server
{
server_name example.com www.example.com task-manager.example.com;
location /
{
# Frontend application
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:9091;
}
location /api/
{
# Backend application
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://localhost:9090;
}
# some Certbot SSL configuration ...
}
server
{
if ($host = www.example.com) {
return 301 https://$host/;
} # managed by Certbo
if ($host = example.com) {
return 301 https://$host/;
} # managed by Certbo
if ($host = task-manager.example.com) {
return 301 https://$host/api;
} # managed by Certbo
listen 80 default_server;
listen [::]:80 default_server;
server_name example.com www.example.com task-manager.example.com;
return 404; # managed by Certbot
}