2

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.

1
  • Yes, I made a location ~ \.php to process PHP files in the root directory, and those work fine. Commented Feb 21, 2013 at 15:12

2 Answers 2

2

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 { }
Sign up to request clarification or add additional context in comments.

3 Comments

That fixed it, thank you! I guess it's because I'm not serving PHP files directly with an extension .php?
From try_files documentation: the processing is performed in the current context. And note, that every request is processed only in one location context. In your configuration it was either location \.php or location ~ /home/.
It's also worth mentioning that I had everything right but the php code was using php short tags so I had to set short_open_tag=On in /etc/php5/fpm/php.ini then restart service php5-fpm restart
-1

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;

4 Comments

When I removed the @404 from the try_files it still brought up the page with the PHP source code.
So when you visit something at /home/page.php it works fine?
When I visit something at /home.php it works fine, but that is because I have a location directive specifically for the / directory, but not other ones. I tried to change the location directive to reflect /home/ or /main/home/ but neither work.
Please write your answer such that it makes sense for the first time reader; the history lets anyone see what changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.