0

I'm moving my site from Apache to Nginx but I'm stuck at a issue for which I'm not able to find a solution, neither here nor on Google.

Issue:

On my home page, there are some items on menubar, clicking on which calls for .js scripts which in turn calls for some php files.

Eg. http://localhost/cart/shopping_options.php/?func=get_shop_menu&ajax=1

Eg. http://localhost/index.php/?func=get_request_demo&ajax=1

This works fine with Apache but not with Nginx. Nginx is taking the trailing slash /and it turns it into a directory so if I see my Nginx error logs I see, the file being requested is:

http://localhost/cart/shopping_options.php/index.php

nginx error:

2019/04/29 21:29:23 [error] 5033#5033: *1 "/var/www/html/cart/shopping_options.php/index.php" is not found (20: Not a directory), client: 127.0.0.1, server: _, request: "GET /cart/shopping_options.php/?func=get_shop_menu&ajax=1 HTTP/1.1", host: "localhost", referrer: "http://localhost/"

If something is not clear or if anyone needs more information, please comment. If anyone can help me with this, it would be great. There are bunch of .js files and this coding practice of putting a / after .php has been used in many places which I'm not even aware of, so it is not possible to fix by going directly to files where this link is being generated and fixing the code.

0

The URLs are actually completely correct according to "normal" HTTP URL syntax. The additional feature you've been relying on is called "CGI pathinfo" in Apache, which if enabled tells the webserver to detect CGI scripts that occur in the middle of a path, and treat the rest as a "virtual" path passed to the CGI script.

Nginx sort-of supports this feature, but differently from Apache: it doesn't test each path component automatically; you have to tell it precisely where to split the input path, using the fastcgi_split_path_info setting.

The usual configuration looks like this:

# https://security.stackexchange.com/questions/177354
location ~ \.php(?:$|/) {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    # [other options like fastcgi_pass follow]
}

In general it's not a very reliable feature as it involves a bit of guessing on the webserver's part, and it has had various security issues associated with it (as can be seen in the Security.SE thread). In this particular case, your examples do not even make any good use of it – there's no useful path appended, just a single slash everywhere, so that's a waste. (The way it's actually intended to be used is when one wants to make URLs look nicer by emulating a whole path, e.g. /shopping.php/products/1234?ajax=1.)

1
  • It is still requesting the same file: Request URL:http://localhost/index.php/?func=get_request_demo&ajax=1 Request method:GET Remote address:127.0.0.1:80 Status code: 404 How do I request it to call for the file localhost/index.php?func=get_request_demo&ajax=1 (without trailing slash after .php). This trailing slash works fine with Apache but not with nginx . – Axel Apr 30 '19 at 7:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.