# REST URLs and NGINX (config help needed)

**URL:** <https://www.ruby-forum.com/t/rest-urls-and-nginx-config-help-needed/244788>\
**Category:** NGINX\
**Created:** [June 14, 2016, 7:29pm UTC](https://www.ruby-forum.com/t/rest-urls-and-nginx-config-help-needed/244788 "2016-06-14T19:29:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rafa\_F](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/958977/32.png) [@Rafa\_F](https://www.ruby-forum.com/u/Rafa_F)\
**Post date:** [June 14, 2016, 7:29pm UTC](https://www.ruby-forum.com/t/rest-urls-and-nginx-config-help-needed/244788/1 "2016-06-14T19:29:36Z")

</div>

Hi,

Based on scraps found in the NGINX docs, I have a semi-working config  
that looks as follows :

##### 

location /demo {  
allow 10.0.0.0/8;  
deny all;  
try\_files $uri @pdemo;  
}  
location @pdemo {  
fastcgi\_param SCRIPT\_FILENAME /path/to/demo.php;  
fastcgi\_param SCRIPT\_NAME /demo.php;  
fastcgi\_param PATH\_INFO $fastcgi\_path\_info;  
fastcgi\_pass unix:/var/run/php/php7.0-fpm.sock;  
fastcgi\_param QUERY\_STRING $args;  
include fastcgi\_params;  
}

##### 

I say this config is “semi-working” because :

A base-call to [http://example.com/demo](http://example.com/demo) returns the hello-world output  
from my PHP router. No problems there.

But, if I call, say, [http://example.com/demo/hello/x](http://example.com/demo/hello/x), the PHP router  
doesn’t match the route (despite expecting hello/x as the path).

If it helps, I’m using bramus router and my configured paths are as  
follows :

$router-\>get(‘/’, function() { echo “hello world”};  
$router-\>get(‘hello/(\w+)’, function($name) {echo “hello”.$name};

However bramus returns a 404, and it doesn’t matter if I prefix the  
hello path with a forwad slash in bramus.

Therefore I am guessing my nginx config is broken ?

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [June 14, 2016, 8:09pm UTC](https://www.ruby-forum.com/t/rest-urls-and-nginx-config-help-needed/244788/2 "2016-06-14T20:09:55Z")

</div>

Hi

On Wed, Jun 15, 2016, at 02:28, Ben wrote:

> }
> 
> follows :

Looks like the logic for getting base path is a bit interesting. Try  
this for related line:

fastcgi\_param SCRIPT\_NAME /demo/;

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [June 16, 2016, 3:11pm UTC](https://www.ruby-forum.com/t/rest-urls-and-nginx-config-help-needed/244788/3 "2016-06-16T15:11:57Z")

</div>

Hi

On Thu, Jun 16, 2016, at 22:06, Ben wrote:

> > 
> 
> for /demo/hello/x it prints “/demo/hello”
> 
> Which looks like their “auto base-path” is setting moving goalposts !!

That doesn’t make sense if you actually use my configuration since  
SCRIPT\_NAME is static.

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [June 16, 2016, 3:07pm UTC](https://www.ruby-forum.com/t/rest-urls-and-nginx-config-help-needed/244788/4 "2016-06-16T15:07:52Z")

</div>

> > If it helps, I’m using bramus router and my configured paths are as  
> > follows :
> 
> Looks like the logic for getting base path is a bit interesting. Try  
> this for related line:
> 
> fastcgi\_param SCRIPT\_NAME /demo/;

That didn’t work unfortunatley.

Just dug up their code and I see what you mean !

I put their “implode(‘/’, array\_slice(explode(‘/’,  
$\_SERVER[‘SCRIPT\_NAME’]), 0, -1)) . ‘/’;” into my PHP and

for /demo/ it prints “/demo/”  
and  
for /demo/hello/x it prints “/demo/hello”

Which looks like their “auto base-path” is setting moving goalposts !!

Any other alternative NGINX configs I could try ? I’ve tried reporting  
their base path but they’re not particularly interested in fixing it!

I have also tried :

try\_files $uri $uri/ /demo/demo.php;

But that only seems to work intermittently (which I guess is why NGINX  
recommend the original method I posted !).

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [June 16, 2016, 3:14pm UTC](https://www.ruby-forum.com/t/rest-urls-and-nginx-config-help-needed/244788/5 "2016-06-16T15:14:18Z")

</div>

Hi

On Thu, Jun 16, 2016, at 22:11, Edho A. wrote:

> 

Looking again, you probably need to include fastcgi\_params first.

Here’s the config I’m using:

12 location @pdemo {  
13 include fastcgi\_params;  
14 fastcgi\_param SCRIPT\_FILENAME /path/to/demo.php;  
15 fastcgi\_param SCRIPT\_NAME /demo/;  
16 fastcgi\_pass 127.0.0.1:9000;  
17 }

---

<div class="post-metadata">

**Author:** ![ben](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/df788c/32.png) [@ben](https://www.ruby-forum.com/u/ben)\
**Post date:** [June 16, 2016, 3:35pm UTC](https://www.ruby-forum.com/t/rest-urls-and-nginx-config-help-needed/244788/6 "2016-06-16T15:35:20Z")

</div>

On 16/06/2016 14:14, Edho A. wrote:

> > 
> 
> 14 fastcgi\_param SCRIPT\_FILENAME /path/to/demo.php;  
> 15 fastcgi\_param SCRIPT\_NAME /demo/;  
> 16 fastcgi\_pass 127.0.0.1:9000;  
> 17 }
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://mailman.nginx.org/mailman/listinfo/nginx)

Doing some testing but that seems to have done the trick in the few  
things I’ve tried so far.

Thanks !
