# Index and location not working together properly?

**URL:** <https://www.ruby-forum.com/t/index-and-location-not-working-together-properly/235966>\
**Category:** NGINX\
**Created:** [July 26, 2013, 12:20pm UTC](https://www.ruby-forum.com/t/index-and-location-not-working-together-properly/235966 "2013-07-26T12:20:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![alcina](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a587f6/32.png) [@alcina](https://www.ruby-forum.com/u/alcina)\
**Post date:** [July 26, 2013, 12:20pm UTC](https://www.ruby-forum.com/t/index-and-location-not-working-together-properly/235966/1 "2013-07-26T12:20:37Z")

</div>

Hi all,

Still fighting with my nginx configuration. What I want to achieve.

If static file exists in “reseller” root,  
serve from “reseller” root  
else if static file file exists in “central” root,  
then serve from “Central” root  
else  
reply with 404  
endif  
If php file exists in “reseller” root,  
serve from “reseller” root via FCGI  
else if php file file exists in “central” root,  
then serve from “Central” root via FCGI  
else  
reply with 404  
endif  
if no query given  
serve “index.php” according to above rules.  
end if

I have got everything working, except the last bit. If index.php exists  
ONLY in the central root,  
then I get a 403. I’ve tried rewrite. I’ve tried index. Tried  
“try\_files $uri index.php”.

Below I have tried a special location to force things - it doesn’t work  
either.

Here is my config

# reseller on anake

# 

# This is the development version - in reseller and served without

ssl!

# 

server {  
server\_name reseller.anake.hcs;  
listen 80;  
fastcgi\_read\_timeout 300;  
index index.php;  
root /home/ian/websites/reseller/htdocs;  
# if / then redirect to index.php  
location = / {  
# serve /index.php  
rewrite ^$ /index.php last;  
}  
# if local php file exits, serve with fcgi  
location ~ .php$ {  
try\_files $uri $uri/ @masterphp;  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_param SCRIPT\_FILENAME  
$document\_root$fastcgi\_script\_name;  
include /etc/nginx/fastcgi\_params;  
}  
# serve php file from master root  
location @masterphp {  
root /home/ian/websites/coachmaster3dev/htdocs;  
try\_files $uri /index.php =404;  
fastcgi\_pass 127.0.0.1:9000;  
fastcgi\_param SCRIPT\_FILENAME  
$document\_root$fastcgi\_script\_name;  
include /etc/nginx/fastcgi\_params;  
}  
# serve local static files if they exist  
try\_files $uri @masterstatic;  
# switch to master set  
location @masterstatic {  
root /home/ian/websites/coachmaster3dev/htdocs;  
try\_files $uri =404;  
}  
}

reseller.anake.hcs/index.php results in  
if reseller/htdocs/index.php exists it is served - correctly  
if reseller/htdocs/index.php does not exist and  
coachmster3dev/htdocs/index.php exists, then the coachmaster3 version  
is served - correctly  
However  
reseller.anake.hcs results in  
if reseller/htdocs/index.php exists it is served - correctly  
if reseller/htdocs/index.php does not exist and  
coachmster3dev/htdocs/index.php exists, then I get a 403 Forbidden. It  
should serve the coachmater3dev version.

I have checked many times, and there are NO permission problems on  
directories and files.

So what is going wrong. It appears as if try\_files ?? ?? @somewhere  
; disables index.

Thanks for your help.

Ian

p.s. Is there an alternate approach that I might try?

–  
Ian H.  
31 Sheerwater, Northampton NN3 5HU,  
Tel: 01604 513875  
Preparing eBooks for Kindle and ePub formats to give the best reader  
experience.

---

<div class="post-metadata">

**Author:** ![dcian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@dcian](https://www.ruby-forum.com/u/dcian)\
**Post date:** [July 26, 2013, 5:25pm UTC](https://www.ruby-forum.com/t/index-and-location-not-working-together-properly/235966/2 "2013-07-26T17:25:37Z")

</div>

Hello!

On Fri, Jul 26, 2013 at 11:19:45AM +0100, Ian H. wrote:

[…]

> ```
> root /home/ian/websites/reseller/htdocs;
> # if / then redirect to index.php
> location = / {
> # serve /index.php
> rewrite ^$ /index.php last;
> 
> ```

The rewrite here does nothing as only URI “/” may appear here, and  
it’s not matched by the “^$” pattern.

You probably want to change it to

```
     rewrite ^ /index.php last;

```

[…]

–  
Maxim D.

> **[nginx: donation](http://nginx.org/en/donation.html)**

---

<div class="post-metadata">

**Author:** ![dcian](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/91b2a8/32.png) [@dcian](https://www.ruby-forum.com/u/dcian)\
**Post date:** [July 26, 2013, 9:51pm UTC](https://www.ruby-forum.com/t/index-and-location-not-working-together-properly/235966/3 "2013-07-26T21:51:30Z")

</div>

Hi Maxim,

Thank you. That worked a treat.

Regards

Ian

On 26/07/2013 16:25, Maxim D. wrote:

> > ```
> > rewrite ^$ /index.php last;
> > 
> > ```
> 
> The rewrite here does nothing as only URI “/” may appear here, and  
> it’s not matched by the “^$” pattern.
> 
> You probably want to change it to
> 
> ```
> rewrite ^ /index.php last;
> 
> ```
> 
> […]

–  
Ian H.  
31 Sheerwater, Northampton NN3 5HU,  
Tel: 01604 513875  
Preparing eBooks for Kindle and ePub formats to give the best reader  
experience.
