简体   繁体   English

CWP: SSL + WebSocket + Ngnix

[英]CWP: SSL + WebSocket + Ngnix

My CentOS 7 + CWP panel configured as:我的 CentOS 7 + CWP 面板配置为:

Nginx & Apache Additional Options: php-cgi/suphp, nginx/php-fpm, apache/php-fpm, proxy HTTP: Nginx (80) --> Apache (8181) HTTPS: Nginx (443) --> Apache (8181) Nginx & Apache Additional Options: php-cgi/suphp, nginx/php-fpm, apache/php-fpm, proxy HTTP: Nginx (80) --> Apache (8181) HTTPS: Nginx (443) --> Apache (8181 )

I want to run a WebSocket application that using localhost address 0.0.0.0:8282 for communication So I have put a cron job to run my chatserver WebSocket app on each server boot using a command:我想运行一个 WebSocket 应用程序,该应用程序使用本地主机地址 0.0.0.0:8282 进行通信所以我已经放置了一个 cron 作业来运行我的聊天服务器 WebSocket 应用程序在每个服务器启动时使用以下命令:

PHP index.php chatserver server

In the SSH console I can see that the server run without any issues:在 SSH 控制台中,我可以看到服务器运行没有任何问题:

Running server on host 0.0.0.0:8282在主机 0.0.0.0:8282 上运行服务器

But in the browser client side I can see an error like that:但是在浏览器客户端我可以看到这样的错误:

WebSocket connection to 'wss:// . WebSocket 连接到 'wss:// :8282/? :8282/? ' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED socket.js?v= :** WebSocket connection to 'wss:// . ' 失败:连接建立错误:net::ERR_CONNECTION_REFUSED socket.js?v= :** WebSocket 连接到 'wss:// :8282/?***' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED :8282/?***' 失败:连接建立错误:net::ERR_CONNECTION_REFUSED

Looks like Nginx doesn't let to pass the socket connection on the port 8282. Could you please to help me to change the Nginx configuration file of the specific domain that works over SSL to successfully establish WebSocket connection to the port 8282 and connected to my localhost chatserver. Looks like Nginx doesn't let to pass the socket connection on the port 8282. Could you please to help me to change the Nginx configuration file of the specific domain that works over SSL to successfully establish WebSocket connection to the port 8282 and connected to my本地主机聊天服务器。 I will appreciate any help.我将不胜感激。

The error dictates that the websocket could not connect via port 8282 you need to set up a TLS certificate for this since WebRTC requires secure connection.该错误表明 websocket 无法通过端口 8282 连接,您需要为此设置 TLS 证书,因为 WebRTC 需要安全连接。

I believe the answer to your question is here - How to Create Secure(TLS/SSL) Websocket Server我相信您的问题的答案就在这里 - 如何创建安全(TLS/SSL)Websocket 服务器

I've found a way to redirect the server's websocket port to port 80 (or wss to 443) using nginx and CWP.我找到了一种使用 nginx 和 CWP 将服务器的 websocket 端口重定向到端口 80(或 wss 到 443)的方法。

On the vhosts conf file (WebServers Conf Editor > /etc/nginx/conf.d/vhosts/ tab), you should locate the location / {... } part.在 vhosts conf 文件(WebServers Conf Editor > /etc/nginx/conf.d/vhosts/ 选项卡)上,您应该找到location / {... }部分。

Replace the entire location / {... } part with this:将整个location / {... }部分替换为:

location / {
  proxy_set_header HOST $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_pass_request_headers on;
  proxy_pass http://YOUR_SERVER_IP:WEBSOCKET_PORT;
  proxy_http_version 1.0;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "Upgrade";
}

You should change only YOUR_SERVER_IP and WEBSOCKET_PORT part with the respective information.您应该只使用相应的信息更改YOUR_SERVER_IPWEBSOCKET_PORT部分。 Leave the rest as it is. rest 保持原样。 If you are editing the SSL conf file, instead of http you should use https .如果您正在编辑 SSL conf 文件,而不是 http,您应该使用https

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM