You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

搭建自定义RTMP流服务器时1935端口连接失败求助

Fixing "Connection Refused" for Nginx RTMP Server

Hey there, let's break down why your RTMP server isn't listening on port 1935 and resolve that frustrating connection error. Based on the details you shared, here's what's likely going wrong and how to fix it:

Key Observations from Your Setup

  • You compiled Nginx from source with the nginx-rtmp-module, but netstat/lsof show port 1935 isn't listening.
  • You used sudo systemctl start nginx, but source-compiled Nginx doesn't come with a default systemd service—so you're probably starting the apt-installed version of Nginx (which doesn't have the RTMP module) instead of your custom-compiled one.

Step 1: Verify Which Nginx You're Running

First, confirm which Nginx binary is being executed:

which nginx
  • If the output is /usr/sbin/nginx, that's the apt-installed version (no RTMP support).
  • Your custom-compiled Nginx is located at /usr/local/nginx/sbin/nginx.

Step 2: Stop the Wrong Nginx and Start Your Custom One

  1. Stop the apt-installed Nginx service:
    sudo systemctl stop nginx
    
  2. Start your source-compiled Nginx:
    sudo /usr/local/nginx/sbin/nginx
    
  3. Check if port 1935 is now listening:
    sudo lsof -i:1935
    # OR
    netstat -ltn | grep 1935
    

You should see output showing Nginx is actively listening on port 1935 now.


Step 3: Ensure Your Nginx Config is Correct

Double-check that your rtmp block is placed at the top level of your config (not nested inside the http block). Here's the correct structure for /usr/local/nginx/conf/nginx.conf:

user www-data;
worker_processes auto;
error_log logs/error.log;
pid logs/nginx.pid;

# RTMP block sits at the same level as the http block
rtmp {
    server {
        listen 1935;
        application live {
            live on;
            interleave on;
            hls on;
            hls_path /tmp/hls;
            hls_fragment 15s;
        }
    }
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }

        # Add this block to serve HLS streams if needed
        location /hls {
            types {
                application/vnd.apple.mpegurl m3u8;
                video/mp2t ts;
            }
            root /tmp;
            add_header Cache-Control no-cache;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

Validate the config syntax with:

sudo /usr/local/nginx/sbin/nginx -t

If it passes, reload Nginx to apply changes:

sudo /usr/local/nginx/sbin/nginx -s reload

To ensure your RTMP-enabled Nginx starts automatically on boot, create a systemd service file:

  1. Create the service file:
    sudo nano /etc/systemd/system/nginx-rtmp.service
    
  2. Paste this content:
    [Unit]
    Description=NGINX RTMP Media Server
    After=network.target
    
    [Service]
    Type=forking
    ExecStart=/usr/local/nginx/sbin/nginx
    ExecReload=/usr/local/nginx/sbin/nginx -s reload
    ExecStop=/usr/local/nginx/sbin/nginx -s stop
    PrivateTmp=true
    
    [Install]
    WantedBy=multi-user.target
    
  3. Enable and start the service:
    sudo systemctl daemon-reload
    sudo systemctl enable nginx-rtmp.service
    sudo systemctl start nginx-rtmp.service
    

Step 5: Fix the FFmpeg Push Command

Note that your original FFmpeg command uses 640*480 for resolution—this should be 640x480 (use x instead of *). Here's the corrected command for testing:

input="/path/to/your/local/video.mp4"
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 640x480 -f flv $output

If this works, try pushing your IP camera's RTSP stream again—it should connect successfully now.


内容的提问来源于stack exchange,提问作者showkey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:26:30