允许使用 Symfony 4 和 nginx 执行除 index.php 之外的其他 php 脚本
Allow other php scripts to be executed besides index.php using Symfony 4 and nginx
我想我对语法的理解不够好,无法理解为什么我无法实现这一点。
给定以下文件夹结构
- 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,这是有道理的。
我希望能够在不被重定向到 index.php.
的情况下执行 localhost:8000/otherscript.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(/|$) {
之后,因为正则表达式位置按照配置中出现的顺序进行匹配。
我想我对语法的理解不够好,无法理解为什么我无法实现这一点。
给定以下文件夹结构
- 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,这是有道理的。 我希望能够在不被重定向到 index.php.
的情况下执行 localhost:8000/otherscript.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(/|$) {
之后,因为正则表达式位置按照配置中出现的顺序进行匹配。