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

部署WordPress+Nginx时遇ERR_TOO_MANY_REDIRECTS错误求助

解决Nginx+WordPress配置导致的ERR_TOO_MANY_REDIRECTS问题

问题根源分析

你的配置存在三处关键问题,共同引发了重定向循环:

1. Nginx server块逻辑冗余且顺序不规范

第一个处理example.com的server块,将return重定向指令放在listen端口指令之前,虽然Nginx能解析,但容易引发逻辑混淆;同时Certbot生成的80端口server块重复处理了HTTP重定向,导致重定向链的冗余,为循环埋下隐患。

2. WordPress未同步HTTPS站点配置

如果WordPress后台的**站点地址(URL)和WordPress地址(URL)**仍为HTTP协议,WordPress会自动发送重定向到HTTP,而Nginx又强制将HTTP重定向到HTTPS,形成无限循环。

3. FastCGI未传递HTTPS环境变量

你的PHP-FPM配置没有向WordPress传递当前连接的HTTPS状态,导致WordPress无法识别HTTPS环境,进而生成HTTP链接触发重定向。


修复步骤

步骤1:清理并规范Nginx配置

替换为以下简化后的配置,统一重定向逻辑:

upstream site1-php-handler {
    server unix:/var/run/php/php-fpm.sock;
}

# 所有HTTP请求直接重定向到HTTPS的www域名
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://www.example.com$request_uri;
}

# 非www的HTTPS请求重定向到www的HTTPS
server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    return 301 https://www.example.com$request_uri;
}

# 处理www的HTTPS请求,提供WordPress服务
server {
    listen 443 ssl;
    server_name www.example.com;

    root /var/www/html/example.com;
    index index.php index.html;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        # 添加HTTPS环境变量,告知WordPress当前使用HTTPS
        fastcgi_param HTTPS on;
        fastcgi_pass site1-php-handler;
    }

    access_log /var/log/nginx/example_access.log;
    error_log /var/log/nginx/example_error.log;
}

步骤2:强制WordPress使用HTTPS

若无法登录后台,修改/var/www/html/example.com/wp-config.php,添加以下两行:

define('WP_HOME','https://www.example.com');
define('WP_SITEURL','https://www.example.com');

步骤3:验证配置并重启服务

  1. 检查Nginx配置语法:
nginx -t
  1. 重启相关服务:
systemctl restart nginx
systemctl restart php-fpm

内容的提问来源于stack exchange,提问作者s.khan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 10:03:22