I'm trying to broadcast a stream from OBS (codec is set x264) to nginx with an rtmp server and then view the stream as mpeg-dash in VLC.
I've set up nginx with the rtmp module and that works. I can stream to nginx and receive the stream via rtmp in VLC. For that I used this URL: rtmp://127.0.0.1/live/stream
This is my config.
#user nobody;
worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
rtmp {
server {
listen 1935;
ping 30s;
notify_method get;
application live {
live on;
dash on;
dash_path /tmp/dash;
}
}
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile off;
tcp_nopush on;
aio off;
directio 512;
#keepalive_timeout 0;
keepalive_timeout 65;
server {
listen 8080;
location /dash {
# Serve DASH fragments
types {
application/dash+xml mpd;
video/mp4 mp4;
}
root /tmp;
add_header Cache-Control no-cache;
# CORS setup
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length';
# Allow CORS preflight requests
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
}
}
}
I can see the stream.mpd in the file explorer, but VLC always says can't open source. I've tried both URLs: http://127.0.0.1/tmp/dash/stream.mpd and http://127.0.0.1/dash/stream.mpd, but both didn't work.
I also tried it with HLS, but I couldn't get this to work either.
To avoid having troubles with file privileges, I set the whole folder to chmod 777.
Any ideas what could be wrong or what I could try? Thank you
I solved it, sort of...
I used the config from here: HTML5 live streaming
I had to change some things in the file (http-tag was missing as an example) but then it worked with HLS. Turns out it doesn't really make a difference if I'm using MPEG-DASH or HLS, so I'm fine with it.
Another thing I found out, is that I always tried it without defining the port. For RTMP this made no problems since I could use the standard port. But on http I had to use port 8080 since 80 was already in use. But in VLC I didn't think to define the port.
Now it works for me;)
Related
We are having kubernetes pod with running two containers (Nginx container and Application container) inside it. We have defined nginx configuration as a config map along with deployment.yaml file and deploying application in each sprint with latest code.
But while testing it, we are getting intermittent issue and before browser cookies clearance getting 502 Service Temporarily Unavailable & after browser cookies clearance 503 BadGateway Nginx errors respectively.
we have defined large_client_header_buffers 4 16k; from default values in nginx.conf to avoid any issue with cookies. Still it is not resolved and still getting 502 Service Temporarily Unavailable & after browser cookies clearance 503 BadGateway Nginx errors. I'll highly appreciate if anybody can suggest a solution to overcome over it.
Here is the file I am using for deployment:
apiVersion: v1
kind: ConfigMap
metadata:
name: {{APP_NAME}}-nginx
namespace: {{NAME_SPACE}}
data:
nginx.conf: |
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
large_client_header_buffers 4 16k;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on;
include /etc/nginx/conf.d/*.conf;
}
default.conf: |
map $http_user_agent $ignore_ua {
default 1;
"ELB-HealthChecker/2.0" 0;
"kube-probe/1.21+" 0;
}
server {
listen 3001 ssl;
ssl_certificate /etc/ssl/certs/ssc/ssc.crt;
ssl_certificate_key /etc/ssl/certs/ssc/ssc.key;
server_name localhost;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
location ~* ^/(api/.+) {
add_header X-Robots-Tag "noindex, nofollow, nosnippet, noarchive";
proxy_pass http://localhost:5000/$1$is_args$args;
access_log /var/log/nginx/access.log combined if=$ignore_ua;
}
location / {
proxy_http_version 1.1;
proxy_hide_header Cache-Control;
proxy_hide_header Expires;
proxy_ignore_headers Cache-Control Expires;
proxy_intercept_errors on;
add_header Cache-Control "private, no-cache, no-store, max-age=0, must-revalidate";
proxy_pass http://localhost:5000/;
}
}
I explained the problem in this Video Stack Exchange Question as I don't believe this is something code related (e.g. a bug).
It may be a useful information for dev's however, that I did not compile nginx from source. I mention that explicitly, because searching the web, it seems like building from source is the recommended way for nginx/rtmp setups. I could not find a bug report, however, that would explain this behavior.
I feel like I am overseeing something stupid.
For the sake of completeness, I will post the configuration files. Also, dont miss out the live-demo and have fun playing around with the streaming server linked in the other Post ;)
Happy Coding!
nginx.conf:
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Gzip Settings
##
gzip on;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
rtmp {
include /etc/nginx/streams-enabled/*;
}
rtmp server (in streams-enabled/)
server {
listen 1935;
chunk_size 4096;
application live {
live on;
hls on;
hls_path /home/streamer/hls;
hls_nested on;
hls_fragment 3s;
hls_playlist_length 30s;
hls_base_url http://0.0.0.0:3456/hls;
on_publish http://0.0.0.0:3456/auth;
# Recent versions of IE need this for normal playback.
wait_video on; # start audio with video
# force hls
deny play all;
}
}
http-server (in sites-enabled/)
server {
listen 3456;
location /hls {
add_header Cache-Control no-cache;
# CORS setup
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length';
# allow CORS preflight requests
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
types {
application/dash+xml mpd;
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /home/streamer/;
}
}
The problem is with the stream configuration.
vlc -vv http://104.248.36.47:3644/hls/debug/index.m3u8 ouput revealed:
[00007f0594002580] main stream debug: connecting to 0.0.0.0 port 3644 ...
[00007f0594002580] main stream error: connection error: Connection refused
...
[00007f0594002580] adaptive stream debug: Retrieving http://0.0.0.0:3644/hlsdebug/75.ts #0
Note that it says hlsdebug instead of the expected hls/debug
After adding a '/' to the hls_base_url, and changing 0.0.0.0 to the public IP, it works.
I do not understand fully tho why 0.0.0.0 would not work..
I am trying to build a live stream platform using nginx and nginx-http-flv-module (with nginx-rtmp-module).
I have been using nginx-http-flv-module's guide.
I built an nginx server with rtmp and http-flv support.
My nginx.conf file:
#user nobody;
worker_processes 1;
error_log logs/error.log debug;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name localhost;
...
location /live {
flv_live on; #open flv live streaming (subscribe)
chunked_transfer_encoding on; #open 'Transfer-Encoding: chunked' response
add_header 'Access-Control-Allow-Origin' '*'; #add additional HTTP header
add_header 'Access-Control-Allow-Credentials' 'true'; #add additional HTTP header
add_header 'Access-Control-Expose-Headers' 'Content-Length';
}
}
}
rtmp {
server {
listen 1935;
ping 30s;
notify_method get;
application myapp {
live on;
}
}
}
I start publishing my stream using OBS and play the stream in the browser using flv.js like this:
<video id="videoElement" controls autoplay></video>
...
<script>
let videoElement = document.getElementById('videoElement');
let flvPlayer = flvjs.createPlayer({
type: 'flv',
isLive: "true",
url: 'http://192.168.1.122:8080/live?port=1935&app=myapp&stream=test'
});
flvPlayer.attachMediaElement(videoElement);
flvPlayer.load();
</script>
And everything works great! The stream is playing in the browser as expected. But the problem is whenever a second viewer starts watching the stream (if I open it in another browser tab f.e). The player stops playing and starts infinite loading. So what can cause this problem?
I am the owner of nginx-http-flv-module, I am sorry that the bug was caused by the commit on July 7, 2019 and it has been fixed already.
You can try the latest code.
So I have an application running on http://localhost:3000. I want to expose it to other people using nginx. I redirect http connection to https using the following configuration:
server {
listen 80 default;
server_name www.example.com;
return 301 https://$server_name$request_uri;
}
The issue is when I try to access www.example.com or http://www.example.com, it redirects me to https://localhost/ instead of https://www.example.com.
https://www.example.com redirects as intended.
For your information, www.example.com is an internal domain.
All my configuration :
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
# Disable sending the server identification
server_tokens off;
# Prevent displaying Botpress in an iframe (clickjacking protection)
add_header X-Frame-Options SAMEORIGIN;
# Prevent browsers from detecting the mimetype if not sent by the server.
add_header X-Content-Type-Options nosniff;
# Force enable the XSS filter for the website, in case it was disabled manually
add_header X-XSS-Protection "1; mode=block";
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
# Load modular configuration files from the /etc/nginx/conf.d directory.
include /etc/nginx/conf.d/*.conf;
# Redirect unsecure requests to the HTTPS endpoint
server {
listen 80 default;
server_name www.example.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 http2 ssl;
server_name www.example.com;
ssl_certificate cert.pem;
ssl_certificate_key cert.key;
# Force the use of secure protocols only
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
# Enable session cache for added performances
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_session_tickets off;
# Added security with HSTS
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";
# We need to add specific headers so the websockets can be set up through the reverse proxy
location /socket.io/ {
proxy_pass http://localhost:3000/socket.io/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
}
# All other requests should be directed to the server
location / {
proxy_pass http://localhost:3000;
}
}
}
Thanks in advance for any help,
As #johnsing stated in the comment section, removing default and clearing the cache did the job.
I set up Nginx for live stream edge server which pull RTMP from origin server. For my case RTMP is work fine but hls return not found.
I pull rtmp like dynamic stream based on origin for example "pull rtmp://ORIGIN_IP:1935/appname" and I can play "rtmp://EDGE_IP:1935/stream/origin_streamname" but I can't play "http://EDGE_IP:8080/stream/origin_streamname.m3u8" always return "not found" port 8080 opened.
below is nginx.conf
worker_processes auto;
error_log /var/log/nginx/error.log warn;
events {
worker_connections 1024;
}
# RTMP configuration
rtmp {
server {
listen 1935;
chunk_size 4000;
application show {
live on;
hls on;
hls_path /mnt/hls/;
hls_fragment 3;
hls_playlist_length 60;
deny play all;
}
application vod {
play /mnt/mp4s;
}
application stream {
live on;
pull rtmp://ORIGIN_IP:1935/appname;
}
}
}
http {
sendfile off;
tcp_nopush on;
aio on;
directio 512;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
server {
listen 8080;
server_name 127.0.0.1;
location /nginx_status {
stub_status on;
allow 127.0.0.1;
deny all;
}
location / {
add_header 'Cache-Control' 'no-cache';
add_header 'Access-Control-Allow-Origin' '*' always;
add_header 'Access-Control-Expose-Headers' 'Content-Length';
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
types {
application/dash+xml mpd;
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root /mnt/;
}
}
}
I solved it by adding
hls on;
hls_path /mnt/hls/;
hls_fragment 3;
hls_playlist_length 60;
then you need to play rtmp://EDGE_IP:1935/stream/origin_streamname and http://EDGE_IP:8080/hls/origin_streamname.m3u8 at the same time.