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

时间:2018-05-20 11:48:45

标签: php nginx static fpm

首先说明我的设置的一些细节:

  • 我从默认的Nginx webroot
  • 提供静态webapp(HTML + JS)
  • 我有一个在localhost上运行的PHP-FPM服务器:9000
  • 对于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配置很难,因为它不容易测试。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

最简单的解决方案是将值为game.load.audio('background', ['assets/audio/background.wav']) const backgroundSound = game.add.audio('background', 0.5, true) // here "true" means to loop backgroundSound.play() 的SCRIPT_FILENAME硬连线并完全删除其中一个/app/webroot/index.php块。

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; } 扩展名的URI的灵活性,您可以使用以下方法简化配置:

.php