zoukankan      html  css  js  c++  java
  • nginx + http2.0 解决浏览器跨域和同源限制问题

    一、srs
    1. 编译srs
    ./configure --with-librtmp --without-ssl
    make

    2. 配置srs
    编辑./conf/srs.conf

    # main config for srs.
    # @see full.conf for detail config.

    listen              1935;
    max_connections     1000;
    srs_log_tank        file;
    srs_log_file        ./objs/srs.log;
    http_api {
        enabled         on;
        listen          1985;
    }
    http_server {
        enabled         on;
        listen          8080;
        dir             ./objs/nginx/html;
    }
    stats {
        network         0;
        disk            sda sdb xvda xvdb;
    }
    vhost __defaultVhost__ {
        http_remux {
            enabled     on;
            mount       [vhost]/[app]/[stream].flv;
            hstrs       on;
        }
        hls{
            enabled       on;
            hls_path      ./objs/nginx/html;
            hls_fragment  10;
            hls_window    60;
        }
    }


    二、nginx
    1. 编译srs
    ./configure
    --with-pcre=/home/dong/src/pcre-8.41
    --with-openssl=/home/dong/src/openssl-OpenSSL_1_0_2u
    --without-http_gzip_module
    --without-http_upstream_zone_module
    --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module
    make
    sudo make install

    2. 生成密钥
    https://blog.csdn.net/CJY8023/article/details/106716378


    3. 配置nginx
    编辑/usr/local/nginx/conf/nginx.conf

    #user  nobody;
    worker_processes  1;

    #error_log  logs/error.log;
    #error_log  logs/error.log  notice;
    #error_log  logs/error.log  info;

    #pid        logs/nginx.pid;


    events {
        worker_connections  1024;
    }


    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  localhost;

            #charset koi8-r;

            #access_log  logs/host.access.log  main;

            location / {
                root   html;
                index  index.html index.htm;
            }

            #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   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;
            #}
        }


        # another virtual host using mix of IP-, name-, and port-based configuration
        #
        #server {
        #    listen       8000;
        #    listen       somename:8080;
        #    server_name  somename  alias  another.alias;

        #    location / {
        #        root   html;
        #        index  index.html index.htm;
        #    }
        #}


        # HTTPS server
        #
        server {
            listen       8443 ssl http2;
            server_name  localhost;

            ssl_certificate      server.crt; #cert.pem;
            ssl_certificate_key  server.key; #cert.key;

            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;

            ssl_ciphers  HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers  on;

            location / {
                root   html;
                index  index.html index.htm;
                proxy_pass http://localhost:8080;
                add_header Cache-COntrol no-cache;
                add_header Access-Control-Allow-Origin *;
            }
        }

        server {
            listen       9001 ssl http2;
            server_name  localhost;
     
            ssl_certificate      server.crt; #cert.pem;
            ssl_certificate_key  server.key; #cert.key;
     
            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
     
            ssl_ciphers  HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers  on;
     
            location / {
                root   html;
                index  index.html index.htm;
                proxy_pass http://localhost:9000;
                add_header Cache-COntrol no-cache;
                add_header Access-Control-Allow-Origin *;
            }
        }

    }

    三、启动nginx和srs,即可通过nginx的proxy模块将http2.0的8443端口映射到http1.0的8080端口
    root@Ubuntu14:~#/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    root@Ubuntu14:~#./objs/srs -c ./conf/srs.conf

    四、http-flv视频预览
    1. http2.0需要启动nginx
    https://120.xx.xxx.153:8443/live/livestream.flv

    2. 也可以用http1.0来预览
    http://120.xx.xxx.153:8080/live/livestream.flv

    五、打包发布
    1. 打包nginx
    tar czvf nginx.bin.tar.gz /usr/local/nginx
    2. srs只需要打包objs和conf目录

    六、软件包的应用
    1. nginx
    1)解压nginx.bin.tar.gz到usr/local目录
    如果打包带了目录,就不需要-C指定目录
    tar xzvf nginx.bin.tar.gz -C /usr/local
    2)启动nginx
    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    3)更改了nginx配置文件需要重新导入
    /usr/local/nginx/sbin/nginx -s reload

    2. srs
    1)解压srs-2.0-r6.bin.tar.gz
    tar xzvf srs-2.0-r6.bin.tar.gz
    2)启动srs
    ./objs/srs -c ./conf/srs.mtxy.conf


    参考

    https://www.cnblogs.com/yjmyzz/p/srs_study_2_hls_rtmp_httpflv_and_cross-domain.html

    https://blog.csdn.net/CJY8023/article/details/106716378



  • 相关阅读:
    python字典的遍历
    python字典
    python可变对象
    python元组
    python的range()
    python遍历列表
    Kafka的知识总结(18个知识点)
    为什么fastjson字段为null时不输出空字符串?
    oracle建表字段包含关键字注意事项
    spring websocket 使用@SendToUser
  • 原文地址:https://www.cnblogs.com/dong1/p/12504927.html
Copyright © 2011-2022 走看看