NGINX - 如何将“/<param>”重定向到 index.php?page=<param>

NGINX - How to redirect '/<param>' to index.php?page=<param>

我在 Whosebug 上阅读了很多问题,我也尝试了回答的解决方案,但我仍然无法实现我需要的。

我在PHP中编写了以下简单的路由代码:

$page = $_GET['page'];
echo "<section id='content' class='container'>";

if ( isset($page) )
{
    switch($page)
    {
        case "guides":
            require_once("v/pages/guides/index.php");
            break;
        case "error-list-dpto":
            require_once("v/pages/error-list/error-list-dpto.php");
            break;
        //other cases here...
    }
}
echo "</section>";

我有以下简单的 nginx 配置:

server {  
    listen 80;
    listen [::]:80;
    root  /app/mylogs;
    server_name localhost;

    index index.php;

    location /(.*) {
            try_files $uri $uri/ /index.php?page= =404;
    }

    location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
    }

    location ~ /\.ht {
                    deny all;
    }
}

我想访问类似“http://example.com/guides”的内容,并使 $page 变量接收 "guides" 字作为值。

但是我的 NGINX 代码不工作。当我尝试访问上面的地址时,我从 NGINX 收到了 404。

我做错了什么?

谢谢。

我设法使用以下 NGINX conf 文件行实现了我的需要:

rewrite ^/page/(.*)$ /index.php?page= last;
location / {
    try_files $uri $uri/ =404;
}