45IT.COM- 电脑学习从此开始!
DIY硬件教程攒机经验装机配置
设计Photoshop网页设计特效
系统注册表DOS系统命令其它
存储主板显卡外设键鼠内存
维修显卡CPU内存打印机
WinXPVistaWin7unix/linux
CPU光驱电源/散热显示器其它
修技主板硬盘键鼠显示器光驱
办公ExcelWordPowerPointWPS
编程数据库CSS脚本PHP
网络局域网QQ服务器
软件网络系统图像安全
页面导航: 首页 > 电脑学院 > 服务器技术 >

nginx+php使用open_basedir限制站点目录防止跨站

电脑软硬件应用网 45IT.COM 时间:2013-12-21 23:19 作者:佚名
以下三种设置方法均需要PHP版本为5.3或者以上。

方法1)在Nginx配置文件中加入
fastcgi_param  PHP_VALUE  "open_basedir=$document_root:/tmp/:/proc/";
通常nginx的站点配置文件里用了include fastcgi.conf;,这样的,把这行加在fastcgi.conf里就OK了。
如果某个站点需要单独设置额外的目录,把上面的代码写在include fastcgi.conf;这行下面就OK了,会把fastcgi.conf中的设置覆盖掉。
这种方式的设置需要重启nginx后生效。


方法2)在php.ini中加入:
[HOST=www.45its.com]
open_basedir=/home/www/www.45its.com:/tmp/:/proc/
[PATH=/home/www/www.45its.com]
open_basedir=/home/www/www.45its.com:/tmp/:/proc/
这种方式的设置需要重启php-fpm后生效。

方法3)在网站根目录下创建.user.ini并写入:
open_basedir=/home/www/www.45its.com:/tmp/:/proc/
这种方式不需要重启nginx或php-fpm服务。安全起见应当取消掉.user.ini文件的写权限。
关于.user.ini文件的详细说明:
http://php.net/manual/zh/configuration.file.per-user.php

设置open_basedir的同时最好禁止下执行命令的函数,比如:
shell_exec('ls /etc')仍然查看到/etc目录的文件列表
shell_exec('cat /etc/passwd')仍可查看到/etc/passwd文件的内容

建议禁止的函数如下:
disable_functions = pcntl_alarm, pcntl_fork, pcntl_waitpid, pcntl_wait, pcntl_wifexited, pcntl_wifstopped, pcntl_wifsignaled, pcntl_wexitstatus, pcntl_wtermsig, pcntl_wstopsig, pcntl_signal, pcntl_signal_dispatch, pcntl_get_last_error, pcntl_strerror, pcntl_sigprocmask, pcntl_sigwaitinfo, pcntl_sigtimedwait, pcntl_exec, pcntl_getpriority, pcntl_setpriority, eval, popen, passthru, exec, system, shell_exec, proc_open, proc_get_status, chroot, chgrp, chown, ini_alter, ini_restore, dl, pfsockopen, openlog, syslog, readlink, symlink, popepassthru, stream_socket_server, fsocket, chdir
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
无法在这个位置找到: baidushare.htm
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片
推荐知识