【发布时间】:2023-02-25 05:55:47
【问题描述】:
我希望这样当用户键入 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;
}
# 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
}
【问题讨论】:
标签: nginx nginx-reverse-proxy nginx-config nginx-location