更换域名后,需要在老域名上,实现两个目的:
- 访问首页和SSL证书续订,本nginx处理。
- 访问除此之个的,全带URI跳转blog.t725.cn
Nginx
# 25/10/26 仅处理首页和SSL证书,其他跳转blog.t725.cn
server {
listen 80;
server_name t725.cn www.t725.cn;
access_log off;
return 301 https://t725.cn$request_uri;
}
server {
listen 443 ssl;
http2 on;
server_name www.t725.cn;
ssl_certificate cert/t725.cn-crt.pem;
ssl_certificate_key cert/t725.cn-key.pem;
access_log off;
return 301 https://t725.cn$request_uri;
}
server {
listen 443 ssl;
http2 on;
server_name t725.cn;
# SSL证书 + 日志文件配置 + 禁止特定UA访问
include custom/certConfig.conf;
include custom/logFileConfig.conf;
include custom/denyUA.conf;
root html;
index index.html;
# SSL证书验证: root是相对路径 alias 是绝对路径,验证程序会在root目录下创建[.well-known/acme-challenge/xxx]文件,以[y.z/.well-known/acme-challenge/xxx]访问。
location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
root certVerify_$host;
}
location = / { }
location = /index.html { }
location = /favicon.ico { }
location = /robots.txt { }
location / {
return 301 https://blog.t725.cn$request_uri;
}
}走的弯路配置:
Nginx
location = / { }
location / {
return 301 https://blog.t725.cn$request_uri;
}- 在浏览器里访问https://t725.cn,也被跳转到https://blog.t725.cn;看上去
location = / {没有匹配到,反而匹配到location / {了。 - 后配置
error_log logs/error.log debug;,再观察日志文件,发现:
2025/10/26 19:54:21 [debug] 4864#2932: *110081 http script var: "/"
2025/10/26 19:54:21 [debug] 4864#2932: *110081 http finalize request: 301, "/index.html?" a:1, c:2
逻辑上是匹配到location = / {,所以得到 /index.html,再就被匹配到location / {。所以,同时需要加上 location = /index.html {、location = /favicon.ico {、location = /robots.txt {。
发表回复