Warm tip: This article is reproduced from serverfault.com, please click

Cannot open connection tcp://localhost:1935 when to set up custom RTMP stream server

发布于 2020-11-29 15:57:30

My simple network is as following:

192.168.31.52 is my local pc 
192.168.31.251 is an ip camera.

I can open the stream rtsp://192.168.31.251/cam/realmonitor?channel=1&subtype=0 with SMPlayer.
Build my nginx for customizing RTMP stream this way.

sudo apt update
sudo apt install build-essential git
sudo apt install libpcre3-dev libssl-dev zlib1g-dev 
git clone https://github.com/arut/nginx-rtmp-module.git
git clone https://github.com/nginx/nginx.git
cd nginx
./auto/configure --add-module=../nginx-rtmp-module
make
sudo make install

Set config file for nginx:

sudo vim /usr/local/nginx/conf/nginx.conf
rtmp { 
    server { 
        listen 1935; 
        application live { 
            live on; 
            interleave on;
 
            hls on; 
            hls_path /tmp/hls; 
            hls_fragment 15s; 
        } 
    } 
} 

Then set permission for nginx:

mkdir /tmp/hls
sudo chmod -R 755  /tmp/hls
sudo chown -R www-data:www-data  /tmp/hls

Edit index.html in /tmp/hls.

<p>test for nginx</p>

Both 127.0.0.1/index.html and 192.168.31.52/index.html can open the /tmp/hls/index.html.

Now open port 1935 on my network:

sudo firewall-cmd --zone=public --add-port=1935/tcp --permanent
sudo firewall-cmd --reload 
sudo firewall-cmd --list-ports | grep 1935
1935/tcp

Start nginx:

sudo systemctl start nginx

Up stream the rtsp stream from ip camera--192.168.31.251 to local pc --192.168.31.52.

input="rtsp://192.168.31.251/cam/realmonitor?channel=1&subtype=0"
output="rtmp://192.168.31.52:1935/live/sample"
ffmpeg -i $input -acodec aac -strict experimental -ar 44100 -ac 2 -b:a 96k -r 25 -b:v 500k -s 640*480 -f flv $output

It encounter the following errors:

[tcp @ 0x59fb700] Connection to tcp://192.168.31.52:1935 failed: Connection refused
[rtmp @ 0x59fc5c0] Cannot open connection tcp://192.168.31.52:1935
rtmp://192.168.31.52:1935/live/sample: Connection refused

To keep the issue simple,i replace $input with a mp4 file in local pc,same error info.
How can fix it?
Ping my machine:

ping 192.168.31.52
PING 192.168.31.52 (192.168.31.52): 56 data bytes
64 bytes from 192.168.31.52: icmp_seq=0 ttl=64 time=0.108 ms
64 bytes from 192.168.31.52: icmp_seq=1 ttl=64 time=0.107 ms
64 bytes from 192.168.31.52: icmp_seq=2 ttl=64 time=0.111 ms

Why the port 1935 not opened,i had restarted nginx after setting?

sudo lsof -i:1935
#nothing in the output
netstat -ltn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:51413           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:1080          0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:8123          0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:8384          0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:9091            0.0.0.0:*               LISTEN     
tcp6       0      0 :::3306                 :::*                    LISTEN     
tcp6       0      0 :::80                   :::*                    LISTEN     
tcp6       0      0 :::22000                :::*                    LISTEN     
tcp6       0      0 :::51413                :::*                    LISTEN     
tcp6       0      0 :::21                   :::*                    LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
tcp6       0      0 :::2681                 :::*                    LISTEN  

Firewall command can't work:

sudo firewall-cmd --zone=public --add-port=1935/tcp --permanent
sudo firewall-cmd --reload 

My nginx version:

sudo nginx -v
nginx version: nginx/1.10.3
Questioner
showkey
Viewed
11
aergistal 2021-01-08 19:54:33

If you haven't configured nginx as a system service yourself then you're most likely running a pre-installed version and not the version you compiled which includes the RTMP module.

sudo nginx -v
nginx version: nginx/1.10.3

This is a stable version from 2017.

Using the latest version https://github.com/nginx/nginx.git:

./objs/nginx -v
nginx version: nginx/1.19.6

The version from 29/11/2020 when you first asked the question should be mainline 1.19.5.

Check the definition of the system service and the nginx executable paths.