允许使用Symfony 4和nginx执行index.php之外的其他php脚本

允许使用Symfony 4和nginx执行index.php之外的其他php脚本,php,symfony,nginx,nginx-location,Php,Symfony,Nginx,Nginx Location,我想我对语法理解得不够透彻,无法理解为什么我不能做到这一点 给定以下文件夹结构 - www/website -- public --- index.php --- otherscript.php 以及下面的nginx.conf server { listen 8000; server_name localhost; root /www/website/public; index index.ph

我想我对语法理解得不够透彻,无法理解为什么我不能做到这一点

给定以下文件夹结构

- 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_参数
文件,因此:

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(/|$){
之后,因为正则表达式位置是按照配置中的存在顺序匹配的。

您可以将
location~^/index\.php(/|$)
更改为
location~\.php(/|$)
并删除
内部指令。@RichardSmith仍然没有骰子,除了index.php之外的所有其他脚本都被忽略了:/谢谢!哇,我想我已经尝试过了:/Well惊喜,我挖得太远了,找不到解决方案。谢谢buddyDanila,你的方法奏效了,但你能解释一下:浏览到
localhost:8000/otherscript.php执行脚本,浏览到
localhost:8000
downloads index.php而不是执行it@DerpyNerd可能您没有放置块
location~\.php${
配置中最低的配置。您可以尝试更改初始配置,使其仅与特定脚本匹配。毕竟,列出允许执行的脚本是最安全的,而不是盲目地允许所有脚本运行,因此请尝试将
location~\.php${
替换为
location~^/(其他脚本| foo)\.php(/|$){
只允许
otherscript.php
foo.php
index.php
之外运行。它在配置中的位置(在索引处理程序之前或之后)无关紧要。它肯定是放在服务器块的最后。我将给出一个按名称指定脚本的例子