静的ファイルとPHPファイルを提供するようにnginxを設定しようとしています。私が持っている設定は機能していません。私は、次のローカルフォルダ構造をしたい:私はhttp://mysite.localを訪問静的ファイルとPHPファイルのNGINX設定
src/static/ -> contains HTML, CSS, JS, images etc
src/api/ -> contains PHP files for a small REST service
場合、私は/静的フォルダからファイルを提供することにしたいです。私がhttp://mysite.local/apiにアクセスした場合、私はAPI PHPファイルを提供したいと思います。私はapiへのリクエストを書き直し、index.phpファイルに送りたいと思っています。
いくつかの例:
http://mysite.local/test.html -> served from src/static/test.html
http://mysite.local/images/something.png -> served from src/static/images/something.png
http://mysite.local/css/style.css -> served from src/static/css/style.css
http://mysite.local/api/users -> served from src/api/index.php?users
http://mysite.local/api/users/bob -> served from src/api/index.php?users/bob
http://mysite.local/api/biscuits/chocolate/10 -> served from src/api/index.php?biscuits/chocolate/10
以下の設定は、APIファイルの静的ファイルに対して動作しますが、ありません。 APIパスの1つにアクセスすると、404エラーが返されます。
location /api {
root /var/www/mysite/src;
...
}
がローカルになり:
server {
listen 80;
server_name mysite.local;
access_log /var/log/nginx/mysite.access.log main;
error_log /var/log/nginx/mysite.error.log debug;
location/{
index index.html;
root /var/www/mysite/src/static;
try_files $uri $uri/ =404;
}
location /api {
index index.php;
root /var/www/mysite/src/api;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
try_files $uri = 404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
}
非常に有益な回答をいただきありがとうございます。主な問題は、/ apiロケーションブロックのルートの定義です。 – Bob