我想配置nginx來提供靜態和PHP文件。我有的配置不起作用。我想下面的本地文件夾結構:靜態和PHP文件的NGINX配置
src/static/ -> contains HTML, CSS, JS, images etc
src/api/ -> contains PHP files for a small REST service
如果我訪問http://mysite.local我想從/靜態文件夾中的文件服務。如果我訪問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路徑,則會返回404錯誤。
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