josh
josh

Reputation: 10368

PHP not processing in Nginx

I have created a try_files directive that tries files in a different directory than the files are put in. I.e., if I visit www.example.com/home/main it will try the file /var/www/main/home/main.php. It works, but when I visit a page, it only shows the PHP source code and not the processed page. Here is my directive:

location ~ /home/ {
   index main.php
   try_files /main$uri.php @404;
   location \.php {
       fastcgi_pass php_server;
       include fastcgi_params;
   }
}

Any ideas on how I can get the page to process instead of just showing the PHP source?

Edit for additional information: I have tried making a location directive that looks like

location ~ /main/(.*)\.php {
       fastcgi_pass php_server;
       include fastcgi_params;
}

but this doesn't work either and the pages still show just the source code.

Upvotes: 2

Views: 3047

Answers (2)

VBart
VBart

Reputation: 15130

Of course it will show you source, since you don't have fastcgi_pass in your /home/ location.

Try this one:

location /home/ {
   try_files /main$uri.php @static;
   fastcgi_pass php_server;
   include fastcgi_params;
}

location @static { }

Upvotes: 2

Michael Marr
Michael Marr

Reputation: 2099

EDIT: Actually the below is wrong - the last parameter is always used as a redirect if the file can't be found anywhere else in the try_files directive. In this example, it is hitting the .php file, but not be processed, so the @404 wasn't being hit. However, if no @404 example exists, then a failure to find the file would blow up anyways.

In the event that no file is found, an internal redirect to the last parameter is invoked [...]The last parameter is the fallback URI and must exist, or else an internal error will be raised.

source: http://wiki.nginx.org/HttpCoreModule#try_files

The @404 at the end of your try_files directive will send the php file to be parsed by a location @404 directive, not your php directive that uses fastcgi.

Try dropping the @404 or simple doing: try_files $uri /main$uri.php;

Upvotes: -1

Related Questions