janus behind nginx

6,374 views
Skip to first unread message

m.j.s...@gmail.com

unread,
Mar 19, 2015, 2:36:58 PM3/19/15
to meetech...@googlegroups.com
When you use nginx as a reverse proxy you use proxy_pass to forward all requests to a location directory. 

smith[11:22 AM]So to forward requests coming in on port 80 to a janus gateway listening on port 8088 by default you'd think you'd do this

smith[11:24 AM]
```location /janus/ {
proxy_pass         http://127.0.0.1:8088;}```


smith[11:26 AM]where /janus is the home of the janus API (I simplified the above to omit other header re-writes). The problem is that the initial API request for a janus webRTC session goes not to a sub-directory of janus but to janus itself. Only leaf directory requests are accurately forwarded by nginx. So you have to do this 

smith[11:26 AM]
```location /janus/ {
proxy_pass         http://127.0.0.1:8088/;}```


smith[11:27 AM](note trailing /) and make the requests to janus/janus instead of janus (you do this by setting a variable var server = "/janus/janus"; in your .js). Then the proxy_pass above drops the first janus.

Lorenzo Miniero

unread,
Mar 19, 2015, 3:04:35 PM3/19/15
to meetech...@googlegroups.com
Shouldn't you do something like that instead?

location /janus/ {
proxy_pass         http://127.0.0.1:8088/janus/;}

When you configure a root in janus (in this case /janus) you're never going to GET or POST / directly.
That said, I'm not familiar with nginx so I wouldn't know. Other people in the group are using it in their servers so they may have better ideas.

L.

Nicholas Wylie

unread,
Mar 19, 2015, 6:13:05 PM3/19/15
to meetech...@googlegroups.com
I have nginx configured like this:

location /rtc {
   proxy_pass http
://127.0.0.1:8088/janus;
}


(Different endpoint from the client-side because I'm serving the Janus files on /janus)

m.j.s...@gmail.com

unread,
Mar 19, 2015, 7:02:58 PM3/19/15
to meetech...@googlegroups.com
Nicholas: what is your var server ?

Lorenzo: Thanks for your response! I may be wrong. A better description of my setup to avoid confusion is:


location /janusbase/ {
proxy_pass         http://127.0.0.1:8088/;}


and I use var server = "/janusbase/janus";


Michaels-MacBook-Air:~ mike$ curl-H "Content-Type: application/json" -d '{"janus":"create","transaction":"A3x4TXLyZ3Ev"}' http://192.168.0.224/janusbase/janus

{

   "janus": "success",

   "transaction": "A3x4TXLyZ3Ev",

   "data": {

      "id": 1904517468

   }

}

Michaels-MacBook-Air:~ mike$ 



What would var server be in your setup with location /janus/ and  proxy_pass http://127.0.0.1:8088/janus/; If it were var server = "/janus"; I don't think it works because you are trying to have nginx match on what it thinks is a page target name not a directory stem. See http://nginx.com/resources/admin-guide/reverse-proxy/


A request to http://192.168.0.224/janusbase/janus looks like a page to nginx but http://192.168.0.224/janusbase/ looks like a directory stem (I deliberately avoid the terms URL and URI here).


The problem, or part of it, seems to be described in http://nginx.org/en/docs/http/ngx_http_core_module.html#location, as follows:


If a location is defined by a prefix string that ends with the slash character, and requests are processed by one of proxy_pass, fastcgi_pass, uwsgi_pass, scgi_pass, or memcached_pass, then the special processing is performed. In response to a request with URI equal to this string, but without the trailing slash, a permanent redirect with the code 301 will be returned to the requested URI with the slash appended. If this is not desired, an exact match of the URI and location could be defined like this:

location /user/ {
    proxy_pass http://user.example.com;
}

location = /user {
    proxy_pass http://login.example.com;
}


It seems the difference between ProxyPass /path and ProxyPass /path/ in http://httpd.apache.org/docs/2.2/mod/mod_proxy.html is not well described or defined and neither is the difference between proxy_pass /path/ and proxy_pass /path in http://nginx.org/en/docs/http/ngx_http_proxy_module.html?&_ga=1.43766309.1651162240.1426448296#proxy_pass 


Does anyone else have an nginx setup with proxy_pass and var server they can share, please? 


I'm trying to get all traffic over port 80 so I can tunnel it. This would help Junichi Yamazaki in https://groups.google.com/forum/#!topic/meetecho-janus/luiHb6FM6qE 

m.j.s...@gmail.com

unread,
Mar 19, 2015, 7:26:46 PM3/19/15
to meetech...@googlegroups.com
The proxy setup described by Nicholas works with var server = "/rtc";

So does var server = "/janus"; as long as you have location /janus (with no trailing /, thats the key part, not that well documented) and proxy_pass http://127.0.0.1:8088/janus;

I may stick with my solution using location /janusbase/ with var server = "/janusbase/janus"; and proxy_pass http://127.0.0.1:8088/; otherwise I will never remember the issues.

Nicholas Wylie

unread,
Mar 19, 2015, 7:50:24 PM3/19/15
to meetech...@googlegroups.com
my janus.cfg has the default base_path "/janus", my javascript files are using:
var server = "https://" + window.location.hostname + "/rtc";
(nginx is setup to only serve pages over https)

I'm not sure on the semantics nginx uses with the trailing slash and the proxy_pass directive. AFAIK, it should use the "location" directive to match any incoming URI, and translate that URI relative to the hostname specified in the "proxy_pass" directive.

m.j.s...@gmail.com

unread,
Mar 19, 2015, 8:05:11 PM3/19/15
to meetech...@googlegroups.com
>>  it should use the "location" directive to match any incoming URI

Thats the tricky part. Your example really helped in figuring out what was going on with matching. I have solutions that work now though, including yours. Thats all I need for now. Thanks!
Reply all
Reply to author
Forward
0 new messages