Php not working from aliased subdir

Hello,

I have been adding (to my nginx config) directories outside of the
default “root” tree like:

 location ~ /newlocation(.*) {
     alias /var/websites/externaldir$1;
 }

This works OK.

however, I find that the above config does not process php files. I
tried adding (before the above):

 location ~ /newlocation/(.*)\.php$ {

     alias /var/websites/externaldir$1.php;

     fastcgi_cache off;

     try_files $uri =404;
     include /etc/nginx/fastcgi_params;
     fastcgi_param PATH_INFO $fastcgi_script_name;
     fastcgi_intercept_errors on;

     fastcgi_buffer_size 128k;
     fastcgi_buffers 256 16k;
     fastcgi_busy_buffers_size 256k;

     fastcgi_temp_file_write_size 256k;
     fastcgi_read_timeout 240;

     fastcgi_pass unix:/tmp/php-fpm.sock;

     fastcgi_index index.php;

 }

This config however always leads to “404 Not Found” errors for php
files.

What am I doing wrong?

Please correct me.

Thanks!
Nick

On Tue, Jan 19, 2016 at 11:21:46AM +0200, Nikolaos M. wrote:

Hi there,

location ~ /newlocation/(.*)\.php$ {

    alias /var/websites/externaldir$1.php;

    fastcgi_cache off;

    try_files $uri =404;
    include /etc/nginx/fastcgi_params;

This config however always leads to “404 Not Found” errors for php files.

What am I doing wrong?

add “debug_connection 127.0.0.12;” to your events{} section, then do
“curl -v http://127.0.0.12/newlocation/X.php”, and then read the error
log that was generated.

You should see, among other lines, something like

http request line: “GET /newlocation/X.php HTTP/1.1”
http uri: “/newlocation/X.php”
test location: ~ “/newlocation/(.).php$"
using configuration "/newlocation/(.
).php$”
try files phase: 11
trying to use file: “/newlocation/X.php”
“/var/websites/externaldirX.php/newlocation/X.php”
http special response: 404, “/newlocation/X.php?”

Two things there: “alias” and “try_files” is not a good combination;
and your config loses the “/” that should be between “externaldir” and
“X.php”.

If you temporarily remove the try_files line that is blocking you, and
repeat the “curl” command, then you will see what is sent to the fastcgi
server. Usually, the most interesting parameter is SCRIPT_FILENAME.

What value do you see for that? Is it exactly the filename that you want
your fastcgi server to process?

If not, what file do you want your fastcgi server to process?

After that, you can decide whether you want to use a try_files directive
in this location{} block, and if so, what exactly you want there.

(I tested this using a nginx/1.9.1, because that’s what I had
handy. Different versions may show different output.)

Good luck with it,

f

Francis D. [email protected]