nginx安装和使用

作者

QQ群:852283276
微信:arm80x86
微信公众号:青儿创客基地
B站:主页 https://space.bilibili.com/208826118

参考

搭建Nginx+nginx-rtmp-module的hls流媒体服务器并用OBS进行推流
Nginx-RTMP服务搭建
基于nginx的rtmp直播服务器(nginx-rtmp-module实现)
流媒体协议介绍(rtp/rtcp/rtsp/rtmp/mms/hls)
详解nginx websocket配置
Nginx支持WebSocket反向代理-学习小结

ubuntu

$ sudo apt install nginx
$ which nginx
/usr/sbin/nginx
$ ls -l /etc/nginx/
total 64
drwxr-xr-x 2 root root 4096 Dec 19 15:41 conf.d
-rw-r--r-- 1 root root 1077 Feb  4  2019 fastcgi.conf
-rw-r--r-- 1 root root 1007 Feb  4  2019 fastcgi_params
-rw-r--r-- 1 root root 2837 Feb  4  2019 koi-utf
-rw-r--r-- 1 root root 2223 Feb  4  2019 koi-win
-rw-r--r-- 1 root root 3957 Feb  4  2019 mime.types
drwxr-xr-x 2 root root 4096 May 26  2021 modules-available
drwxr-xr-x 2 root root 4096 Dec 11 23:11 modules-enabled
-rw-r--r-- 1 root root 1490 Feb  4  2019 nginx.conf
-rw-r--r-- 1 root root  180 Feb  4  2019 proxy_params
-rw-r--r-- 1 root root  636 Feb  4  2019 scgi_params
drwxr-xr-x 2 root root 4096 Dec 11 23:11 sites-available
drwxr-xr-x 2 root root 4096 Dec 11 23:11 sites-enabled
drwxr-xr-x 2 root root 4096 Dec 11 23:11 snippets
-rw-r--r-- 1 root root  664 Feb  4  2019 uwsgi_params
-rw-r--r-- 1 root root 3071 Feb  4  2019 win-utf

安装文件

  • /usr/sbin/nginx:主程序
  • /etc/nginx:存放配置文件
  • /usr/share/nginx:存放静态文件
  • /var/log/nginx:存放日志
$ ls dist
css  favicon.ico  fonts  index.html  js
$ sudo vim /etc/nginx/conf.d/dingchuan_iot.conf
$ ls -l /etc/nginx/conf.d/
total 8
-rw-r--r-- 1 root root  917 Jan 24 10:16 dingchuan_iot.conf
$ cat /etc/nginx/conf.d/dingchuan_iot.conf
server {
    # 需要被监听的端口号,前提是此端口号没有被占用,否则在重启 Nginx 时会报错
    listen       8090;
    # 服务名称,无所谓
    server_name  dingchuan_iot;

    # 上述端口指向的根目录
    root /home/ubuntu/dist;
    # 项目根目录中指向项目首页
    index index.html;

    client_max_body_size 20m;
    client_body_buffer_size 128k;

    # 根请求会指向的页面
    location / {
      # 此处的 @router 实际上是引用下面的转发,否则在 Vue 路由刷新时可能会抛出 404
      try_files $uri $uri/ @router;
      # 请求指向的首页
      index index.html;
    }

    # 由于路由的资源不一定是真实的路径,无法找到具体文件
    # 所以需要将请求重写到 index.html 中,然后交给真正的 Vue 路由处理请求资源
    location @router {
      rewrite ^.*$ /index.html last;
    }
}
$ systemctl restart nginx

rtmp流媒体服务器

下载rtmp,为hi3531d交叉编译uclib版本的nginx,更新配置文件,

rtmp {  
  
    server {  
  
        listen 1935;  #监听的端口
  
        chunk_size 4000;  
        
        application hls {
            live on;
            hls on;
            hls_path /usr/local/html/hls;#视频流存放地址
            hls_fragment 5s;
            hls_playlist_length 15s;
            hls_continuous on; #连续模式。
            hls_cleanup on;    #对多余的切片进行删除。
            hls_nested on;     #嵌套模式。
        }
    }  
}

websocket

http{
 map $http_upgrade $connection_upgrade { #根据客户端请求中$http_upgrade 的值,来构造改变$connection_upgrade的值
  default upgrade;
  ''   close;
 }
 upstream websocket {
  #ip_hash;
  server localhost:8010; 
  server localhost:8011;
 }
# 以下配置是在server上下文中添加,location指用于websocket连接的path。
 server {
  listen    80;
  server_name localhost;
  access_log /var/log/nginx/yourdomain.log;
  location / {
   proxy_pass http://websocket;
   proxy_read_timeout 300s;
   proxy_set_header Host $host;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_http_version 1.1;
   proxy_set_header Upgrade $http_upgrade;
   proxy_set_header Connection $connection_upgrade;
  }
 }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值