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

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!

图片转代码服务由CSDN问答提供 功能建议

有关我的设置的一些详细信息:

  • I 我从默认的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应该处理所有这些请求。 < p>我有以下工作 Nginx配置:
      upstream fastcgi_backend {
     server localhost:9000; 
     keepalive 30; 
    } \  n 
    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 / in  dex.php $ is_args $ args; 
     
    位置〜\ .php $ {
     fastcgi_pass fastcgi_backend; 
     
     fastcgi_split_path_info ^(?:\ / api \ /)(。+ \。php)(。*)  $; 
     fastcgi_param SCRIPT_FILENAME / app / webroot / $ fastcgi_script_name; 
     
    包括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条回答 默认 最新

  • douwen7516 2018-05-20 16:00
    已采纳

    The simplest solution would be to hardwire SCRIPT_FILENAME with a value of /app/webroot/index.php and remove one of your location blocks altogether.

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

    Alternatively, to keep the flexibility of specifying a URI with a .php extension, you could simplify the configuration with:

    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;
    }
    
    打赏 评论

相关推荐 更多相似问题