You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx未配置rewrite规则 WordPress站点访问异常返回301重定向

问题现象

80端口部署的WordPress站点thecoachmasternetwork.com,访问根路径/、/index.php时会被301重定向到同服务器其他HTTPS站点,跳转目标既不是默认站点,也不是配置加载优先级最高的站点,异常仅在80端口访问时出现。
访问/license.txt等静态资源、自定义phpinfo测试文件可正常返回,PHP解析无异常,Nginx错误日志无相关报错记录,初步排查WordPress文件未发现明显异常,原有Certbot生成的HTTPS跳转配置块已全部注释。
对应站点Nginx配置如下:

ian@ianhobson:/etc/nginx/sites-enabled$ cat thecoachmasternetwork
# Statements for thecoachmasternetwork.com
#
# rewrites of http to https at bottom of file
server {

server_name thecoachmasternetwork.com www.thecoachmasternetwork.com;

listen 80;
fastcgi_read_timeout  300;

root /var/www/thecoachmasternetwork/htsecure;
index index.php;
fastcgi_index index.php;

access_log /var/log/nginx/thecoachmasternetwork.access.log;
# error_log  /var/log/nginx/error.log;
#
location = /favicon.ico {
    log_not_found off;
    access_log off;
}
location ~ \.user\.ini$ {
    deny all;
}
# disallow hot linking to images
location ~ .(gif|png|jpg|jpeg)$ {
    valid_referers none blocked thecoachmasternetwork.com www.thecoachmasternetwork.com;
    if ($invalid_referer) {
        return 403;
    }
}
# serve static files that exist
location / {
    try_files $uri $uri/ /index.php?$args;
}
# send .php files to fastcgi if file exists
location ~ \.php$ {
   # Zero-day exploit defence
   try_files $uri =404;
   fastcgi_split_path_info ^(.+\.php)(/.+)$;
   fastcgi_param SCRIPT_FILENAME $document_root@fastcgi_script_name;
   fastcgi_param PATH_INFO       $fastcgi_path_info;
   include /etc/nginx/fastcgi.conf;
   fastcgi_pass 127.0.0.1:9000;
 }

}

    # redirects from http -> https (已注释)
    #server {
    #    if ($host = www.thecoachmasternetwork.com) {
    #        return 301 https://$host$request_uri;
    #    } # managed by Certbot
    
    
    #    if ($host = thecoachmasternetwork.com) {
    #        return 301 https://$host$request_uri;
    #    } # managed by Certbot
    
    
    #    server_name thecoachmasternetwork.com www.thecoachmasternetwork.com;
    #    listen 80;
    #    return 404; # managed by Certbot
    
    #}
    ian@ianhobson:/etc/nginx/sites-enabled$
排查方向(按概率从高到低排序)
  • 优先排查PHP层跳转:静态资源访问完全正常,只有走FastCGI解析的/index.php触发跳转,基本可以先排除Nginx本身配置问题,重点查WordPress逻辑:
    1. 直接查站点根目录wp-config.php里的WP_SITEURL、WP_HOME常量值,大概率是这两个值被误配置成了跳转目标的HTTPS域名。WordPress初始化时如果这两个配置指向其他域名,会自动对入口路径、首页做301跳转,静态资源由Nginx直接返回不会触发该逻辑,和当前现象完全吻合。
    2. 检查wp-content/mu-plugins目录下是否存在强制跳转类的必须启用插件,或者当前主题functions.php中是否写死了域名跳转逻辑,这类代码优先级高于后台站点地址设置,即使在后台改了站点地址也不会生效。
    3. 直接在服务器本机执行curl命令绕过Nginx测PHP返回,确认跳转是否来自PHP层:
      curl -I -H "Host: thecoachmasternetwork.com" http://127.0.0.1:9000/index.php
      
      如果返回头里带Location字段指向跳转目标站点,直接坐实是PHP层问题。
  • 其次排查Nginx配置加载异常:
    1. 不要只看单个站点配置文件,执行nginx -T全量打印Nginx当前实际加载的所有配置,确认conf.d/目录、其他站点配置中是否存在误写了当前域名的server_name、全局rewrite规则匹配了当前请求。
    2. 执行nginx -t校验配置语法,确认不存在未闭合配置块、语法错误导致配置解析异常,请求错误落到其他server块。
      注:当前贴出的配置中fastcgi_param SCRIPT_FILENAME $document_root@fastcgi_script_name;存在明显笔误,@应为变量连接符,正确写法是$document_root$fastcgi_script_name,该错误会导致PHP文件找不到路径,和测试PHP文件可正常访问的描述矛盾,需确认贴出的配置和服务器实际运行配置完全一致。
  • 最后排查缓存/前置代理问题:
    1. 用浏览器隐私模式、加随机查询参数(比如/?rand=xxxx)请求,排除浏览器本地301缓存影响,301是永久重定向,浏览器会长期缓存之前的跳转结果。
    2. 在服务器本机执行curl请求本地80端口,绕过外层CDN、WAF、反向代理,确认是否还有跳转:
      curl -I -H "Host: thecoachmasternetwork.com" http://127.0.0.1/
      
      如果本机请求不跳转,说明跳转来自前端代理层,和本机Nginx、WordPress配置无关。

内容的提问来源于stack exchange,提问作者Ian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 20:51:07