# JSON REST APIs

**URL:** <https://www.ruby-forum.com/t/json-rest-apis/236682>\
**Category:** NGINX\
**Created:** [September 25, 2013, 2:40am UTC](https://www.ruby-forum.com/t/json-rest-apis/236682 "2013-09-25T02:40:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![addis\_a](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/c4cdca/32.png) [@addis\_a](https://www.ruby-forum.com/u/addis_a)\
**Post date:** [September 25, 2013, 2:40am UTC](https://www.ruby-forum.com/t/json-rest-apis/236682/1 "2013-09-25T02:40:53Z")

</div>

Hi,

Are there any JSON APIs defined for Nginx Configuration?

Regards,  
John

---

<div class="post-metadata">

**Author:** ![Myla\_John-B22173](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@Myla\_John-B22173](https://www.ruby-forum.com/u/Myla_John-B22173)\
**Post date:** [September 27, 2013, 1:22pm UTC](https://www.ruby-forum.com/t/json-rest-apis/236682/2 "2013-09-27T13:22:52Z")

</div>

I have many “virtual” paths on one nginx server. What I mean by this is  
there can be many top level paths, where each one has a cookie, static  
files, and a upstream server. The way I am doing it now is just  
duplicate every path.

Is there a way to do this ‘faster’ ‘less writing’ ‘better’? I am  
expecting 1000’s of entries.

upstream a1 {

# server localhost:48000;

```
 unix:/tmp/a1

```

}  
upstream a2 {  
server localhost:48001;

# unix:/tmp/a2

}  
upstream a3 {

# server localhost:48002;

```
 unix:/tmp/a3

```

}

location /a1/exe { # to upstream if have cookie  
if ($http\_cookie !~\* ‘a1’) {  
rewrite ^a1(.\*)$ /login?a1=$1;  
}  
proxy\_… #setup  
proxy\_pass [http://a1](http://a1);  
}

location /a1 { # static files  
if ($http\_cookie !~\* ‘a1’) {  
rewrite ^a1(.\*)$ /login?a1=$1;  
}  
alias /var/www/a1  
}

location /a2/exe { # to upstream if have cookie  
if ($http\_cookie !~\* ‘a2’) {  
rewrite ^a2(.\*)$ /login?a2=$1;  
}  
proxy\_… #setup  
proxy\_pass [http://a2](http://a2);  
}

location /a2 { #static files  
if ($http\_cookie !~\* ‘a2’) {  
rewrite ^a2(.\*)$ /login?a2=$1;  
}  
alias /var/www/a2  
}

…  
IDEA

* * *

not correct!!  
location / {  
parse url  
check cookie  
if $2 == ‘exe’ {  
proxy\_pass  
}  
alias /var/www/$2 OR rewrite /var/www/$2  
}

---

<div class="post-metadata">

**Author:** ![Myla\_John-B22173](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@Myla\_John-B22173](https://www.ruby-forum.com/u/Myla_John-B22173)\
**Post date:** [September 27, 2013, 7:35pm UTC](https://www.ruby-forum.com/t/json-rest-apis/236682/3 "2013-09-27T19:35:48Z")

</div>

On Fri, Sep 27, 2013 at 04:22:25AM -0700, Shaun Savage wrote:

Hi there,

None of what follows is tested by me. So double-check before committing  
to anything 🙂

> I have many “virtual” paths on one nginx server. What I mean by this is  
> there can be many top level paths, where each one has a cookie, static  
> files, and a upstream server. The way I am doing it now is just  
> duplicate every path.
> 
> Is there a way to do this ‘faster’ ‘less writing’ ‘better’? I am  
> expecting 1000’s of entries.

Faster for you to write, or faster for nginx to read and process?

Less writing for you, or for your “turn this input file into an  
nginx.conf” script?

It looks like you could either auto-generate the conf file, so that it  
will be big; or keep it small by using a (probably, nested) “location”  
with regex and named captures.

After you have both, you can test your workload on your hardware to see  
if there is a significant benefit of one over the other to you.

Or you can just make one, and see if your workload on your hardware is  
handled well enough, and stop if it is.

The examples here look like there is a regular pattern to them. Assuming  
that holds for all, then a simple template / macro system that does  
a string replacement should work – you keep your “real config” as a  
list of a1, a2, etc; then run the script to generate the fragment of  
nginx.conf that can be pasted in or "include"d.

Or you could try something like

==  
location / {  
location ~ ^/(?[^/]\*)/exe {

# your “exe” stuff goes here, with $bit = a1 or a2

}  
location ~ ^/(?[^/]\*) {

# your “static” stuff goes here, with $bit = a1 or a2

# } }

but even with that, you’re likely to want to auto-generate the  
“upstream”  
sections externally to nginx anyway.

(You’ll need other top-level location{} blocks for anything that should  
not match the pattern you have shown, such as /login.)

All the “alias” lines you’ve shown look equivalent to a single “root  
/var/www” at server-level – that might simplify things, depending on  
what else you plan to use the server for.

## f

Francis D. removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Myla\_John-B22173](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@Myla\_John-B22173](https://www.ruby-forum.com/u/Myla_John-B22173)\
**Post date:** [September 30, 2013, 9:19am UTC](https://www.ruby-forum.com/t/json-rest-apis/236682/4 "2013-09-30T09:19:30Z")

</div>

I am have a problem

I am very confused. There is an extra “?” added to the end of the  
rewrite. I have no idea why, I look at the logs, and is magically  
appears.

I want to reload the login page, but nothing happens, any ideas?

```
     location /login/ {
             if ($args) {
                     set $lin 1;
                     rewrite ^/login/login(.*)$ /auth$1;
             }
             alias /var/www/login/;

     }

     location /hzc/ws {
             if ($http_cookie !~* 'hzc') {
                     rewrite ^/hzc(.*)$ /login/;
             }
             proxy_redirect off;
             proxy_buffering off;
             proxy_set_header Host $host;
             proxy_set_header X-Real-IP $remote_addr;
             proxy_set_header X-Real-Port $remote_port;
             proxy_set_header X-Forwarded-For

```

$proxy\_add\_x\_forwarded\_for;  
proxy\_set\_header Upgrade $http\_upgrade;  
proxy\_set\_header Connection “upgrade”;  
proxy\_http\_version 1.1;  
proxy\_pass\_request\_headers on;  
proxy\_pass [http://gofw](http://gofw);  
}

---

<div class="post-metadata">

**Author:** ![Myla\_John-B22173](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e480ec/32.png) [@Myla\_John-B22173](https://www.ruby-forum.com/u/Myla_John-B22173)\
**Post date:** [September 30, 2013, 2:20pm UTC](https://www.ruby-forum.com/t/json-rest-apis/236682/5 "2013-09-30T14:20:26Z")

</div>

Hello!

On Mon, Sep 30, 2013 at 12:18:41AM -0700, shaun wrote:

> I am very confused. There is an extra “?” added to the end of the  
> rewrite. I have no idea why, I look at the logs, and is magically  
> appears.

Could you please point out where is the “?” which confuses you?  
Please note that in debug logs like

> 2013/09/29 23:42:00 [debug] 3386#0: \*1 internal redirect: “/login/index.html?”

the “?” character is printed unconditionally as a separator  
between r-\>uri and r-\>args.

> I want to reload the login page, but nothing happens, any ideas?

As per debug log you’ve provided, the “/var/www/login/index.html”  
file is properly returned as configured. Not sure what you want  
nginx to do instead.

–  
Maxim D.  
[http://nginx.org/en/donation.html](http://nginx.org/en/donation.html)
