Проблема с несколькими расположениями (псевдонимами) NGINX PHP FastCGI

Проблема с несколькими расположениями (псевдонимами) NGINX PHP FastCGI

Первый раз пишу и совсем новичок во всем этом - мне посоветовали попробовать здесь через Stack Overflow, и я прочитал несколько разных ссылок, но просто не могу с этим разобраться. После множества проб и ошибок и поисков мои блоки местоположения в настоящее время выглядят так - глобальный PHP был удален и включен в мои блоки местоположения.

Первый вариант работает нормально, второй после нескольких изменений теперь не показывает ошибку 403 Forbidden или 404 not found, а показывает общую строку «Файл не найден».

www.mydomain.com- обслуживать файл index.php из/var/www/html/test

www.mydomain.com/test- следует обслуживать файл из index.php из /var/www/html/test2но происходит сбой со следующей ошибкой.

При посещении моего журнала ошибок Nginx отображается следующее:

2018/02/26 19:13:47 [error] 25229#25229: *185968 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: X.X.X.X, server: domain.co.uk, request: "GET /test/ HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "domain.co.uk"

Я просматривал здесь различные фрагменты о различных параметрах fastcgi_param SCRIPT_FILENAME, но не могу заставить ни один из них работать. Любые предложения были бы очень признательны, так как я потратил часы, пытаясь заставить это работать, и добился некоторого прогресса, но озадачен тем, что, как я предполагаю, будет последней задачей в том, чтобы заставить это работать.

Я уже удалил try_files из псевдонима, так как мне сказали, что это не очень хорошо работает (до этого были ошибки 403), и добавил $request_filename в мой fastcgi_param, но это не решило эту ошибку.

location ~ ^((?!\/test).)*$ {
include /etc/nginx/mime.types;
    root /var/www/html/test;
    index index.php index.html index.htm;
    location ~ \.php$ {
        root /var/www/html/test;
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}



location ~ ^/test(.*)$ {
include /etc/nginx/mime.types;
    alias /var/www/html/test2/;
    index index.php index.html index.htm;
    location ~ \.php$ {
        alias /var/www/html/test2/;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name$request_filename;
        include fastcgi_params;
    }
}

Пользователь, который рекомендовал мне попробовать здесь, посоветовал

Move the root /var/www/html/test; outside the first location block. nginx pitfalls
Replace alias with root in second block. according to nginx docs
remove the second alias in second block. it was redundant anyway
remove $request_filename; from fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name$request_filename; same error on serverfault

Я сделал это, однако это было почти шагом назад, что привело к ошибке 404 nginx при посещении /test/ с их предложенными изменениями, которые стали выглядеть так, как в их примере -

 server {
   root /var/www/html/test;
   location ~ ^((?!\/test).)*$ {
      include /etc/nginx/mime.types;
      index index.php index.html index.htm;
      location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
   }

   location ~ ^/test(.*)$ {
      include /etc/nginx/mime.types;
      root /var/www/html/test2/;
      index index.php index.html index.htm;
      location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
   }
}

Я с радостью поработаю с любым из них (я доверяю им больше, чем своим собственным), чтобы попытаться разобраться в этом вопросе, если у кого-то есть какие-либо знания, которые могли бы мне в этом помочь.

решение1

Джемаль на Stackoverflow решил эту проблему за меня!

server {
   root /var/www/html/test;

   location /test {
      include /etc/nginx/mime.types;
      root /var/www/html/test2/;
      index index.php index.html index.htm;
      location ~ \.php$ {
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
   }

   location / {
      include /etc/nginx/mime.types;
      index index.php index.html index.htm;
      location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param HTTP_IF_NONE_MATCH $http_if_none_match;
        fastcgi_param HTTP_IF_MODIFIED_SINCE $http_if_modified_since;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
      }
   }


}

Проблема с ошибками 404 возникла из-за того, что файлы для первого расположения находились в /var/www/html/test2, но с расположением /test, которое имеет префикс, поэтому каталог должен быть /var/www/html/test2 на стороне веб-сервера.

Связанный контент