Cannot create directory alias (with php pages)

Hello,

I have a hard time trying to make a “directory alias” (in Apache
terminology - sorry, I still have a lot to learn on nginx)…

I have piwik.example.com, configured to run piwik analytics (it is a php
site), and it works fine.

I runs under nginx v1.6.1, php-fpm v5.3.3 on an updated CentOS 6.5 64bit
VPS.

Now, I want to create an /xhprof location, which includes php code and
lives in another disk path (in: /usr/share/xhprof/xhprof_html). I can’t
make this work…

I have tried multiple possible solutions, googled around, but it just
won’t work.

Follows the attempted config. In this, calling
http://piwik.example.com/xhprof or
http://piwik.example.com/xhprof/index.php returns “404 Not Found”.

The error log shows: "rewrite or internal redirection cycle while
internally redirecting to “/xhprof”.

server {

 listen [::]:80;

 server_name  piwik.example.com;
 access_log  /var/webs/wwwpiwik/log/access_log main;
 error_log /var/webs/wwwpiwik/log/error_log warn;
 root   /var/webs/wwwpiwik/www/;

 index  index.php index.html index.htm index.cgi default.html

default.htm default.php;

 location / {
    try_files $uri $uri/ /index.php?$args;
    allow 127.0.0.1;
    allow ::1;

allow 10.10.10.0/24;
deny all;
}

 location /xhprof {

    alias /usr/share/xhprof/xhprof_html;

    try_files $uri $uri/ /xhprof$is_args$args;

    allow 127.0.0.1;
    allow ::1;

allow 10.10.10.0/24;
deny all;
}

 location ~ \.php$ {

    allow 127.0.0.1;
    allow ::1;

allow 10.10.10.0/24;

    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 384k;
     fastcgi_buffers 256 16k;
     fastcgi_busy_buffers_size 384k;
     fastcgi_temp_file_write_size 384k;
     fastcgi_read_timeout 240;

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

     fastcgi_index index.php;
     include /etc/nginx/fastcgi_params;
     fastcgi_param SCRIPT_FILENAME 

$document_root$fastcgi_script_name;

 }

}

I appreciate your help in correcting the /xhprof definition, in order to
make it work…

I also

Thanks in advance,
Nick

hi!
if it can help
i just get a ‘redirection cycle’ error

in a *.conf

return 301 http://

but my problem was solved because i found in etc/nginx
some default.conf that made cycling www on a non-www

check all etc/nginx/
maybe not your case

alias /usr/share/xhprof/xhprof_html;
try_files $uri $uri/ /xhprof$is_args$args;

On 13/8/2014 11:42 μμ, Nikolaos M. wrote:

Now, I want to create an /xhprof location, which includes php code and
lives in another disk path (in: /usr/share/xhprof/xhprof_html). I
can’t make this work…

I have modified configuration as follows; the /xprof location works
(i.e. I can access non php files), but all php files cannot be accessed
(message: 404 Not Found). Nothing is recorded in the error log.

Why can’t I access the php files in /usr/share/xhprof/xhprof_htmlas
/xhprof/filename.php ?

Please advise!


server {

 listen [::]:80;

 server_name  piwik.example.com;
 access_log  /var/webs/wwwpiwik/log/access_log main;
 error_log /var/webs/wwwpiwik/log/error_log warn;
 root   /var/webs/wwwpiwik/www/;

 index  index.php index.html index.htm index.cgi default.html

default.htm default.php;

 location / {
    try_files $uri $uri/ /index.php?$args;
    allow 127.0.0.1;
    allow ::1;
    allow 10.10.10.0/24;
    deny all;
 }

 location ~ /xhprof(.+\.php)$ {

    alias /usr/share/xhprof/xhprof_html$1;

    allow 127.0.0.1;
    allow ::1;
    allow 10.10.10.0/24;
    deny all;

    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 384k;
     fastcgi_buffers 256 16k;
     fastcgi_busy_buffers_size 384k;
     fastcgi_temp_file_write_size 384k;
     fastcgi_read_timeout 240;

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

     fastcgi_index index.php;
     include /etc/nginx/fastcgi_params;
     fastcgi_param SCRIPT_FILENAME $document_root$1;
 }

 location ~ /xhprof(.*) {

     alias /usr/share/xhprof/xhprof_html$1;

     allow 127.0.0.1;
     allow ::1;
     allow 10.10.10.0/24;
     deny all;
 }

 location ~ \.php$ {

    allow 127.0.0.1;
    allow ::1;
    allow 10.10.10.0/24;
    deny all;

    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 384k;
     fastcgi_buffers 256 16k;
     fastcgi_busy_buffers_size 384k;
     fastcgi_temp_file_write_size 384k;
     fastcgi_read_timeout 240;

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

     fastcgi_index index.php;
     include /etc/nginx/fastcgi_params;
     fastcgi_param SCRIPT_FILENAME 

$document_root$fastcgi_script_name;

 }

}

On 14/8/2014 3:26 μμ, Francis D. wrote:

The combination of a regex location, alias, and try_files, probably does
not do what you want.

I suspect that the 404-with-no-error-log comes from this try_files line.

Thank you very much Francis,

I removed the try_files line and did the modifications you suggest (esp.
correction of the mistaken double inclusion of fastcgi_params you
noticed), and indeed it works. (I have not tested requesting a PHP page
with URL parameters yet.)

I have only decided to set:

fastcgi_param SCRIPT_FILENAME $request_filename;

following a suggestion from here:

http://blog.martinfjordvald.com/2011/01/no-input-file-specified-with-php-and-nginx/

rather than $document_root. Would you thing this setting is really
preferable/correct in our case or not?

Here is current configuration:

 location ~ /xhprof(.+\.php)$ {

    alias /usr/share/xhprof/xhprof_html$1;

    allow 127.0.0.1;
    allow ::1;
    allow 10.10.10.0/24;
    deny all;

    fastcgi_cache off;

    include /etc/nginx/fastcgi_params;

    fastcgi_param PATH_INFO $fastcgi_script_name;

    fastcgi_intercept_errors on;

    fastcgi_buffer_size 384k;
    fastcgi_buffers 256 16k;
    fastcgi_busy_buffers_size 384k;
    fastcgi_temp_file_write_size 384k;
    fastcgi_read_timeout 240;
    fastcgi_pass unix:/tmp/php-fpm.sock;

    fastcgi_index index.php;

    fastcgi_param SCRIPT_FILENAME $request_filename;
 }

Thanks again,
Nick

On Thu, Aug 14, 2014 at 02:06:38PM +0300, Nikolaos M. wrote:

On 13/8/2014 11:42 μμ, Nikolaos M. wrote:

Hi there,

I have modified configuration as follows; the /xprof location works
(i.e. I can access non php files), but all php files cannot be
accessed (message: 404 Not Found). Nothing is recorded in the error
log.

Why can’t I access the php files in /usr/share/xhprof/xhprof_htmlas
/xhprof/filename.php ?

The request is /xhprof/filename.php; that should be handled in this
location:

location ~ /xhprof(.+\.php)$ {
   alias /usr/share/xhprof/xhprof_html$1;
   try_files $uri =404;

The combination of a regex location, alias, and try_files, probably does
not do what you want.

I suspect that the 404-with-no-error-log comes from this try_files line.

You could (temporarily) either remove the line, or replace it with

try_files “” =404;

in order to get through to the next stage of the configuration.

   include /etc/nginx/fastcgi_params;
   fastcgi_param PATH_INFO $fastcgi_script_name;
    fastcgi_pass unix:/tmp/php-fpm.sock;
    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$1;

alias with variables (and possibly try_files, I don’t recall) probably
leave some of these variables in a state you don’t expect.

Your fastcgi server, when it receives repeated fastcgi params, will
probably only honour the first or the last one received. If you care,
test so that you know, or don’t send repeat params (by editing the
fastcgi_params files, and only including it once).

So: what file-on-the-filesystem do you want your fastcgi server to
process?

What variable or variables in nginx make up exactly that name?

Put that in as SCRIPT_FILENAME.

(It may be that $document_root is all you need here.)

You can see what nginx sends by looking in the debug log; or by watching
the traffic going from nginx to the fastcgi server. Or maybe your
fastcgi
server has its own logging.

f

Francis D. [email protected]

On Thu, Aug 14, 2014 at 04:04:47PM +0300, Nikolaos M. wrote:

On 14/8/2014 3:26 μμ, Francis D. wrote:

Hi there,

I have only decided to set:

fastcgi_param SCRIPT_FILENAME $request_filename;

following a suggestion from here:

rather than $document_root. Would you thing this setting is really
preferable/correct in our case or not?

I think it is both correct and preferable.

The variable has the right value (because you do
not care about the request “/dir/” indicating the file
“/usr/share/xhprof/xhprof_html/dir/index.php”); and it is clearer than
$document_root (which would normally be a directory name, if “alias”
were not involved here).

Cheers,

f

Francis D. [email protected]