navigator.mediaDevices是undefined怎么办

147 阅读1分钟

navigator.mediaDevices如果是undefined,则不能使用getUserMedia()

三个解决方法:

  1. 网页使用https访问

  2. 使用 localhost或127.0.0.1 进行访问

  3. 修改浏览器安全配置

    1)浏览器地址栏输入:chrome://flags/ #unsafely-treat-insecure-origin-as-secure

    2)修改 Insecure origins treated as secure 项,添加上自己要使用的域名

    3)点击Relaunch

针对方法1,如果网页是http请求,不改代码的情况下,如何转换为https?

配置nginx代理:

server {
        listen       9007;
        server_name  xx.xx.xx.xx; #前端服务部署ip
        root         /root/huiyi/dist;
        sendfile        on;
        tcp_nopush      on;
        default_type text/html;

        location /api/{
          proxy_pass http://yy:yy:yy:yy:9014; #文件服务器
          rewrite ^/api/(.*) /$1 break;
          proxy_set_header        Host $host;
          proxy_set_header        X-Real-IP $remote_addr;
          proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location / {
        index  /index.html;
        try_files $uri $uri/ /index.html;
         }

       # location /v1/{
        # proxy_pass http://ee.ee.ee.ee:18080/;  //配置websocket的
        # proxy_http_version 1.1;
        # proxy_set_header Upgrade $http_upgrade;
        # proxy_set_header Connection "upgrade";
        #proxy_read_timeout 600s;
        # }


        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
        root   html;
        }
    }
    
    
    
    # Settings for a TLS enabled server.
    server {
        listen       9008 ssl;
       # listen       [::]:443 ssl http2;
       # ssl on;
       # server_name  _;
        server_name xx.xx.xx.xx;
        root         /usr/share/nginx/html;

        ssl_certificate "/etc/nginx/ssl/dd.dd.dd.dd.crt";
        ssl_certificate_key "/etc/nginx/ssl/dd.dd.dd.dd.ey";
        ssl_session_cache shared:SSL:1m;
        ssl_session_timeout  10m;
        ssl_ciphers HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers on;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        error_page 497 https://$host:$server_port$uri$is_args$args;

        location / {
            proxy_pass http://xx.xx.xx.xx:9007/;
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }