WordPress配置Nginx 302重定向后主域名出现404错误求助
解决Nginx配置WordPress重定向后主域名404的问题
问题原因
你新建的独立server块只配置了/lps的重定向规则和基础的try_files,但缺少WordPress运行必需的核心配置:
- 未指定WordPress根目录(
root指令),Nginx找不到网站文件的存放路径 - 缺少PHP解析规则,即便
try_files匹配到index.php,也无法交给PHP-FPM执行处理
推荐解决方案:复用原有WordPress Server块
不要新建独立的server块,直接把/lps的重定向规则加到你原本运行正常的WordPressserver块中,既能保留所有原有配置,又能实现重定向需求:
server { listen 80; server_name domain.com; # 你的WordPress根目录,根据实际路径修改 root /var/www/wordpress; index index.php index.html index.htm; # 新增/lps目录的302重定向规则 location /lps { return 302 https://newdomain/$request_uri; } # WordPress核心路由规则 location / { try_files $uri $uri/ /index.php$is_args$args; } # PHP解析规则,根据你的PHP版本调整socket路径 location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; } # 禁止访问隐藏文件(可选但推荐) location ~ /\.ht { deny all; } }
备选方案:补全独立Server块的必要配置
如果你坚持使用独立的server块,必须补全所有WordPress运行的必需配置:
server { listen 80; server_name domain.com; # 必须指定WordPress根目录 root /var/www/wordpress; index index.php index.html index.htm; location /lps { return 302 https://newdomain/$request_uri; } location / { try_files $uri $uri/ /index.php$is_args$args; } # 必须添加PHP解析规则 location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php8.1-fpm.sock; } location ~ /\.ht { deny all; } }
配置修改完成后,重启Nginx服务让配置生效:
sudo systemctl restart nginx
内容的提问来源于stack exchange,提问作者Mike Henriquez
相关产品推荐
相关产品推荐

