我希望这样,当用户键入task-manager.example.com
时,他们会自动重定向到location /api/
,而不必键入task-manager.example.com/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
}
1条答案
按热度按时间5fjcxozz1#
我可以通过将
if conditions
移动到相关的location blocks
来解决我的问题。这允许我为特定的主机指定重定向到特定的URI。这是我的新配置: