在https网站上用socket.io,socket使用http,然后blocked:mixed-content,怎么解决?

  1. socket使用3000端口,并且使用ip访问:
    http://192.54.2.1:3000/socket.io/?EIO=3&transport=polling&t=M8f3APD
  2. nginx把域名下的所有http都转到https
  3. socketip换成域名无法访问;

主要问题:http的socket如何在https的网站使用??

回答:

nginx 配置文件

### 强制把域名下的所有http都转到https
server {
    listen 80;
    server_name ineedtm.com www.ineedtm.com;
    rewrite ^(.*) https://$server_name$1 permanent;
}
# HTTPS server
#
server {
    # listen 80;
    listen       443 ssl;
    server_name  ineedtm.com www.ineedtm.com;

    ssl_certificate   cert/2xxx50.pem;
    ssl_certificate_key  cert/2xx50.key;
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers on;

    root /xxx/blog;

    location / {
        index index.html;
    }
    
    # 后端接口反向代理设置,请忽略
    location /api/ {
       proxy_pass http://api.ineedtm.com/api/;
    }
    
    # socket代理配置
    location /socket.io/ {
        proxy_pass http://192.54.2.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

注意:
配置前端socket连接时别带端口,但要配置到https://ineedtm.com/而不是htt…://ineedtm.com/socket.io/

回答:

你需要在nginx里面处理一下websocket

暂无评论

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注