簡體   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