Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

im having difficulties with a relativity simple rewrite rules / router in nginx config.

All I want to do is, if requested dir or file 'host/my/request/path[/[index.php]]' does not exist, rewrite to 'host/my/request/path.php'

Current rewrite works for:


But wont work for:


Here is the rewrite part of the config:

    location / {
             try_files $uri/ $uri $uri.php;

Error log will report:

Access forbidden by rule, request: "GET /my/request/path/ HTTP/1.0"

Hm, is there a better way to solve this or get rid of the trailing slash?

edit, rules more elaborative:

host[/] >  host/index.php
host/index[/] >  host/index.php
host/my/path[/] > if /path/index.php exists: host/my/path/index.php
else host/my/path.php
share|improve this question


I added

rewrite ^/(.*)/$ /$1;

before the rule in first post, seems to be working.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.