# Nginx, PATH\_INFO, and url-rewriting: i must be missing something

**URL:** <https://www.ruby-forum.com/t/nginx-path-info-and-url-rewriting-i-must-be-missing-something/201962>\
**Category:** NGINX\
**Created:** [January 18, 2011, 7:46pm UTC](https://www.ruby-forum.com/t/nginx-path-info-and-url-rewriting-i-must-be-missing-something/201962 "2011-01-18T19:46:50Z")\
**Posts on this page:** 3\
**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:** [January 18, 2011, 7:46pm UTC](https://www.ruby-forum.com/t/nginx-path-info-and-url-rewriting-i-must-be-missing-something/201962/1 "2011-01-18T19:46:50Z")

</div>

I’m switching from Apache to Nginx/fcgi and I’m having a small issue  
while attempting to setup the applications rewrite rules.

The code handles routes via the use of PATH\_INFO, e.g.,  
[example.com/foo/bar/](http://example.com/foo/bar/) will be routed to [example.com/index.php/foo/bar/](http://example.com/index.php/foo/bar/)

I’ve modified the server configuration to pass PATH\_INFO:

```auto
  location ~ \.php$ {
          include /etc/nginx/fastcgi_params;
          fastcgi_pass 127.0.0.1:9000;
          fastcgi_index index.php;

          fastcgi_split_path_info ^(.+\.php)(.*)$;
          fastcgi_param SCRIPT_FILENAME
/var/www/public$fastcgi_script_name;
          fastcgi_param PATH_INFO $fastcgi_path_info;
          fastcgi_param PATH_TRANSLATED
$document_root$fastcgi_path_info;
  }
```

And the rewrite handler:

```auto
  location / {
          root /var/www/public;
          index index.html index.htm index.php;

          if (!-f $request_filename) {
            rewrite ^(.*)/(.*)$ /index.php/$2 break;
          }
        }
```

With ‘ **rewrite\_log on** ’, the url’s appear to be routing properly:

```auto
    [error]: *2 open() "/var/www/public/index.php/test" failed (20: Not
a directory), client: 192.168.0.254, server: example.com, request: "GET
/test HTTP/1.1", host: "example.com", referrer:
"http://example.com/index.php"
```

However, it appears to be looking for the directory “test”. How can I  
force it to request index.php, passing ‘/test’ to the script?

Posted at Nginx Forum:

> **[Nginx, PATH\_INFO, and url-rewriting: i must be missing something.](https://forum.nginx.org/read.php?2,167319,167319#msg-167319)**
>
> I'm switching from Apache to Nginx/fcgi and I'm having a small issue while attempting to setup the applications rewrite rules. The code handles routes via the use of PATH\_INFO, e.g., example.com/foo/bar/ will be routed to...

---

<div class="post-metadata">

**Author:** ![uid\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/c6cbf5/32.png) [@uid\_b](https://www.ruby-forum.com/u/uid_b)\
**Post date:** [January 18, 2011, 8:29pm UTC](https://www.ruby-forum.com/t/nginx-path-info-and-url-rewriting-i-must-be-missing-something/201962/2 "2011-01-18T20:29:31Z")

</div>

On Tue, Jan 18, 2011 at 01:45:35PM -0500, uid\_b wrote:

Hi there,

> I’m switching from Apache to Nginx/fcgi and I’m having a small issue  
> while attempting to setup the applications rewrite rules.
> 
> The code handles routes via the use of PATH\_INFO, e.g.,  
> [example.com/foo/bar/](http://example.com/foo/bar/) will be routed to [example.com/index.php/foo/bar/](http://example.com/index.php/foo/bar/)

> [code]  
> location ~ .php$ {

This location matches a url that ends in the four characters “.php”.

> [/code]  
> With ‘ **rewrite\_log on** ’, the url’s appear to be routing properly:
> 
> [code]  
> [error]: \*2 open() “/var/www/public/index.php/test” failed (20: Not

This url is “/index.php/test”. Note that is does not end in the four  
characters “.php”, and so will not be handled by the previous location  
block.

> However, it appears to be looking for the directory “test”. How can I  
> force it to request index.php, passing ‘/test’ to the script?

Change the “location” to match what you want it to match.

As a quick test, make it just match “php” to confirm that that works as  
you want. And then tighten the config so that it won’t match things you  
don’t want it to – perhaps “starts with /index.php/” is appropriate?

Good luck,

## f

Francis D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![uid\_b](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/c6cbf5/32.png) [@uid\_b](https://www.ruby-forum.com/u/uid_b)\
**Post date:** [January 20, 2011, 11:14pm UTC](https://www.ruby-forum.com/t/nginx-path-info-and-url-rewriting-i-must-be-missing-something/201962/3 "2011-01-20T23:14:00Z")

</div>

Thanks for all the help guys! Everything is working great now 🙂

Posted at Nginx Forum:

> **[Re: Nginx, PATH\_INFO, and url-rewriting: i must be missing something.](https://forum.nginx.org/read.php?2,167319,168074#msg-168074)**
>
> I'm switching from Apache to Nginx/fcgi and I'm having a small issue while attempting to setup the applications rewrite rules. The code handles routes via the use of PATH\_INFO, e.g., example.com/foo/bar/ will be routed to...
