I am trying to implement simple HLS streaming service that is running of Docker containers. In short, each service is dockerized Apache streaming node. Everything works fine. Except there is a problem is that every container runs on its own port and I have to address it like:
http://localhost:Port/service_name/*m3u8.
What I need to do is setup a proxy to do this port forwarding, so that there will be no need to use port number in URL.
Searching this topic, I got an impression that NGINX can do this job. However it seems that Proxy_Pass is failing to route request to container. In NGINX error log I get (111: Connection refused) error. Or 404. I tried tweaking it different ways without success. I am not expert in NGINX, so I am not sure if I am doing the right thing. Could someone at least point me in the right direction?
Here is my workflow:
I have built an image for Apache service and image for NGINX proxy. Then using Docker compose I create 3 containers (1 NGINX proxy an 2 Apache streams), all in one default network.
When I address the service without port number I get 505 error.
This is my Dockerfile for NGINX server:
FROM ubuntu:14.04
RUN sudo apt-get update && sudo apt-get install -y nginx && sudo apt-get install nano && rm -rf /var/lib/apt/lists/*
EXPOSE 80
This is Dockerfile for Apache service:
FROM ubuntu:14.04
RUN apt-get update && apt-get install -y apt-transport-https && apt-get install -y apache2 && \
apt-get install -y software-properties-common && add-apt-repository ppa:mc3man/trusty-media -y && \
apt-get update && apt-get install -y ffmpeg && mkdir /var/www/html/hls && chmod 777 -R /var/www/html/hls && \
rm -rf /var/lib/apt/lists/*
COPY apache2.conf /etc/apache2/apache2.conf
COPY mime.conf /etc/apache2/mods-available/mime.conf
RUN /etc/init.d/apache2 start
This is docker-compose.yml file:
version: '2'
services:
worker:
image: nginx
stdin_open: true
tty: true
ports:
- "80:80"
stream1:
image: server
ports:
- "8003:80"
command: >
sh -c "sudo /etc/init.d/apache2 start && cd /var/www/html/hls && ffmpeg -i http://b46785a2.iptvzone.me/iptv/GWXXRGQF9G38LU/828/index.m3u8 -c copy -hls_list_size 10 -hls_flags delete_segments live.m3u8 "
restart: always
stream2:
image: server
ports:
- "8004:80"
command: >
sh -c "sudo mkdir /var/www/html/stream && chmod 777 -R /var/www/html/stream && cd /var/www/html/stream && sudo /etc/init.d/apache2 start \
&& ffmpeg -i http://app.live.112.events/hls-ua/112hd_mid/index.m3u8 -c copy -hls_list_size 10 -hls_flags delete_segments live.m3u8 "
restart: always
This is NGINX config file in: /etc/nginx/sites-available/default
server {
listen 80;
location /hls/ {
proxy_pass http://172.20.0.2:8003/hls/; - that is IP of Docker continer. localhost produces 404 error
}
}
And this is output from /var/log/nginx/error.log:
2019/04/29 16:16:43 [error] 116#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.20.0.1, server: , request: "GET /hls/live.m3u8 HTTP/1.1", upstream: "http://127.0.0.1:8003/hls/live.m3u8", host: "localhost"
2019/04/29 16:16:46 [error] 116#0: *1 no live upstreams while connecting to upstream, client: 172.20.0.1, server: , request: "GET /hls/live.m3u8 HTTP/1.1", upstream: "http://localhost/hls/live.m3u8", host: "localhost"
Thank you for any tips!
Update2: Thank you for advice.
The following code worked:
server {
listen 80;
location /hls/ {
allow all;
proxy_pass http://muze.cf:8003/hls/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
}
}
I can open URL http://muze.cf/hls/ without port number. However when I add another server block like this:
server {
listen 80;
location /stream/ {
allow all;
proxy_pass http://muze.cf:8004/stream/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
}
}
I get conflicting server name error:
2019/05/13 09:43:13 [warn] 42#0: conflicting server name "" on 0.0.0.0:80, ignored
2019/05/13 09:43:19 [error] 47#0: *1 open() "/usr/share/nginx/html/stream/live.m3u8" failed (2: No such file or directory), client: 83.134.167.52, server: , request: "GET /stream/live.m3u8 HTTP/1.1", host: "muze.cf"
2019/05/13 09:43:51 [error] 47#0: *1 open() "/usr/share/nginx/html/stream/live.m3u8" failed (2: No such file or directory), client: 83.134.167.52, server: , request: "GET /stream/live.m3u8 HTTP/1.1", host: "muze.cf"
2019/05/13 09:46:00 [warn] 66#0: conflicting server name "" on 0.0.0.0:80, ignored
2019/05/13 09:46:06 [error] 72#0: *3 open() "/usr/share/nginx/html/stream/live.m3u8" failed (2: No such file or directory), client: 83.134.167.52, server: , request: "GET /stream/live.m3u8 HTTP/1.1", host: "muze.cf"
2019/05/13 09:46:32 [error] 72#0: *3 open() "/usr/share/nginx/html/stream/live.m3u8" failed (2: No such file or directory), client: 83.134.167.52, server: , request: "GET /stream/live.m3u8 HTTP/1.1", host: "muze.cf"
2019/05/13 09:52:13 [warn] 94#0: server name "http://muze.cf/hls/" has suspicious symbols in /etc/nginx/sites-enabled/default:3
2019/05/13 09:52:13 [warn] 94#0: server name "http://muze.cf/stream/" has suspicious symbols in /etc/nginx/sites-enabled/default:16
2019/05/13 09:52:49 [error] 100#0: *6 open() "/usr/share/nginx/html/stream/live.m3u8" failed (2: No such file or directory), client: 83.134.167.52, server: http://muze.cf/hls/, request: "GET /stream/live.m3u8 HTTP/1.1", host: "muze.cf"
Theoretically I should be able to make a lot of server blocks in the same domain ?