问题原文
原帖链接 · Kendall · CC BY-SA 3.0
I am running a nginx/ php5.6-fpm server and I have the following
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;
}
and in a php file i have
$app = new Lime\App();
$app->bind("/create/name/", function(){
return "hi mia";
});
$app->bind("/", function() {
return "hellow world";
});
$app->run();
While I get the "hello world" when going to "/" I don't get the "hi mia" when going to "/create/name"
Based on what I researched online I seemed to have done everything right so I'm not sure what is the problem....
社区高分解答
原帖链接 · ttimasdf · CC BY-SA 3.0
Update 2:
Solution:
may apply to any PHP app that need routing, skip the comment if you wish :-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;
}
...
Using @ sign in try_files to perform internal redirect is the cleanest way recommended by nginx documentation. Creating separate location block for each route will cause vast amounts of duplicate configuration as your app grows. Meanwhile, manipulating the code to get the path info in script parameter (a nasty way used by Dupral) breaks the elegance of the routing mechanism by your framework, therefore not recommended.
Update:
It's something related to your nginx config.
when the client visit /create/name/, in your nginx config, it will hit
location / {
try_files $uri $uri/ /index.php?$query_string;
}
with $uri="/create/name/" and $query_string="" , therefore be rewrited into /index.php? and then matched by
location ~ \.php$ {
...
That is, in nginx's perspective, visiting /create/name/ is equivalent to visiting /index.php. As a result, the php script will not get the path specified. Therefore we always get the index page.
Did you add <?php at the very beginning of your php file?
Besides, according to your framework's documentation, you should also have this
require_once("src/Lime/App.php");
in your index.php.
Reference:
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
