0


https协议下配置websocket问题(踩坑)

场景:

在本地开发环境中一直使用的是windows+http+websocket,本地测试都是正常的,但是部署到线上时使用的是https,导致websocket一直连接失败

本地环境

本地环境前端:

ws://ip.端口/GisqPlatformExplorer/ws/login.do

其中loginWebSocketUrl=ws://192.168.xx.xx:8082

本地环境后端

原理就是:当我websocket访问路径含有/ws/login.do时,会对该访问进行loginwebsocketHandler处理。我这里功能主要是访问该路径时,记录用户登录登出日志的功能(前面博客有写过)。

现场线上环境:

linux+https+nginx+websocket

nginx配置

  1. worker_processes 1;
  2. events {
  3. worker_connections 1024;
  4. }
  5. http {
  6. include mime.types;
  7. default_type application/octet-stream;
  8. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  9. # '$status $body_bytes_sent "$http_referer" '
  10. # '"$http_user_agent" "$http_x_forwarded_for"';
  11. #access_log logs/access.log main;
  12. sendfile on;
  13. #tcp_nopush on;
  14. #keepalive_timeout 0;
  15. keepalive_timeout 65;
  16. client_max_body_size 1024M;
  17. #gzip on;
  18. server {
  19. listen 11921 ssl;
  20. server_name localhost;
  21. ssl_certificate .\ssl\8872433_xxx.cn.pem;
  22. ssl_certificate_key .\ssl\8872433_xxx.cn.key;
  23. ssl_session_cache shared:SSL:1m;
  24. ssl_session_timeout 5m;
  25. ssl_ciphers HIGH:!aNULL:!MD5;
  26. ssl_prefer_server_ciphers on;
  27. location /GisqPlatformExplorer/ws/login.do {
  28. proxy_pass http://xx.xx.xx.xx:8081;
  29. proxy_http_version 1.1;
  30. proxy_set_header Upgrade $http_upgrade;
  31. proxy_set_header Connection "Upgrade";
  32. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  33. proxy_set_header X-Real-IP $remote_addr;
  34. }
  35. error_page 500 502 503 504 /50x.html;
  36. location = /50x.html {
  37. root html;
  38. }
  39. }
  40. # HTTPS server
  41. server {
  42. listen 443 ssl;
  43. server_name localhost;
  44. ssl_certificate ssl/cert.crt;
  45. ssl_certificate_key ssl/cert.key;
  46. ssl_session_cache shared:SSL:1m;
  47. ssl_session_timeout 5m;
  48. ssl_ciphers HIGH:!aNULL:!MD5;
  49. ssl_prefer_server_ciphers on;
  50. location / {
  51. root html;
  52. index index.html index.htm;
  53. }
  54. location /GisqPlatformExplorer/ {
  55. proxy_pass http://localhost:8081;
  56. }
  57. }
  58. }

问题一:

Uncaught DOMException: Failed to construct 'WebSocket': An insecure WebSocket connection may not be initiated from a page loaded over HTTPS

原因:https协议下不能使用ws去访问

解决方案:改成wss://xx.xx.xx.xx:11921/GisqPlatformExplorer/ws/login.do与websocket建立连接

问题二:改用wss建立连接后提示连接失败

WebSocket connection to 'wss://xx.xx.xx.xxx:11921/GisqPlatformExplorer/ws/login.do' failed

** 有两个地方要注意:**

一个是域名问题:使用wss时必须要使用域名去访问。

一个是nginx配置问题: ‘

错误的配置:

原因主要是加“/”与不加“/”的区别没有搞清楚:nginx中斜杠(/)详解,配置location、proxy_pass时,加“/”与不加“/”的区别

参考:nginx中斜杠(/)详解_nginx_脚本之家

  1. Nginxproxy_pass的斜杠(/)问题 - 达摩院的BLOG - 博客园
  1. location /GisqPlatformExplorer/ws/login.do {
  2. proxy_pass http://xx.xx.xx.xx:8081/;
  3. proxy_http_version 1.1;
  4. proxy_set_header Upgrade $http_upgrade;
  5. proxy_set_header Connection "Upgrade";
  6. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  7. proxy_set_header X-Real-IP $remote_addr;
  8. }
  9. 实际访问代理地址:http://xx.xx.xx.xx7:8081/

正确的配置

  1. location /GisqPlatformExplorer/ws/login.do {
  2. proxy_pass http://127.0.0.1:8081/GisqPlatformExplorer/ws/login.do;
  3. proxy_http_version 1.1;
  4. proxy_set_header Upgrade $http_upgrade;
  5. proxy_set_header Connection "Upgrade";
  6. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  7. proxy_set_header X-Real-IP $remote_addr;
  8. }
  9. 实际访问代理地址:http://127.0.0.1:8899/GisqPlatformExplorer/ws/login.do
  1. location /GisqPlatformExplorer/ws/login.do {
  2. proxy_pass http://127.0.0.1:8081;
  3. proxy_http_version 1.1;
  4. proxy_set_header Upgrade $http_upgrade;
  5. proxy_set_header Connection "Upgrade";
  6. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  7. proxy_set_header X-Real-IP $remote_addr;
  8. }
  9. 实际访问代理地址:http://127.0.0.1:8081/GisqPlatformExplorer/ws/login.do

再次测试访问wss://xx.xx.xx.xxx:11921/GisqPlatformExplorer/ws/login.do路径时建立连接成功。

理论知识

(不了解必定会踩坑)

1.http情况下用ws,https情况下必须要使用wss

https相当于使用http+ssl认证,使用https时websocket访问(比如建立链接时)必须要使用wss

  1. 使用域名

https下一般都是域名去访问的,存在有域名解析的过程,因此wss时也要使用域名

3.使用域名+指定端口

这个之前没有了解过,只是认为域名就包含了端口,其实也是可以指定端口的下面是现场环境访问的路径

nginx斜杠问题:

参考如下写的不错,很容易理解

Nginx中proxy_pass的斜杠(/)问题 - 达摩院的BLOG - 博客园

  1. location /api1/ {
  2. proxy_pass http://localhost:8080;
  3. }

在访问http://localhost/api1/xxx时,会代理到http://localhost:8080/api1/xxx

  1. location /api2/ {
  2. proxy_pass http://localhost:8080/;
  3. }

当访问http://localhost/api2/xxx时,http://localhost/api2/(注意最后的/)被替换成了http://localhost:8080/,然后再加上剩下的xxx,于是变成了http://localhost:8080/xxx。

主要看proxy_pass http://localhost:8080,这种方式称为不带URI方式;

proxy_pass http://localhost:8080/xx或http://localhost:8080/,这种方式称为带URI方式;

其中不带URI方式主要是匹配(保留匹配路径及后面的所有),而带URI方式主要是替换(将匹配到的相同部分替换掉,然后保留完全匹配后面的所有)


本文转载自: https://blog.csdn.net/qq_38423256/article/details/128465212
版权归原作者 喜羊羊love红太狼 所有, 如有侵权,请联系我们删除。

“https协议下配置websocket问题(踩坑)”的评论:

还没有评论