I use nginx and I want to separate the frontend and backend parts of the site. Roughly speaking, I want front files to be loaded from one folder, and backup files from another.
- There is a strict binding to the domain and subdomains can not be used.
- There is also a strict limitation that front and back files cannot be stored in the same folder.
Therefore, I want to access the front files in this way: my-domain.org/some-file ,
and to the my-domain.org/admin/some-file files like this: my-domain.org/admin/some-file .
Here I try to do this with this configuration:
server { listen 80; server_name domain.loc www.domain.loc; index index.php index.html; error_log /var/log/nginx/domain.loc-error.log; access_log /var/log/nginx/domain.loc-access.log; location / { root /home/www/domain.loc/frontend/web; if (!-e $request_filename) { rewrite ^(.*)$ /index.php; } try_files $uri $uri/ =404; # Перенаправление php-файлов к серверу «PHP-FPM» location ~ "^(/.+?\.php)(/.*)?$" { # Проверяем существование файлов, пробуем исправить если нет, иначе выдаём ошибку # $1 указывает на первую скобку в регулярном выражении выше - собственно адрес запрошенного php файла try_files $1 $uri $uri/ $uri/index.php =404; include common/php-fpm; } } location /admin { root /home/www/domain.loc/backend/web; if (!-e $request_filename) { rewrite ^(.*)$ /admin/index.php; } location ~ ^/admin(.*)?$ { try_files $1 $1/ =404; location ~ \.php$ { try_files /index.php =404; include common/php-fpm; } } } } Help me figure out what is wrong? In essence, you need to assign an arbitrary route in the file system to an arbitrary prefix location. Nginx assumes that the prefix is always part of the route in the file system. For beauty and slimness, I would like to get around this limitation.