[英]Nginx + PHP-FPM redirect to static PHP file
首先關於我的設置的一些細節:
我有以下工作的 Nginx配置:
upstream fastcgi_backend {
server localhost:9000;
keepalive 30;
}
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
location ~ ^/(api|api-debug)/ {
root /app/webroot;
index index.php;
try_files $uri /api/index.php$is_args$args;
location ~ \.php$ {
fastcgi_pass fastcgi_backend;
fastcgi_split_path_info ^(?:\/api\/)(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME /app/webroot/$fastcgi_script_name;
include fastcgi_params;
}
}
}
}
我只是想使其更簡單和有效,因為正如我現在所看到的那樣,這是一團糟。 我試圖進行調整
try_files $ uri /api/index.php$is_args$args;
至
try_files $ uri /api/webroot/index.php$is_args$args;
並且失敗了...起作用的唯一原因是/api/index.php包含/api/webroot/index.php,但是我認為它的效率很低。
我發現調試nginx配置很困難,因為它不容易測試。
非常感謝您的提前幫助!
最簡單的解決方案是使用值/app/webroot/index.php
硬連接SCRIPT_FILENAME並完全刪除您的一個location
塊。
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location ~ ^/(api|api-debug)/ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /app/webroot/index.php;
fastcgi_pass fastcgi_backend;
}
另外,為了保持以.php
擴展名指定URI的靈活性,可以使用以下方法簡化配置:
location / {
root /usr/share/nginx/html;
index index.html index.htm;
rewrite ^/(api|api-debug)/ /index.php last;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /app/webroot$uri;
fastcgi_pass fastcgi_backend;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.