Welcome! Log In Create A New Profile

Advanced

Problems with trailing slashes/path_info (php-fpm)

Posted by RVN 
RVN
Problems with trailing slashes/path_info (php-fpm)
October 21, 2011 06:13PM
Dear All,

I have been having a hell of a time debugging an issue with nginx...

The issue is the following, if someone tries to access mysite.com/myscript.php it works, but if for some reason they try accessing myscript.php/?param=ssss (Dont ask me why i'm getting this, i assume its some sort of ill formed ajax somewhere or its nginx that is re-writing something strangely...)

If i try accessing site.com/script.php it works. site.com/script.php/ doesnt... I have tried all different tutorials, using fastcgi_split_path_info (whenever I enable the PATH_TRANSLATED I begin getting No input file on ALL php files.... if I comment PATH_TRANSLATED line I get same symptom as described initially...)

I tried the (if $uri) method as posted below, same thing... works when accessing script without / or /SOMETEXT but it somehow isnt recognizing what is coming after the .PHP to be path_info/request_uri...

I have cgi_fix_pathh (cant remember the exact name) set to off in PHP.INI as I dont need it on... I just need NGINX to ignore the "path" and route it as if it were a user accessing the script...

I tried debugging in all sorts of ways (adding Location / to route everything to fcgi, tried tons of examples mentioning getting PATH_INFO, but nothing gets me there.... I dont even use it currently, I just need it to call the script...)

example Location blocks I have been toying with, all without success....:

(I have both socket and tcp fpm enabled for testing... I either get no input file, or blank (blank is pretty rare)...

Can anyone please help? :)

Thanks so mcuh!

----------------------------------------------------------------- EXAMPLE -----------------------------------------------------------------
location ~ \.php {
try_files $uri /path/to/404.htm;
#root html;
fastcgi_pass unix:/tmp/php5-fpm-hom.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
----------------------------------------------------------------- EXAMPLE -----------------------------------------------------------------


----------------------------------------------------------------- EXAMPLE 2 -----------------------------------------------------------------

location / {
#try_files $uri /path/to/404.htm;

fastcgi_pass unix:/tmp/php5-fpm-prod.sock;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# include /etc/nginx/fastcgi_params;

set $script $uri;
set $path_info "";

if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
set $path_info $2;
}

fastcgi_param SCRIPT_FILENAME $document_root$script;
fastcgi_param SCRIPT_NAME $script;
fastcgi_param PATH_INFO $path_info;
}



------------------------------- SUPPOSED HOLY GRAIL??? TRIED BUT STILL NO DICE.... ---------------------

location ~ \.php {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# A handy function that became available in 0.7.31 that breaks down
# The path information based on the provided regex expression
# This is handy for requests such as file.php/some/paths/here/

fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}

-------------------------------------




FASTCGI_PARAMS:
-----------------------------------------------------------------------------------

fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;

fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;

fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;

fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
Re: Problems with trailing slashes/path_info (php-fpm)
October 23, 2011 05:30AM
Hi,

try this:

# handle URLs like: "/path/to/myscript.php"

location ~ ^(?<script_name?>.+\.php)$ {
try_files $script_name =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$script_name;
fastcgi_pass 127.0.0.1:9000;
}

# handle URLs like: "/path/to/myscript.php/with/path/info"

location ~ ^(?<script_name?>.+\.php)(?<path_info>/.+)$ {
try_files $script_name =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_param PATH_TRANSLATED $document_root$path_info;
fastcgi_pass 127.0.0.1:9000;
}

# make nginx recognize index.php as index file

location / {
index index.php;
}
RVN
Re: Problems with trailing slashes/path_info (php-fpm)
October 24, 2011 02:00PM
I get a regex error...

----
[emerg]: pcre_compile() failed: unrecognized character after (?< in "^(?<script_name?>.+\.php)$" at "script_name?>.+\.php)$" in /etc/nginx/nginx.conf:117
configuration file /etc/nginx/nginx.conf test failed
-----

I'm not supposed to replace <script_name> with my actual php right? Because there are many variations... I was looking for more of a "generic" solution... Would you also care to enlighten about why the solutions mentioned in the original post all fail? (I'd like to learn a bit more about how nginx works...)

Best rgds!
Re: Problems with trailing slashes/path_info (php-fpm)
October 24, 2011 02:07PM
nginx version?

pcre library version? install the latest.

Andrejs
Re: Problems with trailing slashes/path_info (php-fpm)
October 24, 2011 02:11PM
Sorry, it should have been:


# handle URLs like: "/path/to/myscript.php"

location ~ ^(?<script_name>.+\.php)$ {
try_files $script_name =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$script_name;
fastcgi_pass unix:/tmp/php5-fpm-hom.sock;
}

# handle URLs like: "/path/to/myscript.php/with/path/info"

location ~ ^(?<script_name>.+\.php)(?<path_info>/.+)$ {
try_files $script_name =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_param PATH_TRANSLATED $document_root$path_info;
fastcgi_pass unix:/tmp/php5-fpm-hom.sock;
}




?<....> means capture expression into variable

Andrejs



Edited 1 time(s). Last edit at 10/24/2011 02:19PM by locojohn.
RVN
Re: Problems with trailing slashes/path_info (php-fpm)
October 24, 2011 02:21PM
I kind of follow your logic on your method, but from wht I read the initial methods *should* have worked too? oh well..

anyways, thanks for your help, but still I'm getting the same error...

nginx: [emerg] pcre_compile() failed: unrecognized character after (?< in "^(?<script_name>.+\.php)$" at "script_name>.+\.php)$" in /etc/nginx/nginx.conf:81
nginx: configuration file /etc/nginx/nginx.conf test failed


Installed Packages
Name : pcre
Arch : x86_64
Version : 6.6
Release : 6.el5_6.1
Size : 239 k
Repo : installed
Summary : Perl-compatible regular expression library
URL : http://www.pcre.org/
License : BSD
Description: Perl-compatible regular expression library.
: PCRE has its own native API, but a set of "wrapper" functions that are based on
: the POSIX API are also supplied in the library libpcreposix. Note that this
: just provides a POSIX calling interface to PCRE: the regular expressions
: themselves still follow Perl syntax and semantics. The header file
: for the POSIX-style functions is called pcreposix.h.

Available Packages
Name : pcre
Arch : i386
Version : 6.6
Release : 6.el5_6.1
Size : 117 k
Repo : base
Summary : Perl-compatible regular expression library
URL : http://www.pcre.org/
License : BSD
Description: Perl-compatible regular expression library.
: PCRE has its own native API, but a set of "wrapper" functions that are based on
: the POSIX API are also supplied in the library libpcreposix. Note that this
: just provides a POSIX calling interface to PCRE: the regular expressions
: themselves still follow Perl syntax and semantics. The header file
: for the POSIX-style functions is called pcreposix.h.

Installed Packages
Name : nginx
Arch : x86_64
Version : 1.1.6
Release : 1.el5
Size : 6.2 M
Repo : installed
Summary : Robust, small and high performance http and reverse proxy server
URL : http://nginx.net/
License : BSD
Description: Nginx [engine x] is an HTTP(S) server, HTTP(S) reverse proxy and IMAP/POP3
: proxy server written by Igor Sysoev.
:
: Following third party modules added:
: * nginx-upstream-fair
: * mod_zip
: * ngx_http_auth_pam_module


(Suppose I can remove 32bit version later on... but anyways... I need to cleanup this machine anyways)

I also tried on another test machine with nginx 0.8.55, same deal... Thanks again for your help!
Re: Problems with trailing slashes/path_info (php-fpm)
October 24, 2011 02:29PM
Well, it doesn't work because you have an older version of PCRE library:

http://nginx.org/en/docs/http/server_names.html says:

The PCRE library supports named captures using the following syntax:

?<name> Perl 5.10 compatible syntax, supported since PCRE-7.0
?'name' Perl 5.10 compatible syntax, supported since PCRE-7.0
?P<name> Python compatible syntax, supported since PCRE-4.0

So you can try:

?P<script_name>

instead.

Good luck!

Andrejs
RVN
Re: Problems with trailing slashes/path_info (php-fpm)
October 24, 2011 03:03PM
Sorry about that... I should have checked better about the latest versions... I guess I'll end up building pcre from source... Anyways, trying it with the ?P<xx> format... Seems like I'm closer, but still not quite...

Nginx.conf excerpt:


location ~ ^(?P<script_name>.+\.php)$ {
#try_files $script_name =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$script_name;
fastcgi_pass 127.0.0.1:9000;
}

# handle URLs like: "/path/to/myscript.php/with/path/info"

location ~ ^(?P<script_name>.+\.php)(?P<path_info>/.+)$ {
#try_files $script_name =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_param PATH_TRANSLATED $document_root$path_info;
fastcgi_pass 127.0.0.1:9000;
}
location / {
index index.php;
}


Results:

http://127.0.0.1/ping.php?hh - OK

http://127.0.0.1/ping.php/?hh - 404
Error log: 2011/10/24 17:01:47 [error] 31954#0: *6 "/usr/www/ping.php/index.php" is not found (20: Not a directory), client: 172.20.15.151, server: _, request: "GET /ping.php/?hh HTTP/1.1"

http://127.0.0.1/ping.php/sss?hh - No input file specified
Error log: nothing, as is expected...

I tried commenting out the try_files line on both location clauses just in case it was interfering with something, but it made absolutely no difference... Also tried removing the location / block, in case it was falling into that and trying to redirect to MYSCRIPT.php/index.php, but it made no difference... always tried script.php/index.php... even though there seemed to be no reference at all to "index.php" in the .conf file... anywhere at all (not in server, not in http, anywheere)...

Thanks!



Edited 1 time(s). Last edit at 10/24/2011 03:05PM by RVN.
Re: Problems with trailing slashes/path_info (php-fpm)
October 24, 2011 03:09PM
Make sure the following is set in php.ini:

cgi.fix_pathinfo = 1

Please attach your full nginx.conf as it is now, if possible.

Andrejs



Edited 1 time(s). Last edit at 10/24/2011 03:09PM by locojohn.
RVN
Re: Problems with trailing slashes/path_info (php-fpm)
October 24, 2011 03:19PM
fix_pathinfo was 0... It has always been 0 as our app was designed to not rely on that... I've always heard that it was a bad idea? Anyways, with pathinfo=1, it works for script.php/something?param=abc... still doesnt work for script.php/?param=abc... cool... 2 working :) 1 to go... unfortunately this 1 is the one I was seeing popping up in the logs when I put nginx in our test environment... :( eheheh

Is it ok to have the pathinfo=1? I thought that would only influence the PATH_INFO and REQUEST_URI variable within the scripts themselves? anyways, as long as it doesnt pose a security risk then it would be ok... It may be best to also also limit executing scripts only on the root directory? So no php can be run except from hwww.mysite.com/

(basically mysite.com/?aaaa=a is ok, mysite.com/script.php is ok, but mysite.com/directory/script.php is no go for PHP script.......)

anwyays, full nginx.conf:

#######################################################################
#
# This is the main Nginx configuration file.
#
# More information about the configuration options is available on
# * the English wiki - http://wiki.nginx.org/Main
# * the Russian documentation - http://sysoev.ru/nginx/
#
#######################################################################

#----------------------------------------------------------------------
# Main Module - directives that cover basic functionality
#
# http://wiki.nginx.org/NginxHttpMainModule
#
#----------------------------------------------------------------------

user naweb;
worker_processes 5;

error_log /var/log/nginx/error.log;
#error_log /var/log/nginx/error.log notice;
#error_log /var/log/nginx/error.log info;

pid /var/run/nginx.pid;


#----------------------------------------------------------------------
# Events Module
#
# http://wiki.nginx.org/NginxHttpEventsModule
#
#----------------------------------------------------------------------

events {
worker_connections 1024;
}


#----------------------------------------------------------------------
# HTTP Core Module
#
# http://wiki.nginx.org/NginxHttpCoreModule
#
#----------------------------------------------------------------------

http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';

access_log /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;

gzip on;

#
# The default server
#


#charset koi8-r;

#access_log logs/host.access.log main;

server {
listen 88;
server_name _;
root /usr/www/Application/;

#

location ~ ^(?P<script_name>.+\.php)$ {
try_files $script_name =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$script_name;
fastcgi_pass 127.0.0.1:9000;
}

# handle URLs like: "/path/to/myscript.php/with/path/info"

location ~ ^(?P<script_name>.+\.php)(?P<path_info>/.+)$ {
try_files $script_name =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$script_name;
fastcgi_param PATH_INFO $path_info;
fastcgi_param PATH_TRANSLATED $document_root$path_info;
fastcgi_pass 127.0.0.1:9000;
}
location / {
index index.php;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#

}



# Load config files from the /etc/nginx/conf.d directory
# include /etc/nginx/conf.d/*.conf;

}
RVN
Re: Problems with trailing slashes/path_info (php-fpm)
October 24, 2011 03:47PM
Actually I was being stupid about it...

location ~ ^(?P<script_name>.+\.php)(?P<path_info>/.+)$ { should be location ~ ^(?P<script_name>.+\.php)(?P<path_info>/.*)$ { to allow a /?...

Its now working... a bit paranoid about fixpath being 1, so I'm going to look into it all a bit deeper...

Thanks for your help though Andrejs, much appreciated!
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 146
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 500 on July 15, 2024
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready