操作环境

  • 硬件:华为云服务器
  • 操作系统:Rocky Linux 9.0 64bit
  • 服务器软件:nginx 1.24.0,php 8.2.6,openssl 3.0.7,WordPress6.2.2

问题描述

我最近在调试WordPress网站时,访问网站主页,有时会自动下载随机命名的小文件,且不止一个浏览器(edge、firefox、chrome等)出现此现象,既有PC端也有手机端。但同时,有可以正常访问该网站的其他设备和浏览器。

查看下载的小文件,发现其文件内容就是网站根目录下的 index.php。下载文件的内容详情如下:

<?php
/**
 * Front to the WordPress application. This file doesn't do anything, but loads
 * wp-blog-header.php which does and tells WordPress to load the theme.
 *
 * @package WordPress
 */

/**
 * Tells WordPress to load the WordPress theme and output it.
 *
 * @var bool
 */
define( 'WP_USE_THEMES', true );

/** Loads the WordPress Environment and Template */
require __DIR__ . '/wp-blog-header.php';

问题原因

从我最近调试网站的情况来看,发现问题主要有两种情况:

  • 一是客户端的问题。
  • 二是http重定向配置问题。

解决方案

对于第一种情况,试着将各个设备上出现问题的浏览器的访问历史、内容清空,然后再重新访问上述网站,发现全部恢复正常,问题解决。

对于第二种情况,发现在直接输入 https://wslibai.com时访问正常,不加 https://前缀就会自动下载文件,这种情况是因为重定向配置不当所致。通过修改 nginx.conf文件中http的server块的内容,将所有请求重定向至https块,即可解决。具体修改如下:

server {
        listen       80;
        listen [::]:80 default_server;
        server_name _;
        return 301 https://$host$request_uri;
}

修改完成后,照例需要重启nginx服务,命令: systemctl restart nginx。重启nginx服务后问题解决。