Closed winlinvip closed 3 years ago
In the nginx.org documentation, an article introducing nginx is excerpted. The article is titled Chapter "nginx" in "The Architecture of Open Source Applications", and it is written very well. Let's read it together.
there is always room for improvement
, the wheels of history always keep rolling forward without stopping.TRANS_BY_GPT3
The rewrite module of Nginx is a module that allows rewriting paths. In the configuration of Nginx, it can be seen that besides supporting regular expressions with PCRE, it can also configure complex processes. For example, the "if" statement can rewrite paths under different conditions.
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;
}
This is already equivalent to parsing some logic. Besides supporting the Lua extension language in OpenResty, Nginx also supports njs, which is JavaScript.
SRS does not support scripting languages. Currently, HTTP(S) serves as the common language between systems, and I believe integrating with HTTP is a more suitable approach. However, Nginx has done an excellent job in terms of configuration and scripting language extensions, which is admirable and worth learning from.
TRANS_BY_GPT3
Nginx can support WebSocket proxying, please refer to WebSocket proxying.
For example, to convert a live HTTP-FLV stream http://localhost:8082/live/livestream.flv
into a WebSocket stream ws://localhost:8081/live/livestream.flv
using videojs-flow, and then proxy it with Nginx to ws://localhost:8080/live/livestream.flv
.
The SRS configuration is as follows:
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;
}
}
}
Start videojs-flow/demo/mse.go to convert HTTP-FLV to WS-FLV.
go get golang.org/x/net/websocket &&
go run mse.go -l 8081 -b 8082
Configure Nginx to reverse proxy WS-FLV as follows:
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";
}
}
}
Use bilibili/flv.js to play the stream address: ws://localhost:8080/live/livestream.flv
TRANS_BY_GPT3
Almost all SRS have been completed.
TRANS_BY_GPT3
Learn where Nginx performs better than SRS and the areas worth studying.
TRANS_BY_GPT3