309 lines
9.5 KiB
Markdown
309 lines
9.5 KiB
Markdown
---
|
|
title: HTTP Server
|
|
sidebar_label: HTTP Server
|
|
hide_title: false
|
|
hide_table_of_contents: false
|
|
---
|
|
|
|
# HTTP Server
|
|
|
|
SRS Embeded a HTTP web server, supports api and simple HTTP file for HLS.
|
|
|
|
To deploy SRS HTTP server, read [Usage: HTTP](./sample-http.md)
|
|
|
|
The SRS Embeded HTTP server is rewrite refer to go http module, so it's ok to use srs as http server. Read [#277](https://github.com/ossrs/srs/issues/277)
|
|
|
|
> Remark: The SRS HTTP server is just a origin HTTP server, for HTTP edge server, please use NGINX, SQUID and ATS.
|
|
|
|
SRS also works well with HTTP reverse proxy servers, like [NGINX](#nginx-proxy) and [Caddy](#caddy-proxy).
|
|
|
|
## Use Scenario
|
|
|
|
The SRS Embeded HTTP server is design to provides basic HTTP service,
|
|
like the camera of mobile phone.
|
|
|
|
SRS should provides HTTP api, which is actually a embeded HTTP server.
|
|
|
|
Actually, RTMP is more complex than HTTP, so HTTP server on st is absolutely ok.
|
|
The HTTP Server in SRS1.0 is an experiment, and I will enhance it in the future.
|
|
|
|
## Config
|
|
|
|
Config the HTTP port and root.
|
|
|
|
```bash
|
|
# embeded http server in srs.
|
|
# the http streaming config, for HLS/HDS/DASH/HTTPProgressive
|
|
# global config for http streaming, user must config the http section for each vhost.
|
|
# the embed http server used to substitute nginx in ./objs/nginx,
|
|
# for example, srs runing in arm, can provides RTMP and HTTP service, only with srs installed.
|
|
# user can access the http server pages, generally:
|
|
# curl http://192.168.1.170:80/srs.html
|
|
# which will show srs version and welcome to srs.
|
|
# @remeark, the http embeded stream need to config the vhost, for instance, the __defaultVhost__
|
|
# need to open the feature http of vhost.
|
|
http_server {
|
|
# whether http streaming service is enabled.
|
|
# default: off
|
|
enabled on;
|
|
# the http streaming port
|
|
# @remark, if use lower port, for instance 80, user must start srs by root.
|
|
# default: 8080
|
|
listen 8080;
|
|
# the default dir for http root.
|
|
# default: ./objs/nginx/html
|
|
dir ./objs/nginx/html;
|
|
}
|
|
```
|
|
|
|
And, each vhost can specifies the dir.
|
|
|
|
```bash
|
|
vhost your_vhost {
|
|
# http static vhost specified config
|
|
http_static {
|
|
# whether enabled the http static service for vhost.
|
|
# default: off
|
|
enabled on;
|
|
# the url to mount to,
|
|
# typical mount to [vhost]/
|
|
# the variables:
|
|
# [vhost] current vhost for http server.
|
|
# @remark the [vhost] is optional, used to mount at specified vhost.
|
|
# @remark the http of __defaultVhost__ will override the http_stream section.
|
|
# for example:
|
|
# mount to [vhost]/
|
|
# access by http://ossrs.net:8080/xxx.html
|
|
# mount to [vhost]/hls
|
|
# access by http://ossrs.net:8080/hls/xxx.html
|
|
# mount to /
|
|
# access by http://ossrs.net:8080/xxx.html
|
|
# or by http://192.168.1.173:8080/xxx.html
|
|
# mount to /hls
|
|
# access by http://ossrs.net:8080/hls/xxx.html
|
|
# or by http://192.168.1.173:8080/hls/xxx.html
|
|
# default: [vhost]/
|
|
mount [vhost]/hls;
|
|
# main dir of vhost,
|
|
# to delivery HTTP stream of this vhost.
|
|
# default: ./objs/nginx/html
|
|
dir ./objs/nginx/html/hls;
|
|
}
|
|
}
|
|
```
|
|
|
|
Remark: The `http_stream` of SRS1 renamed to `http_server` in SRS2, which specifies the global HTTP server config, used to delivery http static files, for dvr files(HLS/FLV/HDS/MPEG-DASH).
|
|
|
|
Remark: The `http` of vhost of SRS1 renamed to `http_static`, similar to global `http_server` for HTTP static files delivery. While the `http_remux` introduced in SRS2 is dynamic remux RTMP to HTTP Live FLV/Mp3/Aac/HLS/Hds/MPEG-DASH stream.
|
|
|
|
## HTTPS Server
|
|
|
|
SRS supports HTTPS, just enable it in the configuration. By default, it uses a sub-signed certificate. If you need
|
|
to use a CA-issued certificate, please replace the relevant files. The related configuration is as follows:
|
|
|
|
```bash
|
|
http_server {
|
|
https {
|
|
# Whether enable HTTPS Streaming.
|
|
# Overwrite by env SRS_HTTP_SERVER_HTTPS_ENABLED
|
|
# default: off
|
|
enabled on;
|
|
# The listen endpoint for HTTPS Streaming.
|
|
# Overwrite by env SRS_HTTP_SERVER_HTTPS_LISTEN
|
|
# default: 8088
|
|
listen 8088;
|
|
# The SSL private key file, generated by:
|
|
# openssl genrsa -out server.key 2048
|
|
# Overwrite by env SRS_HTTP_SERVER_HTTPS_KEY
|
|
# default: ./conf/server.key
|
|
key ./conf/server.key;
|
|
# The SSL public cert file, generated by:
|
|
# openssl req -new -x509 -key server.key -out server.crt -days 3650 -subj "/C=CA/ST=Toronto/L=Toronto/O=Me/OU=Me/CN=ossrs.io"
|
|
# Overwrite by env SRS_HTTP_SERVER_HTTPS_CERT
|
|
# default: ./conf/server.crt
|
|
cert ./conf/server.crt;
|
|
}
|
|
}
|
|
```
|
|
|
|
## Crossdomain
|
|
|
|
SRS has CORS (Cross-Origin Resource Sharing) support enabled by default. The related configuration is as follows:
|
|
|
|
```bash
|
|
http_server {
|
|
# whether enable crossdomain request.
|
|
# for both http static and stream server and apply on all vhosts.
|
|
# Overwrite by env SRS_HTTP_SERVER_CROSSDOMAIN
|
|
# default: on
|
|
crossdomain on;
|
|
}
|
|
```
|
|
|
|
## MIME
|
|
|
|
Only some MIME is supported:
|
|
|
|
| File ext name | Content-Type |
|
|
| ------------- | ----------- |
|
|
| .ts | Content-Type: video/MP2T;charset=utf-8 |
|
|
| .m3u8 | Content-Type: application/x-mpegURL;charset=utf-8 |
|
|
| .json | Content-Type: application/json;charset=utf-8 |
|
|
| .css | Content-Type: text/css;charset=utf-8 |
|
|
| .swf | Content-Type: application/x-shockwave-flash;charset=utf-8 |
|
|
| .js | Content-Type: text/javascript;charset=utf-8 |
|
|
| .xml | Content-Type: text/xml;charset=utf-8 |
|
|
| Others | Content-Type: text/html;charset=utf-8 |
|
|
|
|
## Method
|
|
|
|
Supported HTTP method:
|
|
* GET: Query API, or download file.
|
|
|
|
## Paths
|
|
|
|
HTTP/HTTPS API:
|
|
|
|
* `/api/` SRS HTTP API
|
|
* `/rtc/` SRS WebRTC API
|
|
|
|
HTTP/HTTPS Stream:
|
|
|
|
* `/{app}/{stream}` HTTP Stream mounted by publisher.
|
|
|
|
The bellow is some reverse proxy to work with SRS.
|
|
|
|
> Note: Generally, a proxy can be used to route API and Stream together based on the path.
|
|
|
|
## Nginx Proxy
|
|
|
|
The config for NGINX as file [nginx.conf](https://github.com/ossrs/srs/blob/develop/trunk/conf/nginx.proxy.conf):
|
|
|
|
```
|
|
worker_processes 1;
|
|
events {
|
|
worker_connections 1024;
|
|
}
|
|
|
|
http {
|
|
include /etc/nginx/mime.types;
|
|
|
|
server {
|
|
listen 80;
|
|
listen 443 ssl http2;
|
|
server_name _;
|
|
ssl_certificate /usr/local/srs/conf/server.crt;
|
|
ssl_certificate_key /usr/local/srs/conf/server.key;
|
|
|
|
# For SRS homepage, console and players
|
|
# http://r.ossrs.net/console/
|
|
# http://r.ossrs.net/players/
|
|
location ~ ^/(console|players)/ {
|
|
proxy_pass http://127.0.0.1:8080/$request_uri;
|
|
}
|
|
# For SRS streaming, for example:
|
|
# http://r.ossrs.net/live/livestream.flv
|
|
# http://r.ossrs.net/live/livestream.m3u8
|
|
location ~ ^/.+/.*\.(flv|m3u8|ts|aac|mp3)$ {
|
|
proxy_pass http://127.0.0.1:8080$request_uri;
|
|
}
|
|
# For SRS backend API for console.
|
|
# For SRS WebRTC publish/play API.
|
|
location ~ ^/(api|rtc)/ {
|
|
proxy_pass http://127.0.0.1:1985$request_uri;
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
## Caddy Proxy
|
|
|
|
The config for [CaddyServer](https://caddyserver.com/docs/getting-started) with automatic HTTPS, use the config file `Caddyfile`.
|
|
|
|
For HTTP server, note that to set the default port:
|
|
|
|
```
|
|
:80
|
|
reverse_proxy /* 127.0.0.1:8080
|
|
reverse_proxy /api/* 127.0.0.1:1985
|
|
reverse_proxy /rtc/* 127.0.0.1:1985
|
|
```
|
|
|
|
For HTTPS server, please enable a domain name:
|
|
|
|
```
|
|
example.com {
|
|
reverse_proxy /* 127.0.0.1:8080
|
|
reverse_proxy /api/* 127.0.0.1:1985
|
|
reverse_proxy /rtc/* 127.0.0.1:1985
|
|
}
|
|
```
|
|
|
|
Start the CaddyServer:
|
|
|
|
```
|
|
caddy start -config Caddyfile
|
|
```
|
|
|
|
## Nodejs KOA Proxy
|
|
|
|
The nodejs koa proxy also works well for SRS, please use [koa-proxies](https://www.npmjs.com/package/koa-proxies) based by [node-http-proxy](https://github.com/nodejitsu/node-http-proxy), here is an example:
|
|
|
|
```js
|
|
const Koa = require('koa');
|
|
const proxy = require('koa-proxies');
|
|
const BodyParser = require('koa-bodyparser');
|
|
const Router = require('koa-router');
|
|
|
|
const app = new Koa();
|
|
app.use(proxy('/api/', {target: 'http://127.0.0.1:1985/'}));
|
|
app.use(proxy('/rtc/', {target: 'http://127.0.0.1:1985/'}));
|
|
app.use(proxy('/*/*.(flv|m3u8|ts|aac|mp3)', {target: 'http://127.0.0.1:8080/'}));
|
|
app.use(proxy('/console/', {target: 'http://127.0.0.1:8080/'}));
|
|
app.use(proxy('/players/', {target: 'http://127.0.0.1:8080/'}));
|
|
|
|
// Start body-parser after proxies, see https://github.com/vagusX/koa-proxies/issues/55
|
|
app.use(BodyParser());
|
|
|
|
// APIs that depends on body-parser
|
|
const router = new Router();
|
|
router.all('/', async (ctx) => {
|
|
ctx.body = 'Hello World';
|
|
});
|
|
app.use(router.routes());
|
|
|
|
app.listen(3000, () => {
|
|
console.log(`Server start on http://localhost:3000`);
|
|
});
|
|
```
|
|
|
|
Save it as `index.js`, then run:
|
|
|
|
```
|
|
npm init -y
|
|
npm install koa koa-proxies koa-proxies koa-bodyparser koa-router
|
|
node .
|
|
```
|
|
|
|
## HTTPX Proxy
|
|
|
|
Well [httpx-static](https://github.com/ossrs/go-oryx/tree/develop/httpx-static#usage) is a simple HTTP/HTTPS proxy written by Go:
|
|
|
|
```
|
|
go get github.com/ossrs/go-oryx/httpx-static
|
|
cd $GOPATH/bin
|
|
./httpx-static -http=80 -https=443 \
|
|
-skey /usr/local/srs/etc/server.key -scert /usr/local/srs/etc/server.crt \
|
|
-proxy=http://127.0.0.1:1985/api/v1/ \
|
|
-proxy=http://127.0.0.1:1985/rtc/v1/ \
|
|
-proxy=http://127.0.0.1:8080/
|
|
```
|
|
|
|
> Please make sure the path `/` is the last one.
|
|
|
|
Winlin 2015.1
|
|
|
|

|
|
|
|
|