Nginx与PHP配置问题:CSS资源被拦截显示BLOCKED
问题解决方法
核心原因
CSS请求被路由到了index.php,执行IP与哈希校验逻辑时,因请求的不是有效哈希值,被接口判定为拦截,返回BLOCKED!。而JS、图片正常是因为它们的请求未触发index.php的校验逻辑(文件存在时被Nginx直接返回)。
解决方案
方案1:通过Nginx优先处理静态资源(推荐)
修改Nginx配置,添加专门匹配静态资源的location规则,放在所有其他location之前,确保静态资源请求不会进入index.php:
server { listen 80; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; server_name localhost; # 新增:优先处理静态资源,直接返回文件或404 location ~* \.(css|js|png|jpg|jpeg|gif|ico|svg)$ { try_files $uri =404; } location / { try_files $uri $uri/ /index.php?$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; } location ~ /\.ht { deny all; } location ~ ^/([a-fA-F0-9]+) { rewrite ^/([a-fA-F0-9]+) /index.php?id=$1 last; } }
配置修改后,重启Nginx生效:
sudo systemctl restart nginx
方案2:在PHP代码中排除静态资源请求
如果无法修改Nginx配置,可在index.php开头添加逻辑,直接处理静态资源请求,跳过校验:
<?php // 新增:处理静态资源请求,跳过校验逻辑 $staticExts = ['css', 'js', 'png', 'jpg', 'jpeg', 'gif', 'ico', 'svg']; $pathInfo = pathinfo($_SERVER['REQUEST_URI']); if (isset($pathInfo['extension']) && in_array(strtolower($pathInfo['extension']), $staticExts)) { $filePath = $_SERVER['DOCUMENT_ROOT'] . $_SERVER['REQUEST_URI']; if (file_exists($filePath)) { header("Content-Type: " . mime_content_type($filePath)); readfile($filePath); exit; } else { header("HTTP/1.1 404 Not Found"); exit; } } // 原有的代码继续 require_once('_admin/config.php'); // ... 以下保持原代码不变 ?>
额外检查点
- 确认CSS文件存在于
/var/www/html(或对应子目录)下,路径与页面中的引用一致; - 页面中CSS的引用尽量使用绝对路径(如
/css/style.css),避免相对路径导致的404请求触发index.php逻辑。
内容的提问来源于stack exchange,提问作者Fake
相关产品推荐
相关产品推荐

