NodeJs Error during WebSocket handshake - node.js

I am able to establish a websocket connection, from a clienJS to NodeJs. But fails to connect websocket, when the request pass through apache httpd.
Using Httpd2.4.7, I am getting the below error. Please let me know what needs to be corrected.
WebSocket connection to 'ws://172.27.38.86/socket.io/1/websocket/_uW8Sv7lgQfrZncTSzKu' failed: Error during WebSocket handshake: Unexpected response code: 502
Thanks & Regards
Jawahar

Apache SUCKS at handling websockets through proxies. I recommend either getting rid of the Apache layer, or modifying your socket.io settings to use XHR polling.

After 2.4.5, apache included a module called proxy_wstunnel on their trunk, not available yet on current ubuntu production versions of apache (2.4.7). It was somehow painful, but following roughly the steps listed on this blog-post I managed to install the module and use it successfully.
dpkg -s apache2 //this gives you the version of apache in my case "2.4.7-1ubuntu4.1"
//then you checkout that version of apache
svn co http://svn.apache.org/viewvc/httpd/httpd/tags/2.4.7/
//you get into the directory just checked out
cd 2.4.7
//in there you checkout the Apache Portable Runtime Project and utils
svn co http://svn.apache.org/repos/asf/apr/apr/branches/1.4.x srclib/apr
svn co http://svn.apache.org/repos/asf/apr/apr-util/branches/1.3.x srclib/apr-util
//you compile with the corresponding modules flags
./buildconf
./configure --enable-proxy=shared --enable-proxy_wstunnel=shared
make
//You copy the modules (mod_proxy and mod_proxy_wstunnel) to your apache working copy
//It could be advisable to backup the old mods first
sudo cp modules/proxy/.libs/mod_proxy{_wstunnel,}.so /usr/lib/apache2/modules/
sudo chmod 644 /usr/lib/apache2/modules/mod_proxy{_wstunnel,}.so
sudo echo -e "# Depends: proxy\nLoadModule proxy_wstunnel_module /usr/lib/apache2/modules/mod_proxy_wstunnel.so" | sudo tee -a /etc/apache2/mods-available/proxy_wstunnel.load
//you then enable your module and restart your apache... so now the module is ready to use
sudo a2enmod proxy_wstunnel
sudo service apache2 restart
and after all that, researching a little bit I found this page I configured my apache vhost file accordingly like this
<VirtualHost *:80>
ServerAdmin yourmail#mail.com
ServerName yoursubdomain.yourdomain.info
Redirect permanent / https://yoursubdomain.yourdomain.info
</VirtualHost>
<VirtualHost *:443>
ServerAdmin yourmail#mail.com
ServerName yoursubdomain.yourdomain.info
ProxyRequests off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPreserveHost On
//these next two lines are to enable the wstunnel
ProxyPass /socket.io/1/websocket ws://localhost:9091/socket.io/1/websocket
ProxyPassReverse /socket.io/1/websocket ws://localhost:9091/socket.io/1/websocket
//this line is to retrieve the socket.io.js to use
ProxyPass /socket.io/ http://localhost:9091/socket.io/
SSLEngine on
SSLCertificateFile /etc/ssl/certs/yourcert.crt
SSLCertificateKeyFile /etc/ssl/private/yourcert.key
SSLCertificateChainFile /etc/ssl/certs/your_bundle.crt
//your logs
CustomLog /var/log/apache2/yoursubdomain.yourdomain.info.log combined
ErrorLog /var/log/apache2/yoursubdomain.yourdomain.info.error.log
</VirtualHost>
And TaDaaa... I have a working node websocket responding on port 9091 through an apache proxy that gets everything trough the 443 standard ssl port.
I suppose ubuntu will soon include this module on their production version, but meanwhile this is the way to go

I had to setup ws tunnel in CentOs, where apache 2.2.15 was installed by default.
I have tried patching proxy_wstunnel module with apache 2.2.15. But no help. Finally I have decided to remove apache 2.2.15 and install apache 2.4 by following the official documentation from http://httpd.apache.org/docs/2.4/install.html
After installation (I have installed in the default location /usr/local/apache2/), I did the following to get tunneling work
uncomment the following lines from /usr/local/apache2/conf/httpd.conf
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so
Add a virtual host for tunneling websocket requests
<VirtualHost *:80>
ServerName subdomain.mydomain.com
ProxyPass / ws://localhost:8081/
ProxyPassReverse / ws://localhost:8081/
</VirtualHost>
from my frontend the websocket is connected via ws://subdomain.mydomain.com:80
restart apache using
sudo /usr/local/apache2/bin/apachectl -k restart

Related

Nextjs static site throws error apache2 error while deployed to ubantu 20.04 1GB RAM

I have Nextjs Static site deployed to cloud server on ubantu 20.04 matchine. Only the issue is Apache2 server does not start.
domain.conf script is as below
`<VirtualHost *:80>
ServerName domain
ServerAlias www.domain
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
<Proxy *>
Require all granted
</Proxy>
ProxyPass / http://127.0.0.1:3000/
ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>`
then enable domain.conf
sudo a2ensite domain.conf
then enable below modules
sudo a2enmod proxy proxy_http rewrite headers expires
then start apache, but it throws error
sudo systemctl status apache2
check the apache status
sudo systemctl status apache2
above command pointed out the error as below
AH00526: Syntax error on line 10 of /etc/apache2/sites-enabled/domain.conf:
Invalid command '\xa0Require', perhaps misspelled or defined by a module not included i>
What went wrong in the script?
I have already obtained ssl as below and forever is also running successfully
sudo apt install certbot python3-certbot-apache
sudo certbot -d domain -d www.domain --apache --agree-tos -m myemail#gmail.com --no-eff-email --redirect
sudo certbot renew --dry-run
Thanks
I tried to resolved the issue but could not able to realized what went wrong exactly.Why Apache server does'nt start.

Proxy a node/react application in an apache server

Using forever to forever run the node server on the virtual machine, I am unable to get the app to run without explicitly adding the port in the url like so: URL.com:8080
If I don't use the port in the URL, I do load up the file structure of the application.
Steps to reproduce: I have a create-react-app application.On the virtual server I run 'npm run build' to make sure I have a build to serve. I then run forever start on the root of the application.
The code below should give all the necessary details. I can provide more if you need.
I have spent so much time tweaking the .conf file to try different configurations but I can't seem to get it. I am using it and successfully hosting two static html sites but not this node application.
Package.json:
...
"main":"server/index.js",
"proxy":"http://localhost:8080"
...
Apache url.conf:
<VirtualHost *:80>
ServerName URL.com
ServerAlias URL.com:8080/
DocumentRoot /var/www/nameOfApp/
<Directory />
Options -Indexes +FollowSymLinks
AllowOverride All
</Directory>
<Directory /var/www/nameOfApp/public>
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
</Directory>
</VirtualHost>
Node server file using express:
app.use(express.static(`${__dirname}/../build`));
I've also made sure I have the modules enabled to allow for proxying. So I think essentially, what I need is to request this site and not need the :8080 at the end.
Apache and NodeJS are 2 different and separate application.
You interact with them by sending request to the port that they are listening to. In your case here,
Apache is listening at port 80
Your NodeJS application is listening at port 8080
So all request to port 80 will be handled by Apache, and since you do not has an index.html, Apache will default to just list out the files and directory (Options Indexes). Up until this point, your node application do not know anything about your request.
So what you need to do is define some endpoint, say url.com/node, and tell Apache to forward all request of this endpoint to port 8080 and let your node application to do the job.
How to do this?
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypass
Best practices when running Node.js with port 80 (Ubuntu / Linode)
Node.js + Nginx - What now?
Hope this points you to the right direction.
The configuration that eventually worked was as simple as this:
<VirtualHost *:80>
ServerName yourdomain.com
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
</VirtualHost>
Looks like I was over complicating it trying to create a complicated proxy but the solution was very simply adding this to the config for the node application and then running sudo systemctl restart apache2 and everything worked beautifully.

How to deploy NodeJS Rest API in Ubuntu LTS 18 in apache2

I am new to NodeJS and I have a simple website that sends the contact-us information from ReactJS to NodeJS via Axios.
It is working on my local machine and I am trying to deploy it in my AWS EC2 Ubuntu LTS 18.
I have installed the PM2 already and I am stuck on what should I do next, how can I deploy my NodeJS in Ubuntu with Apache2 installed and make it run on my server even if I close my terminal. Also, what would be the API URL endpoint?
I hope someone could help me with this basic nodejs deployment in Ubuntu.
You have 2 problems:
to move the code to the server - you can use shipit.js (https://github.com/shipitjs/shipit) in order to do this. Take a look at this screencast about shipit.js and forever https://youtu.be/8PpBySjkWEM, forever is something like pm2.
to redirect traffic from Apache to your app. It is called reverse proxy. Conf file for that would be:
<VirtualHost *:80>
DocumentRoot **where-your-app-public-files-are**
ServerName **domain_name**
ProxyRequests off
ProxyPreserveHost on
ProxyPass / http://127.0.0.1:**your-node-port**/
ProxyPassReverse / http://127.0.0.1:**your-node-port**/
</VirtualHost>
Change **variables** to your data.
So it could be:
<VirtualHost *:80>
DocumentRoot /var/www/your-app
ServerName your-domain-name.com
ProxyRequests off
ProxyPreserveHost on
ProxyPass / http://127.0.0.1:4040/
ProxyPassReverse / http://127.0.0.1:4040/
</VirtualHost>

Apache reverse proxy configuration issue

i have a problem with setting up an apache reverse proxy server and hope you can help.
I have 3 ubuntu web servers, available on https://service1.domain.com, https://service2.domain.com:4433 and so on...
Now, i will access these servers without typing the port in the addressbar.
So my idea is to use an reverse proxy server, that i can type in service2.domain.com and it redirects to service2 (https).
I installed an ubuntu server with apache and enabled the modules:
sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests
Then i set up the 000-default.conf on the proxy with the following content:
<VirtualHost service1.domain.com:80>
ProxyPreserveHost On
ProxyPass / https://service1.domain.com/
ProxyPassReverse / https://service1.domain.com/
</VirtualHost>
<VirtualHost service2.domain.com:80>
ProxyPreserveHost On
ProxyPass / https://service2.domain.com/
ProxyPassReverse / https://service2.domain.com/
</VirtualHost>
The ports 80 and 443 on the router are forwarded to the proxy server.
On the service(1-3) servers, SSL is enabled with certificates from Lets Encrypt.
Now, if i try to open site service1.domain.com, i get an error (cert_name).
The sites now should not be accessible directly, because there is no port forwarding anymore.
My question is now, how is the right config for reverse proxies? Do i need to enable a certificate for each service also on the proxy server?
Thank you for your help!
Not exactly sure what your end goal is. The certificate is for the client facing server. If you want people to hit the site without having to set the port, you can use the Redirect statement in the virtual host config.
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
Redirect permanent / https://www.example.com
</VirtualHost>
Which would forward any non ssl traffic to use the ssl virtual host.

Apache 2.2.3 reverse proxy issue

I have to setup a website https://xyz.abc.co.in using self-signed certificate. I have apache 2.2.3 on Redhat Linux. But the home page of that website should come from a remote server application i.e http://10.x1.x2.x3:7080/app working on glassfish.
When i write http://10.x1.x2.x3:7080/app in browser it opens http://10.x1.x2.x3:7080/app_web/login.html
Now on my Apache 2.2.3 i have following entry in my httpd.conf to call http://10.x1.x2.x3:7080/app through remote proxy :-
< VirtualHost * :443 >
ServerName xyz.abc.co.in
SSLEngine On
SSLProxyEngine On
ProxyRequests Off
ProxyPreserveHost On
SSLCertificateFile /etc/*/redhat.crt
SSLCertificateKeyFile /etc/*/redhat1.key
SetOutputFilter proxy-html
ProxyPass / http://10.x1.x2.x3:7080/app/
ProxyPassReverse / http://10.x1.x2.x3:7080/app/
< /VirtualHost>
Now when i use https://xyz.abc.co.in in browser it gives error "NO DATA RECIEVED".
I have loaded module proxy_html also.
When i remove directive "SetOutputFilter proxy-html" then it gives error that :-
/app_web/login.html is not found on port 80 of apache 2.2.3 ( although it is hosted on glassfish on remote server). When I use "ProxyHTMLInterp On" then also there are the same errors recieved in browser.
....When i use "ProxyHTMLEnable On" it says invalid command while starting apache.
Please suggest.

Resources