要在Nginx中设置三级域名,您需要完成以下几个步骤:
首先,您需要在DNS管理面板中添加三级域名的A记录或CNAME记录:
三级域名.二级域名.主域名. IN A 服务器IP地址
# 例如:
test.app.example.com. IN A 192.168.1.100
然后,在Nginx配置文件中为三级域名添加server块:
server {
listen 80;
server_name test.app.example.com; # 三级域名
# 如果需要HTTPS
# listen 443 ssl;
# ssl_certificate /path/to/cert.pem;
# ssl_certificate_key /path/to/key.pem;
# 根目录设置
root /var/www/test.app.example.com;
index index.html index.htm;
# 其他配置...
location / {
try_files $uri $uri/ =404;
}
# 可以添加更多location配置
}
server {
listen 80;
server_name test.app.example.com;
return 301 https://$host$request_uri;
}
如果您想支持任意三级域名,可以使用通配符:
server {
listen 80;
server_name *.app.example.com;
# 获取实际的三级域名部分
set $subdomain "";
if ($host ~* ^([^.]+)\.app\.example\.com$) {
set $subdomain $1;
}
root /var/www/$subdomain.app.example.com;
# 其他配置...
}
完成配置后:
# 测试配置语法是否正确
sudo nginx -t
# 重载Nginx配置
sudo systemctl reload nginx
这样您就成功设置了Nginx的三级域名配置。根据您的具体需求,可以进一步定制location块和其他配置参数。