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”

根据我在网上调查,我似乎做了一切正确的,所以我不知道是什么问题….

在你的Nginxconfiguration中,你没有提到Nginx在匹配你的模式时应该做什么。

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

  1. 它试图find/create/name/index.php,因为你提到的索引文件是index.php,但显然它不存在。

  2. 它试图find/你在try_files中提到的$ uri和$ uri /,但它不匹配。

  3. 当它尝试/index.php?query_string设置时,由于您正在浏览文件夹,所以Nginx没有query_string参数可以匹配。

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

添加一个新的位置块

 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

更新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; } ... 

使用@ sign in try_files来执行内部redirect是nginx文档推荐的最干净的方法。 为每条path创build单独的位置块将随着您的应用程序的增长而导致大量的重复configuration。 同时,操作代码以获取脚本参数中的path信息(Dupral使用的一种令人讨厌的方式)会破坏框架的优雅性,因此不推荐使用。


更新:

这与你的nginxconfiguration有关。

当客户端访问/create/name/ ,在你的nginxconfiguration中,它会打

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

$uri="/create/name/"$query_string="" query_string $query_string="" ,因此被重新编译到/index.php? 然后再匹配

  location ~ \.php$ { ... 

也就是说,在nginx的angular度来说,访问/create/name/相当于访问/index.php 。 结果,php脚本将不会获得指定的path。 所以我们总是得到索引页面。


您是否在PHP文件的开头添加了<?php

另外,根据你的框架的文档,你也应该有这个

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

在你的index.php


参考:

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