Apache网站+ Nodejs后端+ Websocket服务器



我正试图使我的网站与nodejs后端和websocket服务器工作

我的网站是完全在HTTPS我的节点后端在端口8080上,我的websocket服务器在8080上我创建了一个这样的虚拟主机

<VirtualHost *:8080>
RewriteEngine On
RewriteCond %{REQUEST_URI}  ^/socket.io            [NC]
RewriteCond %{QUERY_STRING} transport=websocket    [NC]
RewriteRule /(.*)           ws://localhost:8080/$1 [P,L]
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>

<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/www/example.com
ServerName www.example.com
ServerAlias example.com
<Directory /var/www/example.com>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Allow from all
Require all granted
</Directory>
<Directory /var/www/example.com/wp-content>
Options -Indexes +FollowSymLinks +MultiViews
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile    /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile   /etc/letsencrypt/live/example.com/privkey.pem
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=15553000; includeSubDomains; preload"
</IfModule>
ErrorLog /var/log/apache2/error.example.com.log
CustomLog /var/log/apache2/access.example.com.log combined
</VirtualHost>

但是当我试图去我的ip:8080它不工作,同样连接到我的websocket我做错了什么?

启用mod_proxy_wstunnel

那么你应该能够只转发你的位置到你的websocket服务器。

ProxyPass /wssurl/ ws://127.0.0.1:8080/

好了,辛苦了。我明白了。我想问题出在我安装网站时的防火墙上。

所以我必须通过我的安全连接上的所有内容。mod_proxy_wstunnel不工作,因为当我试图连接wss://所以我不得不使用rewriteEngine.

这是我最终的工作虚拟主机

<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
Redirect permanent / https://example.com/
</VirtualHost>
<VirtualHost *:443>
DocumentRoot /var/www/example.com
ServerName www.example.com
ServerAlias example.com
<Directory /var/www/example.com>
Options -Indexes +FollowSymLinks +MultiViews
AllowOverride All
Allow from all
Require all granted
</Directory>
<Directory /var/www/example.com/wp-content>
Options -Indexes +FollowSymLinks +MultiViews
Require all granted
</Directory>
SSLEngine on
SSLCertificateFile    /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile   /etc/letsencrypt/live/example.com/privkey.pem
<IfModule mod_headers.c>
Header always set Strict-Transport-Security "max-age=15553000; includeSubDomains; preload"
</IfModule>
ErrorLog /var/log/apache2/error.example.com.log
CustomLog /var/log/apache2/access.example.com.log combined
ProxyRequests On
ProxyPreserveHost On
ProxyPass /api/test http://localhost:8080
ProxyPassReverse /api/test http://localhost:8080
RewriteEngine On
RewriteCond %{HTTP:Upgrade} websocket [NC]
RewriteCond %{HTTP:Connection} upgrade [NC]
RewriteRule ^/?(.*) "ws://localhost:8080/$1" [P,L]
</VirtualHost>

最新更新