4

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....

Kendall
  • 247
  • 2
  • 4
  • 13

2 Answers2

4

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
ttimasdf
  • 141
  • 4
  • If you get `403 Forbidden` and `directory index of (...) is forbidden` in Nginx error logs, use `try_files $uri @lime` instead (remove `$uri/`). – Furgas May 23 '22 at 15:59
1

In your Nginx configuration, you didn't mention what Nginx should do when matching your pattern.

The default location block will match your request in the following order:

  1. It tries to find the /create/name/index.php as you mention the index file is index.php, but apparently it does not exist.

  2. It tries to find the / as you mentioned in try_files for $uri and $uri/ but it does not match.

  3. When it tries the /index.php?query_string setting, since you are browsing the folder, there is no query_string parameter for Nginx to match.

I think you can have two solutions:

Add a new location block

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

Adding rewrite rule

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

inside your PHP application:

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

Reference: lime-php

Simon MC. Cheng
  • 436
  • 2
  • 7