NGINX PHP FastCGI 複数の場所 (エイリアス) の問題

NGINX PHP FastCGI 複数の場所 (エイリアス) の問題

初めて投稿します。このすべてについてまったくの初心者です。Stack Overflow で試してみるように勧められ、リンクされているいくつかの異なるものを読みましたが、理解できません。試行錯誤を繰り返し、ロケーション ブロックを確認した結果、現在、グローバル PHP が削除され、ロケーション ブロックに含まれています。

最初のものは正常に動作しますが、2 番目のものはいくつかの変更を加えた後、403 Forbidden または 404 not found は表示されず、一般的な文字列「File not found」が表示されます。

マイドメイン- ファイルindex.phpを提供する/var/www/html/テスト

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 パラメータに関するさまざまな情報を調べてきましたが、どれもうまく動作しません。これを動作させるために何時間も費やし、ある程度の進歩は見せたものの、これを動作させるための最終タスクと思われるものに困惑しているため、何かアドバイスがあれば本当にありがたいです。

うまく動作しない (その前は 403) と言われたため、エイリアスから try_files を既に削除し、fastcgi_param に $request_filename を追加しましたが、それでもこのエラーは止まりませんでした。

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

私はこれを実行しましたが、これはほとんど後退となり、/test/ にアクセスしたときに 404 nginx エラーが発生し、提案された変更により、例のとおり次のようになります -

 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 の Cemal がこの問題を解決してくれました!

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 がプレフィックスとして付加されるため、Web サーバー側ではディレクトリは /var/www/html/test2 になる必要があることに起因します。

関連情報