1. 程式人生 > >nginx+php使用open_basedir限制站點目錄防止跨站

nginx+php使用open_basedir限制站點目錄防止跨站

以下三種設定方法均需要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.server110.com]
open_basedir=/home/www/www.server110.com:/tmp/:/proc/ [PATH=/home/www/www.server110.com] open_basedir=/home/www/www.server110.com:/tmp/:/proc/
這種方式的設定需要重啟php-fpm後生效。

方法3)在網站根目錄下建立.user.ini並寫入:
open_basedir=/home/www/www.server110.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

http://www.server110.com/nginx/201308/477.html

http://bbs.csdn.net/topics/390979175