Nginx比SRS做得好的地方

nginx.org文档中,摘录了一篇nginx介绍的文章,Chapter “nginx” in “The Architecture of Open Source Applications”,这篇文章写得很赞,一起读读:

  • 14.1. Why Is High Concurrency Important? 介绍了Nginx到底解决了什么问题,为什么Apache就解决不了这些问题。
  • 14.2. Overview of nginx Architecture 介绍了Nginx的模块架构,和上下游的关系,Cache,Worker模型。
  • 14.3. nginx Configuration 讲配置的逻辑,没想到配置在Nginx中会有专门的一章介绍。
  • 14.4. nginx Internals 主要是讲Nginx Modules。Nginx的core主要是提供web server,web和mail proxy的功能,主要的功能是由模块和模块pipeline或chain组合完成,分为event modules, phase handlers, output filters, variable handlers, protocols, upstreams and load balancers等等。
  • 14.5. Lessons Learned there is always room for improvement,历史车轮总是在滚滚前行,从不停息。
 
@winlinvip  winlinvip added this to the  SRS 4.0 release milestone  on 14 Jan
@winlinvip
 
MemberAuthor

winlinvip commented on 14 Jan

Nginx的rewrite模块,就是重写path的模块,可以看到nginx在配置中,除了可以用PCRE支持正则表达式,还可以配置比较复杂的流程,比如if可以在不同条件下rewrite路径:

if ($http_user_agent ~ MSIE) {
    rewrite ^(.*)$ /msie/$1 break;
}

if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
    set $id $1;
}

if ($request_method = POST) {
    return 405;
}

if ($slow) {
    limit_rate 10k;
}

if ($invalid_referer) {
    return 403;
}

这已经相当于解析一部分逻辑了,除了openresty支持lua扩展语言,Nginx还支持了njs也就是javascript。

SRS不会支持脚本语言,目前HTTP(S)作为系统间的通用语言,我觉得HTTP集成是更合适的方式。当然Nginx在配置和脚本扩展语言方面,做得非常的好,值得钦佩和学习。

@winlinvip
 
MemberAuthor

winlinvip commented on 16 Jan • 

edited 

Nginx可以支持WebSocket代理,参考WebSocket proxying

举个栗子,将直播HTTP-FLV流http://localhost:8082/live/livestream.flv,使用videojs-flow转成WebSocket流ws://localhost:8081/live/livestream.flv,然后用Nginx代理成ws://localhost:8080/live/livestream.flv

SRS配置如下:

listen              1935;
max_connections     1000;
daemon              off;
srs_log_tank        console;
http_server {
    enabled         on;
    listen          8082;
}
vhost __defaultVhost__ {
    http_remux {
        enabled     on;
        mount       [vhost]/[app]/[stream].flv;
    }
    ingest livestream {
        enabled      on;
        input {
            type    file;
            url     ./doc/source.200kbps.768x320.flv;
        }
        ffmpeg      ./objs/ffmpeg/bin/ffmpeg;
        engine {
            enabled          off;
            output          rtmp://127.0.0.1:[port]/live?vhost=[vhost]/livestream;
        }
    }
}

启动videojs-flow/demo/mse.go,将HTTP-FLV转WS-FLV:

go get golang.org/x/net/websocket &&
go run mse.go -l 8081 -b 8082

配置Nginx,反向代理WS-FLV如下:

daemon off;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    server {
        listen       8080;
        location / {
            proxy_pass http://localhost:8081;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
}

使用bilibili/flv.js,播放流地址:ws://localhost:8080/live/livestream.flv

image

猜你喜欢

转载自www.cnblogs.com/xiami2046/p/12758346.html
srs