php 路由 url 到 index.php nginx

php 路由 url 到 index.php nginx

我正在运行 nginx/php5.6-fpm 服务器,并且具有以下内容

server {
        listen 10080;
        listen [::]:10080;

        root /home/vagrant/app;
        index index.php;

        server_name mia-dev.dev;
        port_in_redirect on;
        rewrite ^/static/(.*)$ /dist/static/$1 permanent;

        location / {
          try_files $uri $uri/ /index.php?$query_string;
        }

        # load the index page on 404
        error_page 404 /index.php;

        # don't log requests to favicon.ico
        location = /favicon.ico {
            log_not_found off;
            access_log    off;
        }

        # don't log requests to robots.txt
        location = /robots.txt {
            log_not_found off;
            access_log    off;
        }

        # pass the PHP scripts to FastCGI server listening on the php-fpm socket
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       fastcgi_intercept_errors on;
#       fastcgi_intercept_errors on;
                try_files $uri /index.php =404;
                fastcgi_pass 0.0.0.0:9000;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #       fastcgi_param SCRIPT_FILENAME $document_root/index.php;
                fastcgi_param HTTPS off;
        }

        # disable access to .htaccess files
        location ~ /\.ht {
            deny all;
        }


        sendfile off;
}

在 php 文件中我有

$app = new Lime\App();

$app->bind("/create/name/", function(){
  return "hi mia";
});

$app->bind("/", function() {
    return "hellow world";
});



$app->run();

当我进入“/”时,我得到了“hello world”,但是当我进入“/create/name”时,我没有得到“hi mia”

根据我在网上查到的内容,我似乎做对了所有事情,所以我不确定问题出在哪里……

答案1

更新 2:

解决方案:

可能适用于任何需要路由的 PHP 应用程序,如果你愿意,可以跳过此评论 :-P

    ...

    location / {
      try_files $uri $uri/ @lime;
    }

    # pass the PHP scripts to FastCGI server listening on the php-fpm socket
    location @lime {
            # no `.php` in our fancy uri, useless
    #       fastcgi_split_path_info ^(.+\.php)(/.+)$;

    #       fastcgi_intercept_errors on;

            # useless as well
    #       try_files $uri /index.php =404;

            fastcgi_pass 0.0.0.0:9000;
            fastcgi_index index.php;
            include fastcgi_params;
    #       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

            # Yeah, you should use index.php and it will route for you
            fastcgi_param SCRIPT_FILENAME $document_root/index.php;

            fastcgi_param HTTPS off;
    }

    ...

使用@登录try_files执行内部重定向是最干净的方式推荐nginx 文档。随着应用程序的增长,为每个路由创建单独的位置块会导致大量重复配置。同时,操纵代码以在脚本参数中获取路径信息(Dupral 使用的一种卑鄙的方式)会破坏框架路由机制的优雅性,因此不推荐。


更新:

这与您的 nginx 配置有关。

当客户端访问时/create/name/,在你的 nginx 配置中,它将命中

    location / {
      try_files $uri $uri/ /index.php?$query_string;
    }

$uri="/create/name/"$query_string=""因此可以重写为/index.php?,然后匹配

    location ~ \.php$ {
    ...

也就是说,在 nginx 看来,访问/create/name/就相当于访问/index.php。因此,php 脚本不会获取指定的路径。因此我们总是获取到索引页。


您是否<?php在 php 文件的最开始添加了?

此外,根据你的框架的文档,你还应该有这个

require_once("src/Lime/App.php");

在你的index.php


参考:

  1. http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

答案2

在您的 Nginx 配置中,您没有提及 Nginx 在匹配您的模式时应该做什么。

默认位置块将按以下顺序匹配您的请求:

  1. 它尝试找到 /create/name/index.php 正如您提到的索引文件是 index.php,但显然它不存在。

  2. 它会尝试找到你提到的try_files对于 $uri 和 $uri/ 但它不匹配。

  3. 当它尝试/index.php?query_string设置,由于您正在浏览文件夹,因此没有 Nginx 需要匹配的 query_string 参数。

我认为你可以有两个解决方案:

添加新的位置块

location /create/name/ {
     try_files $uri $uri/ /index.php?$query_string;
}

添加重写规则

rewrite ^/create/name/$ /index.php?q=create permanent;

在你的 PHP 应用程序中:

$app->get("/:q", function($params){
    if ($params["q"] == 'create'){
        return "hi mia";
    }
});

参考: lime-php

相关内容