dqdes60666 2018-05-20 11:48
浏览 435
已采纳

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

Some details about my setup first:

  • I am serving a static webapp (HTML + JS) from default Nginx webroot
  • I have a PHP-FPM server running on localhost:9000
  • The destination file should be /api/webroot/index.php for FPM (always, no need to try_files etc.)
  • I need to forward all /api and /api-debug calls to arrive at localhost:9000, and the /app/webroot/index.php should handle all these requests.

I have the following working Nginx configuration:

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;
            }
        }
    }
}

I just want to make it more simple and efficient, because as I see it now it's a mess. I tried to adjust for example

try_files $uri /api/index.php$is_args$args;

to

try_files $uri /api/webroot/index.php$is_args$args;

and it failed... The only reason that it works is that /api/index.php includes /api/webroot/index.php, but I see it's inefficient.

I found debugging nginx config hard, because it's not easy to test.

Thank you very much for your help in advance!

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 session失效问题
      • ¥15 GMS地下水数值模拟无法打开
      • ¥15 树型结构的数据怎么匹配分支
      • ¥15 stm32F4用.引不出来结构体成员怎么解决
      • ¥15 .exe文件打不开怎么解决?
      • ¥30 Syntax Error: TypeError: this.getOptions is not a function的问题
      • ¥15 eclipse Android环境搭建
      • ¥15 unity中脚本之间的继承有什么作用
      • ¥15 C语言内存对齐代码不太理解
      • ¥15 uniapp中的uni-file-picker组件多图上传,删除,显示。以及C#.Net后端处理问题