# Strange rewrite behaviour

**URL:** <https://www.ruby-forum.com/t/strange-rewrite-behaviour/204509>\
**Category:** NGINX\
**Created:** [March 14, 2011, 4:39pm UTC](https://www.ruby-forum.com/t/strange-rewrite-behaviour/204509 "2011-03-14T16:39:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [March 14, 2011, 4:39pm UTC](https://www.ruby-forum.com/t/strange-rewrite-behaviour/204509/1 "2011-03-14T16:39:24Z")

</div>

Greetings again,

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

* * *

server {  
listen 80;  
server\_name [2.socialwhale.com](http://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

---

<div class="post-metadata">

**Author:** ![Alexander\_Economou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@Alexander\_Economou](https://www.ruby-forum.com/u/Alexander_Economou)\
**Post date:** [March 14, 2011, 5:51pm UTC](https://www.ruby-forum.com/t/strange-rewrite-behaviour/204509/2 "2011-03-14T17:51:05Z")

</div>

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](http://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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Alexander\_Economou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@Alexander\_Economou](https://www.ruby-forum.com/u/Alexander_Economou)\
**Post date:** [March 14, 2011, 6:21pm UTC](https://www.ruby-forum.com/t/strange-rewrite-behaviour/204509/3 "2011-03-14T18:21:11Z")

</div>

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](http://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

---

<div class="post-metadata">

**Author:** ![Alexander\_Economou](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@Alexander\_Economou](https://www.ruby-forum.com/u/Alexander_Economou)\
**Post date:** [March 14, 2011, 7:08pm UTC](https://www.ruby-forum.com/t/strange-rewrite-behaviour/204509/4 "2011-03-14T19:08:54Z")

</div>

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. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
