大网数据广告活动
服务器运维问答

php routing url to index.php nginx

· 52CE 我爱测

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…

问题原文

原帖链接 · 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:

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

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-08-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。