Strange rewrite behaviour

Greetings again,

I got this weird problem. I have the following virtual domain
configuration:


server {
listen 80;
server_name 2.socialwhale.com;
access_log
/var/www/cluster-domains/2_socialwhale_com/logs/2.socialwhale.log main;
error_log
/var/www/cluster-domains/2_socialwhale_com/logs/2.socialwhale.err;
index index.html index.htm index.php;
root /var/www/cluster-domains/2_socialwhale_com/htdocs/website;

location ~ .php$ {
root
/var/www/cluster-domains/2_socialwhale_com/htdocs/website;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
/var/www/cluster-domains/2_socialwhale_com/htdocs/website/$fastcgi_script_name;
include fastcgi_params;
}

     error_page  404              /404.html;
      location = /404.html {
         root   /usr/share/nginx/html;
     }

location /var/www/cluster-domains/2_socialwhale_com/htdocs/website {
auth_basic “Restricted”;
auth_basic_user_file
/var/www/cluster-domains/2_socialwhale_com/htpasswd;
}

}


Now if i add the following just after the error_page definition

location / {
try_files $uri $uri/ index.php?$uri&$args;
}

Nginx says the configuration is ok but php-cgi crashes…

Just so you know i am using those lines to other virtual domains with no
problem.

Any ideas?

Alex

On Mon, Mar 14, 2011 at 05:37:28PM +0200, Alexander Economou wrote:

Hi there,

a few suggestions first, which are not related to the reported
problem:


server {
listen 80;
server_name 2.socialwhale.com;
access_log
/var/www/cluster-domains/2_socialwhale_com/logs/2.socialwhale.log main;
error_log
/var/www/cluster-domains/2_socialwhale_com/logs/2.socialwhale.err;
index index.html index.htm index.php;
root /var/www/cluster-domains/2_socialwhale_com/htdocs/website;

Setting “root” there is good. If you don’t set it inside a particular
location{} block, that value will be inherited (and available as
$document_root)…

location ~ .php$ {
root
/var/www/cluster-domains/2_socialwhale_com/htdocs/website;

…so you could safely omit that line…

        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME

/var/www/cluster-domains/2_socialwhale_com/htdocs/website/$fastcgi_script_name;

…and shorten that line. And then, in future, if you switch directory,
you’ll have fewer places to update in the config file.

        include        fastcgi_params;

Or, instead, you could “include fastcgi.conf” and omit the
SCRIPT_FILENAME
line altogether.

auth_basic_user_file

/var/www/cluster-domains/2_socialwhale_com/htpasswd;
}

That block probably does not do what you want it to. “location” acts on
urls, not file paths. But it’s also unrelated to the reported problem.

Now if i add the following just after the error_page definition

location / {
try_files $uri $uri/ index.php?$uri&$args;
}

Nginx says the configuration is ok but php-cgi crashes…

I’d probably use “/index.php” instead of “index.php” up there. Depending
on what you mean by “php-cgi crashes”, maybe that will help?

If not, then details of what you see vs what you expect to see will
probably help find the problem.

Good luck with it,

f

Francis D. [email protected]

Thank you for your quick reply again Francis.

I followed your instructions and made the following changes


root

/var/www/cluster-domains/2_socialwhale_com/htdocs/website;

Commented the above line since i have already set the root dir


         fastcgi_pass   127.0.0.1:9000;
         fastcgi_index  index.php;

fastcgi_param SCRIPT_FILENAME

/var/www/cluster-domains/2_socialwhale_com/htdocs/website/$fastcgi_script_name;

Commented SCRIPT_FILENAME and added the line below :


         include        fastcgi.conf;

As for the authentication part now , the following works as a charm on a
cacti installation but not in the specified virtualhost
(2.socialwhale.com)

location /var/www/html/cacti {
auth_basic “Restricted”;
auth_basic_user_file /var/www/html/cacti/.htpasswd;
}

As for the rewrite rule i tried your recommendation like below :


location / {
try_files $uri $uri/ /index.php?$uri&$args;
}

but still php-cgi does not respond to any .php and after some minutes i
get 402 gateway error.

Alex

On Mon, Mar 14, 2011 at 07:19:04PM +0200, Alexander Economou wrote:

Hi there,

Thank you for your quick reply again Francis.

You’re welcome.

        fastcgi_pass   127.0.0.1:9000;

Do you have the fastcgi server running on this address? Does that server
generate any logs to report if it exits?


location / {
try_files $uri $uri/ /index.php?$uri&$args;
}

but still php-cgi does not respond to any .php and after some minutes i
get 402 gateway error.

You restart or reload nginx, then you access a particular url, and you
do not get the response you expect? The best I can suggest is “check
the logs”.

If the problem is with the fastcgi server exiting, then you’ll need to
investigate that. But so far, I fail to reproduce your problem.

Good luck with it,

f

Francis D. [email protected]