在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的网站使用??

阅读 14.9k
2 个回答

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

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题