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模板网!

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

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

相关文章

  • windows下采用 nginx配置websocket支持wss流程

    第一步、安装OpenSSL (1)下载OpenSSL软件包 地址:https://slproweb.com/products/Win32OpenSSL.html OpenSSL版本说明: Win64 OpenSSL v1.1.1wLight,安装Win64 OpenSSL v1.1.1w最常用的软件包 Win64 OpenSSL v1.1.1w,安装Win64 OpenSSL v1.1.1w完整软件包 Win32 OpenSSL v1.1.1w Light,安装Win32 OpenSSL v1.1.1w最常用的软件包

    2024年02月22日
    浏览(30)
  • 关于Nginx配置SSL证书(Https)和WebSocket的wss

    一. 生成SSL自签证书        自签证书就是自己生成的证书,免费的,不支持部署浏览器的,支持浏览器的就是收费的,需要购买,这里因为是本地测试,所以就用的自签证书,买的证书可以跳过证书生成部分.  安装OpenSSL           OpenSSL是生成SSL的工具,这里是在Win10下安装的,下载的

    2023年04月14日
    浏览(39)
  • 一个vue项目配置访问两个服务器地址

    一个项目,其中一个模块A是部署在同一个服务器不同端口,这个时候开发就需要配置不同的访问。开发环境的时候, 同一个服务器:10.12.7.99, 其中有一个模块A的接口代码部署在 8899 这个端口,而其他接口的代码部署在 6090 这个端口。 先看vue.config.js这个页面的配置 对 .en

    2024年02月09日
    浏览(32)
  • Java 构建websocket客户端,构建wss客户端,使用wss连接,并发送数据到服务器端,接收服务器端消息

    Java 构建websocket客户端,构建wss客户端,使用wss连接,并发送数据到服务器端,接收服务器端消息 回调函数处理

    2024年02月13日
    浏览(45)
  • 多人开发共用一个nacos,怎样配置可以保证各自的请求不会请求到同事的电脑里,实现请求隔离

    1.在每个服务的配置文件里,给服务加上自己的后缀 2.配置gateway里,指向对应的带后缀服务名,如果是gateway是自动配置 可忽略此项

    2024年02月14日
    浏览(38)
  • 解决cloudflare worker遇到failed to dial to (wss://): 200 OK > websocket: bad handshake 的问题

    destination common/retry: [transport/internet/websocket: failed to dial WebSocket transport/internet/websocket: failed to dial to (wss://cw.fjh1997.top/): 200 OK websocket: bad handshake] common/retry: all retry attempts failed 开发的时候遇到这个问题,奈何浏览器抓不了websocket的握手包,正常握手包是101状态码的http数据包,但

    2024年03月14日
    浏览(48)
  • netty/websocket服务器配置阿里云SSL证书安全访问配置,亲测有效

    背景:java 微服务包括https访问和websocket访问,当https接口访问ws请求时报错,因为https能访问wss。 申请阿里云免费证书后,搜索各种教程比如nginx配置方式、netty访问证书等。走了不少弯路,终于走通一种。 关键点:1、因为使用了netty,nginx配置wss的方式没有走通。需要将证书放

    2024年02月01日
    浏览(35)
  • Ratchet实现PHP WebSocket多人聊天功能的示例

       composer 安装ratchet 使用PDO连接数据库,创建mysql命令如下 使用Redis存储消息列表 这个示例代码中,PHP代码使用Ratchet来创建WebSocket服务器,并实现了简单的聊天功能。HTML代码使用JavaScript来建立WebSocket连接,并处理消息传输和用户输入。要运行此代码,请确保已安装Ratchet并

    2024年02月11日
    浏览(30)
  • mac制作ssl证书|生成自签名证书,nodejs+express在mac上搭建https+wss(websocket)服务器

    mac 自带 openssl 所以没必要像 windows 一样先安装 openssl,直接生成即可 生成 key 让输入两次密码,随便,但是两次得是一样的 移除密码 生成 csr Country Name (2 letter code) [ 国家 ]:CN State or Province Name (full name) [ 省份 ]:Beijing Locality Name (eg, city) [ 城市 ]:Beijing Organization Name (eg, company)

    2024年02月09日
    浏览(37)
  • Spring MVC学习随笔-第一个Spring MVC程序(父子项目结构、Tomcat配置、ViewResolver)

    学习视频:孙哥说SpringMVC:结合Thymeleaf,重塑你的MVC世界!|前所未有的Web开发探索之旅 JDK1.8+ Maven3.6+ IDEA2021+ SpringFramework 5.1.4 Tomcat8.5.29 MySQL5.7.18 按照父子项目的结构,管理和创建项目,创建一个空Project作为父项目,pom文件如下 创建子项目Module: 子项目pom文件: 注意 :初次

    2024年02月05日
    浏览(50)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包