Nginx入门使用

安装

  • yum install -y nginx(没有软件包需安装软件包)
  • 安装软件包
    1. 创建文件:vi /etc/yum.repos.d/nginx.repo
    2. 写入如下配置:(centos7下)
      [nginx]
      name=nginx repo
      baseurl=http://nginx.org/packages/centos/7/x86_64/
      gpgcheck=0
      enabled=1
      
    3. 再次执行 :yum install -y nginx
    4. 环境变量配置
      vi /etc/profile
      export NGINX_HOME=/usr/local/nginx
      export PATH=$PATH:$NGINX_HOME/sbin
      重新加载 :source /etc/profile
      
      

端口映射

  1. 执行nginx -t 可看到配置文件路径(/etc/nginx/nginx.conf)
    [root@localhost yum.repos.d]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    
  2. 修改配置文件,执行vi /etc/nginx/nginx.conf,在最下面加上:
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;#监听端口
            server_name  10.10.10.204;#访问的ip
            location / {
                proxy_pass   http://10.10.10.204:8080;#转向的ip
            }
        }
    }
    
  3. 执行nginx -t 检查配置文件语法
  4. 重启nginx 执行 nginx -s reload
  5. 防火墙没关 执行systemctl status firewalld
  6. 访问http://10.10.10.204转向http://10.10.10.204:8080

静态资源映射

  1. 修改nginx配置文件
  2. 在http{}中加一个server
    server {
            listen      9090 ;
            server_name  192.168.25.130;
            location /ckplayer/ {#资源路径
                root   /usr/local/static/ckplayer/;
                autoindex on;
            }
    
    
  3. nginx -s reload
  4. 访问:http://192.168.25.130:9090/ckplayer/ckplayer.js
    error:403 forbidden错误,检查一下nginx.conf 文件的上面有一个 usernobody 是不是打开的并且改成user root
    error:404 结合错误日志查看访问路径是否正确

Nginx-rtmp 流媒体服务器搭建

安装软件包

  1. 安装gcc编译器:yum -y install gcc gcc-c++
  2. yum install -y pcre pcre-devel
    PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库
  3. yum install -y zlib zlib-devel
    zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip
  4. yum install -y openssl openssl-devel
    OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)

安装nginx和rtmp

  1. nginx: https://nginx.org/en/download.html tar.gz包
  2. nginx-rtmp :https://github.com/arut/nginx-rtmp-module
  3. 解压安装:解压到/usr/local/src/ 目录下:
  4. 进入nginx包目录:执行
    ./configure --prefix=/usr/local/src/nginx --add-module=../nginx-rtmp-module-master --with-http_ssl_module
  5. make & make install
  6. 修nginx配置文件
    rtmp { 
        server {
            listen 1935;
        chunk_size 4096;
    
        application vod {
            play /opt/video/vod; #视频路径
        }
        }
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        #                  '$status $body_bytes_sent "$http_referer" '
        #                  '"$http_user_agent" "$http_x_forwarded_for"';
    
        #access_log  logs/access.log  main;
    
        sendfile        on;
        #tcp_nopush     on;
    
        #keepalive_timeout  0;
        keepalive_timeout  65;
        #gzip  on;
            server {
            listen       80;
            server_name  10.10.10.204;
            location / {
                proxy_pass   http://10.10.10.204:8080;
            }
        }}
    
  7. 访问:rtmp://10.10.10.204/vod/wyy.mp4 #wyy.mp4是/opt/video/vod下的视频文件。
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容