我希望这样,当用户键入
task-manager.example.com
时,他们会自动重定向到 location /api/
,而无需键入 task-manager.example.com/api
的完整路径。只有task-manager.example.com
应该被重定向到location /api/
对于
example.com
和www.example.com
,无论用户是否键入location /
,我都希望将所有请求定向到example.com/api
。对 example.com/api
或 www.example.com/api
的任何请求都应自动重定向到 example.com/
或 www.example.com/
.
可以配置 Nginx 来实现这个功能吗?
这是我当前的配置:
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;
}
listen [::]:443 ssl ipv6only=on; # managed by Certbot
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
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
}