A
A
aI3x2018-06-28 22:35:11
Nginx
aI3x, 2018-06-28 22:35:11

How to combine the launch of php scripts from different directories?

Hello,
there are directories:
/home/data/ - this folder contains files (eg index.php, conf.php, admin/index.php) that need to be hidden from the user (but he can run them).
/home/user/ - user files in this folder.
Task:

ЕСЛИ (запрошенный http адрес соответствует файлу в папке /home/user/)
{
  ТО вернуть клиенту этот файл
}
ИНАЧЕ
{
  ЕСЛИ (файл /home/user/index.php существует)
  {
    ТО вызвать скрипт /home/user/index.php для обработки запроса
  }
  ИНАЧЕ
  {
    указать root -директорию /home/data/ и
    
    ЕСЛИ (запрошенный http адрес соответствует файлу в папке /home/data/)
    {
      ТО вернуть клиенту этот файл
    }
    ИНАЧЕ
    {
      вызвать скрипт /home/data/index.php для обработки запроса
    }
  }
}

Those. if the user creates a file, for example, /home/user/index.php , then this file should be launched when the site is opened. If this file does not exist, then /home/data/index.php should be launched, and so on. At the same time, in addition to php files, folders can contain css files, pictures, and others.
Please tell me the working config for this task.
Tried through try_files until nothing happens...
Config for "single" mode:
location / {
  index index.php;
  try_files $uri $uri/ /index.php?$query_string;
}

location /admin {
  try_files $uri /admin/index.php?$query_string;
}

location ~ [^/]\.php(/|$) {
  fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  if (!-f $document_root$fastcgi_script_name) {
    return  404;
  }
  fastcgi_pass    127.0.0.1:9023;
  fastcgi_index   index.php;
  include         /etc/nginx/fastcgi_params;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shirshov Alexander, 2018-07-03
@Keanor

And what is the actual problem?
Nginx has nothing to do with it, all requests should go to index.php, and from there, with checking the rights, the necessary scripts should be connected, if you resolve through nginx, you will have more configs than php code.
Determine what to connect via $_SERVER['REQUEST_URI'].
You need to check for the existence of a file through file_exists.
You can start it with a simple include (there are options here, if necessary).
Difficulties will be with security, solved if you want to rummage access to an unlimited circle of people.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question