Home > database >  Apache Website Nodejs backend Websocket server
Apache Website Nodejs backend Websocket server

Time:10-19

I'm trying to make my website works with a nodejs backend and a websocket server

my website is entirely in https my node backend is on port 8080 with my websocket server on 8080 I made a virtualhost like that

<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>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

But when I tried to go on myip:8080 it doesn't work and same for connecting to my websocket What did I do wrong ?

CodePudding user response:

Enable mod_proxy_wstunnel

Then you should be able to forward only your location to your websocket server.

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

CodePudding user response:

Ok after a lot of work. I figured out. I think the problem come with the firewall when I installed my website.

so I have to proxypass everything on my secure connection. The mod_proxy_wstunnel is not working because when I tried to connect with wss:// so I had to use rewriteEngine.

Here is my final working virtual host

<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>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related