websocket配置wss访问

这篇具有很好参考价值的文章主要介绍了websocket配置wss访问。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。


前言

做一个小程序项目,3d多人聊天室互动,有两台服务器,windows系统和contos7
分别用来写小程序逻辑和部署socket.io

由于小程序里面都是https的请求,所以socket.io请求需要从ws(未加密)改成wws(加密)

下面应该是使用nginx反向代码解决这wss访问问题
两个简单问题解决记录


一、socket使用加密访问

由于小程序里面都是https访问,直接访问socket监听的3000端口会报错:

Mixed Content: The page at 'https://xxx/test/index.html' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://xxx:3000/socket.io/?EIO=4&transport=websocket'. This request has been blocked; this endpoint must be available over WSS.

在centos7上面,使用nginx反向代理可以解决(反向代理真的好用!)

一键安装nginx

sudo yum install -y nginx

直接修改默认的配置文件如下:(自备ssl证书)
就是把443端口注释开启,最后加了一个反向代理到本地的3000端口,也就是我socket监听的端口

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;

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

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

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




#Settings for a TLS enabled server.

   server {
       listen       443 ssl http2;
       listen       [::]:443 ssl http2;
       server_name  _;
       root         /usr/share/nginx/html;

       ssl_certificate "/etc/nginx/crt/gbmax.com.cn_bundle.crt";
       ssl_certificate_key "/etc/nginx/crt/gbmax.com.cn.key";
       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 404 /404.html;
           location = /40x.html {
       }

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

       location /
	    {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

	        proxy_pass http://127.0.0.1:3000;
	        proxy_http_version 1.1;
	        proxy_set_header Upgrade $http_upgrade;
	        proxy_set_header Connection "Upgrade";
	        proxy_set_header X-Real-IP $remote_addr;
	    }
   }

}


二、nginx反向代理之后socket请求跨域设置失效

本来socket已经配置了跨域了,但是反向代理后依旧还有跨域报错
上面的配置文件已经写了,在反向代理之前设置nginx跨域即可

add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';

总结

解决办法就是nginx 监听443端口配置证书,反向代理socket服务文章来源地址https://www.toymoban.com/news/detail-426603.html

到了这里,关于websocket配置wss访问的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包