gpt4 book ai didi

将未定义的子域重定向到 nginx 中的主域?

转载 作者:行者123 更新时间:2023-12-02 04:56:07 24 4
gpt4 key购买 nike

我想将未定义的子域重定向到我的主域。所以我有一个站点 domain.com,我有 2 个子域:sub1.domain.com 和 sub2.domain.com。我想要的是,当有人试图访问 sub4.domain.com 时,它在 nginx 中不匹配并将其重定向到 domian.com。这能实现吗?

最佳答案

这将重定向任何子域请求。对于所有现有的子域,您必须添加 server 条目。

# this will catch all subdomains and redirect to main domain.
server {
server_name *.domain.com;
return 301 http://domain.com$request_uri;
}

# this block will be used for sub1.domain.com;
server {
server_name sub1.domain.com;
...
}

# this is for sub2.domain.com;
server {
server_name sub2.domain.com;
...
}

# and so on...

nginx 不知道哪些子域存在哪些不存在。所以你必须明确命名所有这些。

关于将未定义的子域重定向到 nginx 中的主域?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22169881/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com