Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't attach resource to root of mount point #174

Open
briankilgore opened this issue Nov 14, 2013 · 7 comments
Open

Can't attach resource to root of mount point #174

briankilgore opened this issue Nov 14, 2013 · 7 comments

Comments

@briankilgore
Copy link

I don't seem to be able to attach a resource to the root of my mount point.
I have defined a mount point of /api for my app. I also have a resource named MainResource with the URI defined as "@uri /". All of my other routes work, but when I try to access http://mysite/api/, Tonic throws an exception stating that "Resource matching URI "/api" not found". I was able to get it to work by adding a condition to the mount() method that checks to see if the URI equals "/", and if so, don't attach the URI to the uriSpace. Am I doing something wrong? Is there another way to attach a resource to "/". If not, any can we get this fix pulled in?

if($uri[0] == "/")
    $this->resources[$className]['uri'][$index][0] = $uriSpace;
else
    $this->resources[$className]['uri'][$index][0] = $uriSpace.$uri[0];
@briankilgore
Copy link
Author

I should also mention that everything was actually working fine with the "/" URI for MainResource on Apache, but when I started testing on NGINX, I began getting the error.

@drkibitz
Copy link
Contributor

I'm curious of the difference with Apache vs NGINX, I'm assuming you were using mod_rewrite with Apache, and if so what were did the rules look like? The reason of course are because sometimes we forget to emulate all the mod_rewrite settings in NGINX when making a switch, myself included.

Please note, you can use Github flavored markdown for code snippets.

$this->one = "is php";

@briankilgore
Copy link
Author

My implementations between Apache and NGINX are very similar (see below).

Apache (.htaccess located in /path/to/app)

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} !router\.php$
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^api(.)* api/router.php [L,QSA]
</IfModule>

NGINX

location /api {
    rewrite ^/api(.*) /api/router.php last;
}

Thanks for the suggestion on GFM

I'm brand new to NGINX, so it's entirely possible (likely) that I made a mistake or left something out, so any suggestions would be much appreciated.

@drkibitz
Copy link
Contributor

Hmm, just from looking I can't really see anything wrong, but I would make a suggestion, if you can, to var_dump/print_r everything from $_SERVER to see what you get as raw headers before invoking anything in tonic. This way, if you can do this in Apache and NGNIX, you can see what you are actually getting and the differences from the 2 different implementations.

I have a feeling it's something that might be able to be fixed with a rewriteBase value.

@briankilgore
Copy link
Author

Here are the $_SERVER outputs from both.

$_SERVER from Apache

Array
(
    [REDIRECT_STATUS] => 200
    [HTTP_HOST] => localhost
    [HTTP_CONNECTION] => keep-alive
    [HTTP_CACHE_CONTROL] => max-age=0
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    [HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48 Safari/537.36
    [HTTP_ACCEPT_ENCODING] => gzip,deflate,sdch
    [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.8
    [HTTP_COOKIE] => PHPSESSID
    [PATH] => /usr/bin:/bin:/usr/sbin:/sbin
    [SERVER_SIGNATURE] => 
    [SERVER_SOFTWARE] => Apache
    [SERVER_NAME] => localhost
    [SERVER_ADDR] => ::1
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => ::1
    [DOCUMENT_ROOT] => /Users/bkilgore/Sites
    [SERVER_ADMIN] => you@example.com
    [SCRIPT_FILENAME] => /Users/bkilgore/Sites/sandbox/RestApp/api/router.php
    [REMOTE_PORT] => 62483
    [REDIRECT_URL] => /sandbox/RestApp/api/
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => 
    [REQUEST_URI] => /sandbox/RestApp/api/
    [SCRIPT_NAME] => /sandbox/RestApp/api/router.php
    [PHP_SELF] => /sandbox/RestApp/api/router.php
    [REQUEST_TIME] => 1384475568
    [argv] => Array
        (
        )

    [argc] => 0
)

$_SERVER from NGINX

Array
(
    [USER] => bkilgore
    [HOME] => /Users/bkilgore
    [FCGI_ROLE] => RESPONDER
    [SCRIPT_FILENAME] => /Users/bkilgore/Sites/sandbox/RestApp/api/router.php
    [QUERY_STRING] => 
    [REQUEST_METHOD] => GET
    [CONTENT_TYPE] => 
    [CONTENT_LENGTH] => 
    [SCRIPT_NAME] => /api/router.php
    [REQUEST_URI] => /api/
    [DOCUMENT_URI] => /api/router.php
    [DOCUMENT_ROOT] => /Users/bkilgore/Sites
    [SERVER_PROTOCOL] => HTTP/1.1
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_SOFTWARE] => nginx/1.4.3
    [REMOTE_ADDR] => 127.0.0.1
    [REMOTE_PORT] => 62444
    [SERVER_ADDR] => 127.0.0.1
    [SERVER_PORT] => 8080
    [SERVER_NAME] => localhost
    [REDIRECT_STATUS] => 200
    [HTTP_HOST] => localhost:8080
    [HTTP_CONNECTION] => keep-alive
    [HTTP_CACHE_CONTROL] => max-age=0
    [HTTP_ACCEPT] => text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
    [HTTP_USER_AGENT] => Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48 Safari/537.36
    [HTTP_ACCEPT_ENCODING] => gzip,deflate,sdch
    [HTTP_ACCEPT_LANGUAGE] => en-US,en;q=0.8
    [HTTP_COOKIE] => PHPSESSID
    [PHP_SELF] => /api/router.php
    [REQUEST_TIME_FLOAT] => 1384475454.7212
    [REQUEST_TIME] => 1384475454
)

Clearly, the DOCUMENT_URI and REQUEST_URI values are slightly different, but would that prevent the resource from attaching to "/"?

@drkibitz
Copy link
Contributor

I think I need more time to look into this, but I'm leaning toward this being the (start of the) culprit.

First this:

tonic/src/Tonic/Request.php

Lines 187 to 191 in 3a00507

if (isset($_SERVER['REDIRECT_URL']) && isset($_SERVER['SCRIPT_NAME'])) { // use redirection URL from Apache environment
$dirname = dirname($_SERVER['SCRIPT_NAME']);
$uri = substr($_SERVER['REDIRECT_URL'], strlen($dirname == DIRECTORY_SEPARATOR ? '' : $dirname));
} elseif (isset($_SERVER['REQUEST_URI'])) { // use request URI from environment
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

Then this:

$this->baseUri = dirname($_SERVER['DOCUMENT_URI']);

  • For Apache, it is using the substring from REDIRECT_URL and SCRIPT_NAME, which in your case would be "/".
  • For NGINX it uses the parsed REQUEST_URI, which in your case is "/api/".
  • Assuming you are not manually defining a baseUri, in NGINX, you now have one set to "/api", while in Apache it should be set to "".

I do not see anywhere in the code where it handles the baseUri by stripping it, but I am not confident enough with the code right now to see where the matching is actually comparing resource paths with some concatenated version of $baseUri . $path.

This is just my initial finding to get somewhere to tracking this down. It does look like this should be accounted for, but sorry I'm unsure of the exact problem right now. At one point I had the whole test suite for Tonic setup, but not on my current machine :/

I do however still believe this is a platform specific issue, that I still definitely think is worth tracking down.

@briankilgore
Copy link
Author

Based on your suggestion, I added the following snippet to the getURIFromEnvironment() method and it worked.

elseif (isset($_SERVER['REQUEST_URI']) && isset($_SERVER['SCRIPT_NAME'])) { // use redirection URL from NGINX environment
    $dirname = dirname($_SERVER['SCRIPT_NAME']);
    $uri = substr($_SERVER['REQUEST_URI'], strlen($dirname == DIRECTORY_SEPARATOR ? '' : $dirname));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants