繁体   English   中英

Nginx + PHP-FPM重定向到静态PHP文件

[英]Nginx + PHP-FPM redirect to static PHP file

首先关于我的设置的一些细节:

  • 我正在从默认的Nginx webroot提供一个静态的webapp(HTML + JS)
  • 我有一个运行在localhost:9000上的PHP-FPM服务器
  • 对于FPM,目标文件应为/api/webroot/index.php( 始终 ,无需try_files等)
  • 我需要转发所有/ api/ api-debug调用以到达localhost:9000,并且/app/webroot/index.php应该处理所有这些请求。

我有以下工作的 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM