我想我对语法的理解不够深刻,无法理解为什么我不能做到这一点。
给出以下文件夹结构
- www/website
-- public
--- index.php
--- otherscript.php
以及以下nginx.conf
server {
listen 8000;
server_name localhost;
root /www/website/public;
index index.php;
# optionally disable falling back to PHP script for the asset directories;
# nginx will return a 404 error when files are not found instead of passing the
# request to Symfony (improves performance but Symfony's 404 page is not displayed)
location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}
# would always refer to index.php (server directive)
location ~ \.php$ {
## tried this - no dice
fastcgi_pass 127.0.0.1:9001;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
index $fastcgi_script_name;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass 127.0.0.1:9001;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
}
我提出的每个请求都被重定向到index.php,这很有意义。我希望能够执行localhost:8000 / otherscript.php而不被重定向到index.php。
如何在不中断symfony路由的情况下做到这一点?
类似于/index.php
处理程序,您需要包含fastcgi_params
文件,因此:
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9001;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
请确保放置此之后 location ~ ^/index\.php(/|$) {
,因为正则表达式的位置按照配置中出现的顺序进行匹配。