How to setup nginx to make php works in site subdirectories

Hello,

I’m nuewbie in nginx, I’ve installed and setup nginx & php,
everything is fine except .php files located in site’s subdirectories
are not processed at all.

i.e. example.com/download.php works fine, but
example.com/stuff/dosomething.php is sent to client as plain text.

Please, would any kind soul tell me what should I change to resolve the
problem.
Thank you.

I use those settings.

example.com

server {
listen 80;
server_name example.com;

 access_log  /var/log/nginx/example.com.access.log;

 location / {
     root   /var/www/nginx-default/example.com;
     index  index.html index.htm index.php;
 }

Parse all .php file in the /var/www directory

 location ~ .php$ {
     fastcgi_split_path_info ^(.+\.php)(.*)$;
     fastcgi_pass   backend;
     fastcgi_index  index.php;
     fastcgi_param  SCRIPT_FILENAME

/var/www/nginx-default/example.com$fastcgi_script_name;
include 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_intercept_errors on;
fastcgi_ignore_client_abort off;
fastcgi_connect_timeout 60;
fastcgi_send_timeout 180;
fastcgi_read_timeout 180;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}

Disable viewing .htaccess & .htpassword

 location ~ /\.ht {
     deny  all;
 }

}

upstream backend {
server 127.0.0.1:9000;
}

Hello,

Maybe you could use virtual conf, ie:

location ~ .php$ {
root /home/example/public_html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/home/example/public_html$fastcgi_script_name;
include fastcgi_params;
}

Regards,
Joe

Hello, Joe, I’ve tried to use settings from your post, unfortunately
nothing changed.
Scripts in site’s root are processed, but in subdirectories are not (

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,222301,222303#msg-222303

12 февраля 2012, 15:00 от Dmitry T. [email protected]:

problem.
server_name example.com;
fastcgi_split_path_info ^(.+.php)(.*)$;
fastcgi_ignore_client_abort off;
location ~ /.ht {
deny all;
}
}

upstream backend {
server 127.0.0.1:9000;
}

That isn’t your complete configuration now, is it? Another
location block (which you haven’t posted) seems to be
matching your subdirectory requests. Add this to your
server config and then check your error log to see what’s
matching your requests - look for log entries that look
like “using configuration” to find the matching location
block.

error_log /var/log/nginx/example.com.error.log debug;
root /var/www/nginx-default/example.com;

Always set the root directory inside the server configuration
block, otherwise it will be reset to the --prefix configuration
argument that nginx was compiled with (run “nginx -V”
to find out yours).

Max

Hello Dmitry,

Please try to put the subdirectory name behind /

Regards,
Joe

On Sun, Feb 12, 2012 at 06:33:09AM -0500, dimentiy wrote:

Hi there,

Scripts in site’s root are processed, but in subdirectories are not (

Using your configuration, I’m unable to reproduce your reported output.

There are some tidy-up fixes that could be done to your configuration,
but what you have should be working already.

I suspect that either it is working but your browser cache is hiding
that from you; or you have an extra location{} block that means that
your
“.php$” one isn’t being used.

Suggestion: create a file
/var/www/nginx-default/example.com/stuff/new.php
with the contents:

==

<?php
print_r($_GET);
?>
stuff/new.php

and look at the result of getting example.com/stuff/new.php. That should
remove the “browser cache” possibility.

If it is still not what you expect, then something like the debug log
should show you which location{} is being used for this request.

Good luck with it,

f

Francis D. [email protected]

Hello, Joe,

behind what /?

I’ve tried different variants. But I really misunderstand how it should
be.

The goal is to make nginx process .php files whereever they located
begining from site’s root folder.

This settings works fine with scripts in site’s root.

location ~ .php$ {
root /var/www/nginx-default/example.com;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/var/www/nginx-default/example.com$fastcgi_script_name;
include fastcgi_params;
}

This redirects ‘example.com/popups/popup_on_enter.php’ to php-fpm but it
returns ‘File not found’.

location /popups/popup_on_enter.php {
root /var/www/nginx-default/example.com/popups;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/var/www/nginx-default/example.com/popups$fastcgi_script_name;
include fastcgi_params;
}

I suppose there sould be variant with one ‘location’ directive
processing all the files in /var/www/nginx-default/example.com.
But due to my little experience I can’t realize how to reach the goal.

Thank you.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,222301,222314#msg-222314

Hello, Francis,

Yes, I have another ‘location’ but within the another ‘server’

Exactly I have four ‘servers’ with different root. Like
one.example.com’, ‘two.example.com’, ‘three.example.com’,
four.example.com’.
Each of them are similar and looks like I posted above as I said the
difference is only in root (location / { root xxx; }).

You say that verything is fine and should already work. Sounds cool.
I’m dummy with nginx and php-fpm.

Could you tell, please, how to enable debug log?

Thank you.

Francis D. Wrote:

There are some tidy-up fixes that could be done to
your configuration,
but what you have should be working already.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,222301,222315#msg-222315

Hello, Francis,

I found out how to enable it

error_log path/to/log;

Thank you.

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,222301,222316#msg-222316

On Sun, Feb 12, 2012 at 01:04:30PM -0500, dimentiy wrote:

Hi there,

Yes, I have another ‘location’ but within the another ‘server’

If you have no other location{} in the server{} block that is being
used,
and you can see from the access log that the browser is actually making
the
request and not using a cached version, then I’m afraid that I have no
idea what your system is doing.

Exactly I have four ‘servers’ with different root. Like
one.example.com’, ‘two.example.com’, ‘three.example.com’,
four.example.com’.
Each of them are similar and looks like I posted above as I said the
difference is only in root (location / { root xxx; }).

When I use a config like what you posted, I see no problem.

(Generally, you don’t put “root” inside a location{} block unless you
know why you are doing it. Leave it inside the server{} block and things
are clearer.)

Can you provide an exact config file that shows the problem, that I can
copy-paste and demonstrate the problem myself? Usually, if you begin
with a simple config file and just add a few lines at a time, it will
become clear at what point things stopped working as you expect.

You say that verything is fine and should already work. Sounds cool.
I’m dummy with nginx and php-fpm.

Using this config file

===
events {
worker_connections 1024;
debug_connection 127.0.0.1;
}

http {
server {
listen 8000;
server_name one.example.com;
root /usr/local/nginx/one;
index index.html index.htm index.php;

    location / {
    }
    location ~ php {
        fastcgi_pass   backend;
        include fastcgi.conf;
    }
}
server {
    listen       8000;
    server_name two.example.com;
    root /usr/local/nginx/two;
    index index.html index.htm index.php;

    location ~ php {
        fastcgi_pass   backend;
        include fastcgi.conf;
    }
}
upstream backend {
    server unix:php.sock;
}

}

I get the expected results from each of

curl -i -H Host:\ one.example.com http://localhost:8000/
curl -i -H Host:\ one.example.com http://localhost:8000/get.php
curl -i -H Host:\ one.example.com http://localhost:8000/sub/get.php

curl -i -H Host:\ two.example.com http://localhost:8000/
curl -i -H Host:\ two.example.com http://localhost:8000/sub/
curl -i -H Host:\ two.example.com http://localhost:8000/sub/index.php
curl -i -H Host:\ two.example.com http://localhost:8000/sub/get.php

where I see (respectively) the content of
/usr/local/nginx/one/index.htm,
and then the php-processed output of /usr/local/nginx/one/get.php,
/usr/local/nginx/one/sub/get.php, /usr/local/nginx/two/index.php,
/usr/local/nginx/two/sub/index.php (twice), and
/usr/local/nginx/two/sub/get.php,

The only difference between

include fastcgi.conf;

and

include fastcgi_params;

is that the former includes the line

fastcgi_param  SCRIPT_FILENAME 

$document_root$fastcgi_script_name;

so that it doesn’t have to be added separately.

(Note also that the “~ php” locations are probably not exactly what you
want on a live site.)

If you still have a problem with your configuration, please include the
usual debug details:

  • what did you do
  • what did you see
  • what did you expect to see

and someone may have a better chance of reproducing what you are seeing.

Could you tell, please, how to enable debug log?

http://nginx.org/en/docs/debugging_log.html
http://wiki.nginx.org/Debugging

Good luck with it,

f

Francis D. [email protected]

Thank you every body, people!

I’m quite stupid… I entirly can’t think… There was <? ?> short tags
disabled, all the scripts use short tags, but only the one I tested
locating in root directory had <?php long tag… because I write it
Damn

I wasted your and my time

Posted at Nginx Forum:
http://forum.nginx.org/read.php?2,222301,222321#msg-222321

On Sun, Feb 12, 2012 at 03:28:03PM -0500, dimentiy wrote:

Hi there,

I’m quite stupid… I entirly can’t think… There was <? ?> short tags
disabled, all the scripts use short tags, but only the one I tested
locating in root directory had <?php long tag… because I write it

Congratulations on finding that that was the problem.

Now that we know the answer, is there any way it could have been found
earlier? This could help the next person who has a similar problem and
is searching the mailing list archive.

I think that

curl -i http://one.example.com/stuff/new.php

would show the “X-Powered-By: PHP” header which indicates that the file
was handled by php (even if it was just a pass-through): and copying
new.php to new.txt and trying

curl -i http://one.example.com/stuff/new.txt

would show the same content but without the PHP header line.

Maybe that would point to the php configuration as being the next place
to look.

All the best,

f

Francis D. [email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs