搭建自定义RTMP流服务器时1935端口连接失败求助
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, butnetstat/lsofshow 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
- Stop the apt-installed Nginx service:
sudo systemctl stop nginx - Start your source-compiled Nginx:
sudo /usr/local/nginx/sbin/nginx - 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
Step 4: Set Up Systemd for Your Custom Nginx (Optional but Recommended)
To ensure your RTMP-enabled Nginx starts automatically on boot, create a systemd service file:
- Create the service file:
sudo nano /etc/systemd/system/nginx-rtmp.service - 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 - 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

