父路径(Parent Path)是指在Web服务器中允许脚本访问其上级目录的能力。通常,出于安全考虑,Web服务器默认会限制脚本只能访问其所在的目录及其子目录,这种限制被称为“无父路径”或“禁止父路径”。
问题:启用父路径后,可能会增加安全风险,如目录遍历攻击。
原因:恶意用户可能利用父路径权限访问到未授权的文件或目录。
编辑httpd.conf
文件,找到以下行并修改:
<Directory />
Options FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
# 启用父路径
<IfModule mod_dir.c>
DirectoryIndex index.html index.php
AllowOverride All
Require all granted
</IfModule>
</Directory>
然后重启Apache服务。
编辑nginx.conf
文件,添加或修改以下配置:
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
index index.html index.php;
# 启用父路径
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
然后重启Nginx服务。
通过以上步骤,可以在确保安全的前提下合理利用父路径功能,提升网站开发和维护的效率。
领取专属 10元无门槛券
手把手带您无忧上云