{"id":947,"date":"2025-11-13T11:35:59","date_gmt":"2025-11-13T03:35:59","guid":{"rendered":"https:\/\/www.inhhh.com\/blog\/?p=947"},"modified":"2025-11-13T11:35:59","modified_gmt":"2025-11-13T03:35:59","slug":"%e5%90%8c%e6%97%b6%e5%9c%a8-nginx-%e4%b8%ad%e5%90%af%e7%94%a8-nginx-http-flv-module-%e5%92%8c-nginx-rtmp-module","status":"publish","type":"post","link":"http:\/\/www.inhhh.com\/blog\/?p=947","title":{"rendered":"\u540c\u65f6\u5728 Nginx \u4e2d\u542f\u7528 nginx-http-flv-module \u548c nginx-rtmp-module"},"content":{"rendered":"\n<p>\u540c\u65f6\u5728 <strong>Nginx<\/strong> \u4e2d\u542f\u7528 <strong><code>nginx-http-flv-module<\/code><\/strong> \u548c <strong><code>nginx-rtmp-module<\/code><\/strong>\uff0c\u8fd9\u662f\u5b8c\u5168\u53ef\u4ee5\u5b9e\u73b0\u7684\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 <strong>Ubuntu<\/strong> \u6216 <strong>Debian<\/strong> \u4e0a\u91cd\u65b0\u7f16\u8bd1\u5e76\u914d\u7f6e Nginx\uff0c\u652f\u6301\u8fd9\u4e24\u4e2a\u6a21\u5757\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">?\ufe0f \u6b65\u9aa4 1\uff1a\u51c6\u5907\u7f16\u8bd1\u73af\u5883<\/h3>\n\n\n\n<p>\u9996\u5148\uff0c\u786e\u4fdd\u4f60\u5df2\u7ecf\u5b89\u88c5\u4e86\u7f16\u8bd1\u6240\u9700\u7684\u4f9d\u8d56\u5305\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sudo apt update<br>sudo apt install -y build-essential libpcre3 libpcre3-dev libssl-dev zlib1g-dev git<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">?\ufe0f \u6b65\u9aa4 2\uff1a\u4e0b\u8f7d\u5e76\u89e3\u538b Nginx \u548c\u6240\u9700\u7684\u6a21\u5757<\/h3>\n\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u4e0b\u8f7d Nginx \u7684\u6e90\u4ee3\u7801\u4ee5\u53ca\u9700\u8981\u7684\u4e24\u4e2a\u6a21\u5757\uff08<code>nginx-http-flv-module<\/code> \u548c <code>nginx-rtmp-module<\/code>\uff09\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cd \/usr\/local\/src<br><br># \u4e0b\u8f7d Nginx \u6700\u65b0\u7248\u6e90\u4ee3\u7801<br>wget http:\/\/nginx.org\/download\/nginx-1.24.0.tar.gz<br>tar -xzvf nginx-1.24.0.tar.gz<br><br># \u4e0b\u8f7d nginx-rtmp-module<br>git clone https:\/\/github.com\/arut\/nginx-rtmp-module.git<br><br># \u4e0b\u8f7d nginx-http-flv-module<br>git clone https:\/\/github.com\/winshining\/nginx-http-flv-module.git<br><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">?\ufe0f \u6b65\u9aa4 3\uff1a\u7f16\u8bd1\u5e76\u5b89\u88c5 Nginx<\/h3>\n\n\n\n<p>\u6ce8\u610f\uff1a<\/p>\n\n\n\n<p>\u8fd9\u91cc\u4fee\u6539<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Include RTMP headers\n#include &lt;ngx_rtmp.h>\n\/\/ Define missing NALU constants (if not already defined)\n#define NGX_RTMP_NALU_SLICE 1  \/\/ NALU type for Slice\n#define NGX_RTMP_NALU_IDR 5    \/\/ NALU type for IDR frame\n#define NGX_RTMP_NALU_SEI 6\n#define NGX_RTMP_NALU_SPS 7    \/\/ NALU type for Sequence Parameter Set\n#define NGX_RTMP_NALU_PPS 8    \/\/ NALU type for Picture Parameter Set\n#define NGX_RTMP_NALU_AUD 9 \n\n\/\/ Define missing constants if not already defined\n#define NGX_RTMP_FRAME_IDR 5    \/\/ IDR frame type (commonly 5 in H.264)\n#define NGX_RTMP_FRAME_I 1      \/\/ I-frame type\n#define NGX_RTMP_FRAME_P 2      \/\/ P-frame type\n#define NGX_RTMP_FRAME_B 3      \/\/ B-frame type\n\n\/\/ \u6ce8\u91ca\u6389\u95ee\u9898\u884c\n\/\/ len = ngx_rtmp_n3_to_h4((u_char *) &amp;rlen);\n\/\/ \u6216\u8005\u66ff\u6362\u4e3a\nlen = sizeof(rlen);<\/code><\/pre>\n\n\n\n<p>\u5728 Nginx \u6e90\u7801\u76ee\u5f55\u4e0b\uff0c\u6267\u884c\u4ee5\u4e0b\u547d\u4ee4\u6765\u7f16\u8bd1 Nginx\uff0c\u5e76\u786e\u4fdd\u540c\u65f6\u52a0\u8f7d\u8fd9\u4e24\u4e2a\u6a21\u5757\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bash\u590d\u5236<code>cd nginx-1.24.0\n\n# \u914d\u7f6e Nginx\uff0c\u6dfb\u52a0\u4e24\u4e2a\u6a21\u5757\uff08nginx-rtmp-module \u548c nginx-http-flv-module\uff09\n.\/configure --add-module=..\/nginx-rtmp-module --add-module=..\/nginx-http-flv-module --with-http_ssl_module\n\n# \u7f16\u8bd1\u5e76\u5b89\u88c5 Nginx\nmake -j$(nproc)\nsudo make install\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u4e00\u6b65\u4f1a\u7f16\u8bd1 Nginx\uff0c\u786e\u4fdd\u5b83\u52a0\u8f7d\u4e86\u8fd9\u4e24\u4e2a\u6a21\u5757\uff1a<code>nginx-rtmp-module<\/code> \u548c <code>nginx-http-flv-module<\/code>\uff0c\u5e76\u5b89\u88c5\u5230 <code>\/usr\/local\/nginx\/<\/code>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">?\ufe0f \u6b65\u9aa4 4\uff1a\u9a8c\u8bc1\u5b89\u88c5<\/h3>\n\n\n\n<p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u547d\u4ee4\u68c0\u67e5 Nginx \u662f\u5426\u6210\u529f\u52a0\u8f7d\u4e86\u8fd9\u4e24\u4e2a\u6a21\u5757\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code># \u68c0\u67e5 Nginx \u914d\u7f6e\u53c2\u6570<br>sudo \/usr\/local\/nginx\/sbin\/nginx -V<br><\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u4e2d\u5e94\u8be5\u5305\u542b\u7c7b\u4f3c\u4e0b\u9762\u7684\u4fe1\u606f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bash\u590d\u5236<code>--add-module=..\/nginx-rtmp-module\n--add-module=..\/nginx-http-flv-module\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u6709\u663e\u793a\u8fd9\u4e24\u4e2a\u6a21\u5757\uff0c\u8bf4\u660e Nginx \u7f16\u8bd1\u6210\u529f\u5e76\u52a0\u8f7d\u4e86\u8fd9\u4e24\u4e2a\u6a21\u5757\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">?\ufe0f \u6b65\u9aa4 5\uff1a\u914d\u7f6e Nginx<\/h3>\n\n\n\n<p>\u73b0\u5728\uff0c\u4f60\u53ef\u4ee5\u914d\u7f6e <code>nginx.conf<\/code> \u6765\u652f\u6301 <strong>RTMP<\/strong> \u548c <strong>HTTP-FLV<\/strong>\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u914d\u7f6e\u793a\u4f8b\uff0c<code>nginx.conf<\/code> \u4e2d\u540c\u65f6\u914d\u7f6e\u4e86 RTMP \u548c HTTP-FLV\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">nginx\u590d\u5236<code>worker_processes auto;\nevents {\n    worker_connections 1024;\n}\n\nhttp {\n    include mime.types;\n    default_type application\/octet-stream;\n    sendfile on;\n    keepalive_timeout 65;\n\n    server {\n        listen 8080;  # HTTP-FLV \u7684\u7aef\u53e3\n        server_name localhost;\n\n        # \u4e3b\u9875\uff0c\u6d4b\u8bd5\u9875\u9762\n        location \/ {\n            root html;\n            index index.html;\n        }\n\n        # HTTP-FLV \u5b9e\u65f6\u6d41\n        location \/live {\n            flv_live on;                   # \u5f00\u542f FLV \u76f4\u64ad\u6d41\n            chunked_transfer_encoding on;\n            add_header 'Access-Control-Allow-Origin' '*';\n            add_header 'Access-Control-Allow-Credentials' 'true';\n            add_header Cache-Control no-cache;\n        }\n\n        # HLS \u652f\u6301\uff08\u53ef\u9009\uff09\n        location \/hls {\n            types {\n                application\/vnd.apple.mpegurl m3u8;\n                video\/mp2t ts;\n            }\n            root \/tmp;   # \u5b58\u653e HLS \u5207\u7247\u7684\u8def\u5f84\n            add_header Cache-Control no-cache;\n            add_header 'Access-Control-Allow-Origin' '*';\n            add_header 'Access-Control-Allow-Credentials' 'true';\n        }\n    }\n}\n\nrtmp {\n    server {\n        listen 1935;  # RTMP \u63a8\u6d41\u7aef\u53e3\n        chunk_size 4096;\n\n        application live {\n            live on;             # \u5f00\u542f RTMP \u76f4\u64ad\u6d41\n            record off;          # \u4e0d\u8bb0\u5f55\u6d41\n            hls on;              # \u5f00\u542f HLS \u6d41\u652f\u6301\uff08\u53ef\u9009\uff09\n            hls_path \/tmp\/hls;   # \u5b58\u50a8 HLS \u6587\u4ef6\u7684\u8def\u5f84\n            hls_fragment 2s;     # HLS \u7247\u6bb5\u65f6\u957f\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">?\ufe0f \u6b65\u9aa4 6\uff1a\u542f\u52a8 Nginx<\/h3>\n\n\n\n<p>\u914d\u7f6e\u597d\u540e\uff0c\u4f60\u53ef\u4ee5\u542f\u52a8 Nginx\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bash\u590d\u5236<code># \u6d4b\u8bd5\u914d\u7f6e\u6587\u4ef6\u662f\u5426\u6b63\u786e\nsudo \/usr\/local\/nginx\/sbin\/nginx -t\n\n# \u91cd\u65b0\u52a0\u8f7d Nginx \u914d\u7f6e\nsudo \/usr\/local\/nginx\/sbin\/nginx -s reload\n\n# \u542f\u52a8 Nginx \u670d\u52a1\nsudo \/usr\/local\/nginx\/sbin\/nginx\n<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u914d\u7f6e\u6587\u4ef6\u6b63\u786e\uff0cNginx \u5c06\u4f1a\u91cd\u65b0\u52a0\u8f7d\u5e76\u5f00\u59cb\u670d\u52a1\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">?\ufe0f \u6b65\u9aa4 7\uff1a\u63a8\u6d41\u4e0e\u64ad\u653e<\/h3>\n\n\n\n<p>\u73b0\u5728\uff0cNginx \u5df2\u7ecf\u914d\u7f6e\u597d\u4e86 <strong>RTMP<\/strong> \u548c <strong>HTTP-FLV<\/strong>\u3002\u4f60\u53ef\u4ee5\u5f00\u59cb\u63a8\u6d41\u5e76\u5728\u6d4f\u89c8\u5668\u4e2d\u64ad\u653e\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">1. \u63a8\u6d41\uff08\u4f7f\u7528 <code>ffmpeg<\/code>\uff09<\/h4>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>ffmpeg<\/code> \u6765\u63a8\u9001\u89c6\u9891\u6d41\u5230 Nginx \u7684 RTMP \u670d\u52a1\u5668\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bash\u590d\u5236<code>ffmpeg -f v4l2 -framerate 30 -video_size 640x480 -i \/dev\/video0 \\\n-vf \"scale=1280:720\" -c:v libx264 -preset veryfast -tune zerolatency \\\n-f flv rtmp:\/\/localhost\/live\/stream\n<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">2. \u64ad\u653e RTMP \u6d41<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528 <strong>VLC<\/strong> \u64ad\u653e RTMP \u6d41\uff1a arduino\u590d\u5236<code>rtmp:\/\/localhost\/live\/stream<\/code><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">3. \u64ad\u653e HTTP-FLV \u6d41<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u6d4f\u89c8\u5668\u4e2d\u64ad\u653e <strong>HTTP-FLV<\/strong>\uff1a bash\u590d\u5236<code>http:\/\/localhost:8080\/live\/stream.flv<\/code><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">?\ufe0f \u6b65\u9aa4 8\uff1a\u68c0\u67e5\u5e76\u8c03\u8bd5<\/h3>\n\n\n\n<p>\u5982\u679c\u4f60\u5728\u542f\u52a8\u6216\u63a8\u6d41\u8fc7\u7a0b\u4e2d\u9047\u5230\u95ee\u9898\uff0c\u53ef\u4ee5\u67e5\u770b Nginx \u9519\u8bef\u65e5\u5fd7\u6765\u6392\u67e5\u95ee\u9898\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bash\u590d\u5236<code>sudo tail -f \/usr\/local\/nginx\/logs\/error.log\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u5c06\u5e2e\u52a9\u4f60\u67e5\u770b\u8be6\u7ec6\u7684\u9519\u8bef\u4fe1\u606f\uff0c\u4ee5\u4fbf\u4f60\u505a\u8fdb\u4e00\u6b65\u7684\u8c03\u8bd5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">? \u5c0f\u7ed3<\/h2>\n\n\n\n<p>\u901a\u8fc7\u4ee5\u4e0a\u6b65\u9aa4\uff0c\u4f60\u6210\u529f\u7f16\u8bd1\u4e86\u4e00\u4e2a <strong>\u652f\u6301 RTMP \u548c HTTP-FLV<\/strong> \u7684 Nginx \u670d\u52a1\u5668\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u8fdb\u884c\u5b9e\u65f6\u89c6\u9891\u63a8\u6d41\u548c\u64ad\u653e\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6709\u4efb\u4f55\u95ee\u9898\uff0c\u6b22\u8fce\u968f\u65f6\u5411\u6211\u63d0\u95ee\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u540c\u65f6\u5728 Nginx \u4e2d\u542f\u7528 nginx-http-flv-module \u548c nginx-rtmp-module<\/p>\n<div class=\"more-link\">\n\t\t\t\t <a href=\"http:\/\/www.inhhh.com\/blog\/?p=947\" class=\"link-btn theme-btn\"><span>Read More <\/span> <i class=\"fa fa-caret-right\"><\/i><\/a>\n\t\t\t<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-947","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/947","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=947"}],"version-history":[{"count":1,"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/947\/revisions"}],"predecessor-version":[{"id":948,"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/947\/revisions\/948"}],"wp:attachment":[{"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=947"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=947"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.inhhh.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=947"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}