NGINX показать index.php из другого каталога
Всем привет. Есть сервер Nginx. На нем есть три каталога: /var/www/wp/(тут CMS WordPress), /var/www/php5/(тут файл index.html и index.php) и /var/www/php7/(тут тоже файл index.html и index.php)
Вот стандартный default конфиг:
server {
listen 80 default_server;
listen [::] 80 default_server;
root /var/www/wp;
index index.php index.html;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ ^/([a-zA-Z0-9]*)/ {
root /var/www/;
try_files $uri $uri/ /$1/index.html;
}
}
При запросе localhost открывается сайт на wordpress. При запросе localhost/php5 открывается файл index.html в папке php5 При запросе localhost/php7 открывается файл index.html в папке php7
А как сделать, чтобы вместо html файлов открывался файл php? пишу try_files $uri $uri/ /$1/index.php; - появляется ошибка типа 404.
Как заставить сервер работать с файлами php в разных каталогах?
Ответы (1 шт):
Было найдено следующее решение.
Нужно было папки php5, php7 создавать ВНУТРИ папки wp. А в секции location для этих папок нужно было дописать wp в root.
Вот исправленный и рабочий вариант:
server {
listen 80 default_server;
listen [::] 80 default_server;
root /var/www/wp;
index index.php index.html;
server_name localhost;
location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ ^/([a-zA-Z0-9]*)/ {
root /var/www/wp;
try_files $uri $uri/ /$1/index.php;
}
}