在k8s集群中去部署nginx服务以及配置图片,mp4播放(通过rancher操作)

前言

最近需要在平台里面集成视频服务,所以决定用nginx来代理视频地址,在网上找来很多关于nginx配置mp4播放,结果都没发用(都不自己测一下就发的吗?)。在一番摸索下弄好了,决定记一笔。

部署

本次的nginx是在rancher里面操作的,不过用命令行去创建的话也是同理。
为了修改配置方便一点,我们决定给nginx添加一个configmap。主要是加了下图的两个配置(网上众说纷纭,挨着测试一圈一直都是错的...下面是我测试好了的配置)


image.png
## default.conf 配置

server {
    listen      80;
    listen  [::]:80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;

    }
 location ~ .*\.(gif|jpg|jpeg|png)$ {
        root   /mnt/files/;
        autoindex on;
     
    }
    location ~ \.(mp3|mp4) {
        root /mnt/files/;
       autoindex on; #开启nginx目录浏览功能

     autoindex_exact_size off; #文件大小从KB开始显示

     autoindex_localtime on; #显示文件修改时间为服务器本地时间
    }
       location /bucket-p/ {
            alias    /infinityfs1/hivefiles/sobeyhive/bucket-p/;
            
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,DELETE';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
            if ($request_method = "OPTIONS") {
                add_header 'Access-Control-Allow-Origin' '*';
                add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Length' 0;
                add_header 'Content-Type' 'text/plain,charset=utf-8';
                return 204;
            }
            # Expire Headers
            if ($request_uri ~* \.(ico|css|js|gif|png|jpe?g)$) {
                expires 24h; #24h后expire  
                break;  
            }
        }
    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ \.php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

###  nginx.conf 配置


user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf; 
     autoindex on; #开启nginx目录浏览功能

     autoindex_exact_size off; #文件大小从KB开始显示

     autoindex_localtime on; #显示文件修改时间为服务器本地时间
}

然后是服务的基本配置,这个没啥好说的,命名空间的话在刚刚添加配置映射的时候就创建好了。


image.png

然后我们添加数据卷,首先是映射配置,把我们刚刚创建的config引用进来


image.png

再然后就是主机映射,这里主要就是你主机存放文件的目录和需要对应的nginx容器路径(这里地址根据自己主机和容器的情况而定,我随便搞的),从下图可以看出已经成功映射了
image.png

image.png

效果展示

视频

image.png

你可能感兴趣的:(在k8s集群中去部署nginx服务以及配置图片,mp4播放(通过rancher操作))