解决nginx跨站浏览问题

解决nginx跨站浏览有两种方法:
1、为每一个网站定义一个pool,然后使用chroot指令,但这种方法的缺点是,当你需要建立很多虚拟主机时,消耗的内存是非常大的。
2、修改php源代码。这种通过修改php源代码的方法可以很好的解决这个问题而不必像第一种额外的内存消耗。
下面开始介绍第二种方法。
下载php源码,执行./configure命令,然后开始修改main/fopen_wrappers.c文件

  1. /* {{{ php_check_open_basedir
  2. */
  3. PHPAPI int php_check_open_basedir_ex(const char *path, int warn TSRMLS_DC)
  4. {
  5.         /* Only check when open_basedir is available */
  6.         if (PG(open_basedir) && *PG(open_basedir)) {
  7.                 char *pathbuf;
  8.                 char *ptr;
  9.                 char *end;
  10.                 // add by anxsoft.com
  11.                 char *env_doc_root;
  12.                 if(PG(doc_root)){
  13.                         env_doc_root = estrdup(PG(doc_root));
  14.                 }else{
  15.                         env_doc_root = sapi_getenv("DOCUMENT_ROOT", sizeof("DOCUMENT_ROOT")-1 TSRMLS_CC);
  16.                 }
  17.                 if(env_doc_root){
  18.                         int        res_root = php_check_specific_open_basedir(env_doc_root, path TSRMLS_CC);
  19.                         efree(env_doc_root);
  20.                         if (res_root == 0) {
  21.                                 return 0;
  22.                         }
  23.                         if (res_root == -2) {
  24.                                 errno = EPERM;
  25.                                 return -1;
  26.                         }
  27.                 }
  28.                 // add by anxsoft.com
  29.  
  30.  
  31.                 pathbuf = estrdup(PG(open_basedir));
  32.  
  33.                 ptr = pathbuf;
  34.  
  35.                 while (ptr && *ptr) {
  36.                         end = strchr(ptr, DEFAULT_DIR_SEPARATOR);
  37.                         if (end != NULL) {
  38.                                 *end = '\0';
  39.                                 end++;
  40.                         }
  41.  
  42.                         if (php_check_specific_open_basedir(ptr, path TSRMLS_CC) == 0) {
  43.                                 efree(pathbuf);
  44.                                 return 0;
  45.                         }
  46.  
  47.                         ptr = end;
  48.                 }
  49.                 if (warn) {
  50.                         php_error_docref(NULL TSRMLS_CC, E_WARNING, "open_basedir restriction in effect. File(%s) is not within the allowed path(s): (%s)", path, PG(open_basedir));
  51.                 }
  52.                 efree(pathbuf);
  53.                 errno = EPERM; /* we deny permission to open it */
  54.                 return -1;
  55.         }
  56.  
  57.         /* Nothing to check... */
  58.         return 0;
  59. }
  60. /* }}} */

两个 add by anxsoft.com 中间的是修改加上去的,然后保存,退出。之后make 和make install 即可。
然后修改php.ini文件:

  1. open_basedir = "/tmp/:/var/tmp/"

经测试php5.2和php5.3同样适用。
参考:http://www.hostloc.com/thread-6546-1-1.html

标签:Nginx 发布于:2019-10-15 11:22:39