[英]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.