如何在 Nginx 中为 Janus REST api 和 socket api 设置反向代理?

Posted

技术标签:

【中文标题】如何在 Nginx 中为 Janus REST api 和 socket api 设置反向代理?【英文标题】:How can I set up a reverse proxy for the Janus REST api and socket api in Nginx? 【发布时间】:2021-01-22 20:19:30 【问题描述】:

我有一个 Janus 网关,它在端口 8088 上公开了一个 REST api。在我的 janus 服务器上的 8188 端口上也启用了 Web 套接字传输。我为 https 流量设置了一个 nginx 反向代理以到达我的 Janus 服务器。如何向我的 Nginx 反向代理添加 wss 支持?这是我在 nginx/sites-available 中的配置文件“janusserver5.example.com”:

server 
    server_name   janusserver5.example.com;
    location / 
        proxy_pass         http://10.10.30.27:8088;


        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_set_header   Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
    


    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/janusserver5.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/janusserver5.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot



server 
    if ($host = janusserver5.example.com) 
        return 301 https://$host$request_uri;
     # managed by Certbot


    server_name   video518.doctogether.com;
    listen 80;
    return 404; # managed by Certbot



【问题讨论】:

【参考方案1】:

默认情况下,Janus REST api 位于 /janus 端点。要允许 Nginx 代理 Web 套接字和 REST 接口,请为 /janus 创建一个位置条目,该条目传递给 http://yourip:8088/janus,第二个位置条目用于 / 传递给 http://yourip:8188。

server 
server_name   janusserver5.example.com;
location /janus 
   proxy_pass         http://10.10.30.20:8088/janus;
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection keep-alive;
    proxy_set_header   Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;


location / 
    proxy_pass        http://10.10.30.20:8188;
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection keep-alive;
    proxy_set_header   Host $host;
    proxy_cache_bypass $http_upgrade;
    proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header   X-Forwarded-Proto $scheme;


    # WebSocket support
    proxy_set_header Connection "upgrade";
    proxy_read_timeout  90;



listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/janusserver5.example.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/janusserver5.example.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot



server 
    if ($host = janusserver5.example.com) 
        return 301 https://$host$request_uri;
     # managed by Certbot


    server_name   janusserver5.example.com;
    listen 80;
    return 404; # managed by Certbot


 

通过此配置,我现在可以使用协议“janus-protocol”连接到 https://janusserver5.example.com/janus/info 和 wss://janusserver5.example.com

【讨论】:

以上是关于如何在 Nginx 中为 Janus REST api 和 socket api 设置反向代理?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 django-rest-framework 中为 API 使用 TokenAuthentication

如何在 spring-mvc 中为 REST 查询提供对象列表?

如何在scala akka(spray)中为rest服务编写测试用例

如何在 Django Rest Framework 的 REST API 中为数组数据编写序列化程序?

请求令牌时如何在 Azure 存储 Blob 中为 REST 请求指定范围? [AZURE-BLOB][REST API]

如何在url中为rest服务的get方法传递逗号分隔的参数