High Load on server due to websocket and Apache - node.js

We have 2 servers (Ubuntu 14.04 - 4vCPUs and 12 GB RAM), running a codeigniter application with Apache 2.4.7. These servers are load balanced as well. We have an average of 300 users accessing the site at a time and also the website has refresh functionality in many areas. So we introduced websockets to reduce the load. But even after introducing that, we are facing high load. Node is running on one of these server. After enabling mod_status I can see new connections are not getting opened for apache.
Vhost
<VirtualHost *:443>
ServerName domain.com
ServerAlias www.domain.com
DocumentRoot /var/www/html/domain
SSLEngine on
SSLProxyEngine On
SSLCertificateFile /etc/apache2/ssl/domain-ssl/a.crt
SSLCertificateKeyFile /etc/apache2/ssl/domain-ssl/domain.key
SSLCertificateChainFile /etc/apache2/ssl/domain-ssl/gd_bundle.crt
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/node/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule "^/node/socket.io" "ws://IPADDRESS:8080/socket.io/" [P,L]
ProxyPreserveHost On
ProxyRequests off
</VirtualHost>
<Location /node/>
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} ^Upgrade$ [NC]
ProxyPass http://IPADDRESS:8080/ connectiontimeout=3 timeout=3 retry=0
ProxyPassReverse http://IPADDRESS:8080/
</Location>
Please let me know the details you need to investigate on this. Thanks

I installed a seperate server for node and redis which has reduced the load.

Related

Getting 404 on a specific path /pricing with Apache server [migrated]

This question was migrated from Stack Overflow because it can be answered on Server Fault.
Migrated 2 days ago.
I've set up an Apache server on Ubuntu 20.04
The site loads fine when I load the home page first (https://leadzilla.ai) and after that when I click on the pricing button and it takes me to https://leadzilla.ai/pricing and the that page loads fine as well.
But when I go directly to https://leadzilla.ai/pricing in the browser, I get a 404
Here is what I have in /etc/apache2/sites-available/leadzilla.ai.conf
<VirtualHost *:80>
DocumentRoot /var/www/leadzilla.ai
ServerName leadzilla.ai
ServerAlias www.leadzilla.ai
<Directory /var/www/leadzilla.ai>
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from all
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =leadzilla.ai [OR]
RewriteCond %{SERVER_NAME} =www.leadzilla.ai
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
I have this config in /etc/apache2/sites-available/leadzilla.ai-le-ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot /var/www/leadzilla.ai
ServerName leadzilla.ai
ServerAlias www.leadzilla.ai
<Directory /var/www/leadzilla.ai>
Options Indexes FollowSymLinks
AllowOverride all
Order Deny,Allow
Allow from all
#Deny from all
#Allow from 127.0.0.1
#Allow from ::1
</Directory>
<Directory /var/www/leadzilla.ai/blog>
AllowOverride All
</Directory>
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/leadzilla.ai/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/leadzilla.ai/privkey.pem
</VirtualHost>
</IfModule>
I have Wordpress on https://leadzilla.ai/blog so that has to be taken care of as well.
Here is what I have tried:
RewriteRule ^pricing$ pricing.html [NC]
I added it before the other rewrite rule but that doesn't seem to be working. Any ideas?
[EDIT]
This is solved now. The issue was a Next.js config, not an Apache config.
I put in exportTrailingSlash: true in my module.exports and it worked
I find this to be curious behavior. But if you have a RewriteRule in the <VirtualHost *:443>, then you should also have RewriteEngine On.
Are there any symbolic links in your directory at all. Anything like foo -> foo.html?
Are there any directories like /var/www/html/pricing/ in your directory structure?
Also, remember, that all of your traffic ends up on HTTPS, which means that only the <VirtualHost *:443> is in play. The other virtual host entry only is used long enough to redirect from HTTP to HTTPS. Any rewrite rules for the :80 VirtualHost do not apply on HTTPS.

How configure multiples process on 443 port? Apache - ubuntu

here its my problem:
I have 2 applications configured on a server. React (client) and nodejs backend with their respective domains.
1 - example1.com
2 - example2.com
I have configured both SSL certificates for each of it as well. The problem occurs when you want to start the backend on the same port that the client is running.
Is it possible to run 2 processes on it? How should I do it?
these are my virtual host files:
example1-le.ssl.conf
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerName example1.com
ServerAlias www.example1.com
ServerAdmin info#xample.com
DocumentRoot /var/www/example1
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
SSLCertificateFile /etc/letsencrypt/live/example1.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example2.com/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>
example1.conf
ServerName example1.com
ServerAlias example1.com
ServerAdmin info#example.com.ar
DocumentRoot /var/www/example/build
<Directory "/var/www/example/build">
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
RewriteCond %{SERVER_NAME} =example1.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
And the same for the another one.
When i tried to start the second project i receive : EADDRINUSE: address already in use :::443
Thanks a lot for you help
You can NOT have to 2 different processes bind on the same port. What Olaf Kock suggested works fine and is easy to implement: you install apache on a dedicated server (or on the same machine: there's no difference) and then you configure a reverse proxy (the module is called mod_proxy). There are also other solutions (like haproxy) which require a bit more complex configuration but provide many more configuration options.

How to configure subdomains in bitnami ruby stack

I am using Bitnami ruby stack on Google Cloud which supports php and nodejs at the same time. I have want my express js app which I want to run on main domain. eg www.example.com and I have two other codeigniter app (PHP) which I want them to run on subdomains like one.example.com and the other on domain like two.example.com.
I have followed different Bitnami articles such these 1 2 3 but still I can't have both apps run well together.
I placed all apps inside /opt/bitnami/apps and every app folder has 2 sub folders which are conf and htdocs just as described in article.
Here are contents of conf folder of codeigniter app which I want it to run on subdomain.
/opt/bitnami/apps/one/conf/httpd-app.conf
<Directory /opt/bitnami/apps/one/htdocs/>
Options +FollowSymLinks
AllowOverride All
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3>
Require all granted
</IfVersion>
</Directory>
/opt/bitnami/apps/one/conf/httpd-vhosts.conf
<VirtualHost *:8080>
ServerName one.example.com
ServerAlias www.one.example.com
DocumentRoot "/opt/bitnami/apps/one/htdocs"
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=permanent,L]
Include "/opt/bitnami/apps/one/conf/httpd-app.conf"
</VirtualHost>
<VirtualHost *:8444>
ServerName one.example.com
ServerAlias www.one.example.com
DocumentRoot "/opt/bitnami/apps/one/htdocs"
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1$1 [R=permanent,L]
Include "/opt/bitnami/apps/one/conf/httpd-app.conf"
</VirtualHost>
And conf of nodejs app which I want to run on main domain.
/opt/bitnami/apps/main/conf/httpd-app.conf
ProxyPass / http://127.0.0.1:2000/
ProxyPassReverse / http://127.0.0.1:2000/
/opt/bitnami/apps/nyererefy/conf/httpd-vhosts.conf
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
# redirect www and non-www http routes to https-non-www:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
Include "/opt/bitnami/apps/main/conf/httpd-app.conf"
</VirtualHost>
<VirtualHost *:443>
ServerName example.com
ServerAlias www.example.com
#redirect https-wwww to https-non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
Include "/opt/bitnami/apps/main/conf/httpd-app.conf"
</VirtualHost>
/opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf
# Bitnami applications installed in a prefix URL
Include "/opt/bitnami/apps/phpmyadmin/conf/httpd-prefix.conf"
Include "/opt/bitnami/apps/phppgadmin/conf/httpd-prefix.conf"
Include "/opt/bitnami/apps/letsencrypt/conf/httpd-prefix.conf"
/opt/bitnami/apache2/conf/bitnami/bitnami-apps-vhosts.conf
# Bitnami applications installed in a Virtual Host
Include "/opt/bitnami/apps/one/conf/httpd-vhosts.conf"
Include "/opt/bitnami/apps/main/conf/httpd-vhosts.conf"
/opt/bitnami/apache2/conf/bitnami/bitnami.conf
# Default Virtual Host configuration.
<IfVersion < 2.3 >
NameVirtualHost *:80
NameVirtualHost *:443
</IfVersion>
<VirtualHost _default_:80>
DocumentRoot "/opt/bitnami/apache2/htdocs"
<Directory "/opt/bitnami/apache2/htdocs">
Options Indexes FollowSymLinks
AllowOverride All
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3 >
Require all granted
</IfVersion>
</Directory>
# Error Documents
ErrorDocument 503 /503.html
# Bitnami applications installed with a prefix URL (default)
Include "/opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf"
</VirtualHost>
# Default SSL Virtual Host configuration.
<IfModule !ssl_module>
LoadModule ssl_module modules/mod_ssl.so
</IfModule>
Listen 443
SSLProtocol all -SSLv2 -SSLv3
SSLHonorCipherOrder on
SSLCipherSuite "EECDH+ECDSA+AESGCM EECDH+aRSA+AESGCM EECDH+ECDSA+SHA384 EECDH+ECDSA+SHA256 EECDH+aRSA+SHA384 EECDH+aRSA+SHA256 EECDH !aNULL !eNULL !LOW !3DES !MD5 !EXP !PSK !SRP !DSS !EDH !RC4"
SSLPassPhraseDialog builtin
SSLSessionCache "shmcb:/opt/bitnami/apache2/logs/ssl_scache(512000)"
SSLSessionCacheTimeout 300
<VirtualHost _default_:443>
DocumentRoot "/opt/bitnami/apache2/htdocs"
SSLEngine on
SSLCertificateFile "/opt/bitnami/apache2/conf/server.crt"
SSLCertificateKeyFile "/opt/bitnami/apache2/conf/server.key"
<Directory "/opt/bitnami/apache2/htdocs">
Options Indexes FollowSymLinks
AllowOverride All
<IfVersion < 2.3 >
Order allow,deny
Allow from all
</IfVersion>
<IfVersion >= 2.3 >
Require all granted
</IfVersion>
</Directory>
# Error Documents
ErrorDocument 503 /503.html
# Bitnami applications installed with a prefix URL (default)
Include "/opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf"
</VirtualHost>
# Bitnami applications that uses virtual host configuration
Include "/opt/bitnami/apache2/conf/bitnami/bitnami-apps-vhosts.conf"
# Status
ExtendedStatus on
<VirtualHost _default_:80>
ServerName local-stackdriver-agent.stackdriver.com
<Location /server-status>
SetHandler server-status
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>
</VirtualHost>
How to configure them to run together?
Bitnami developer here!
You need to configure all your applications to use Apache virtual hosts.
First, you need to change the default root application to be configured with virtual host instead of using the Apache prefix.
This is done through the files /opt/bitnami/apache2/conf/bitnami/bitnami-apps-prefix.conf and /opt/bitnami/apache2/conf/bitnami/bitnami-apps-vhosts.conf.
Second, you need to configure your codeigniter applications as virtual hosts too. This guide may be helpful.
https://docs.bitnami.com/general/infrastructure/lapp/configuration/configure-custom-application/
Also, if you have any issues feel free to open a case in the Bitnami community forum.

Comfiguring Apache for socket.io and SSL / WSS

As the title suggests, I'm trying to get Apache and Socket.io (node.js) to play nicely, especially on SSL. Currently, the client app at https://www.example.com uses Socket.io over SSL to connect to the server at [SSL protocol]://socket.example.com/socket.io/?query_stuff*. The connection to wss:// always fails, so Socket.io degrades to https://, which works fine. But I would like to take advantage of the websocket protocol and not rely on polling over http(s).
Linux & Apache
Server version: Apache/2.4.7 (Ubuntu)
Relevant mods: mod_proxy, mod_proxy_http, mod_proxy_wstunnel, mod_rewrite, mod_ssl
Iptables: I have opened ports 80, 443, and 9000.
VirtualHost:
I created a virtualhost on *:443 called socket.example.com. It's intended purpose is to reverse proxy [wss,https]://socket.example.com/ to point to the socket.io server running at http://localhost:9000/. Here it is, with extraneous bits removed:
<VirtualHost *:443>
ServerName socket.example.com
DocumentRoot /var/www/example/socket.io/
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/socket.io [NC]
RewriteCond %{QUERY_STRING} transport=websocket [NC]
RewriteRule /(.*) wss://localhost:9000/$1 [P,L]
## I have also tried the following RewriteRules: ##
# RewriteRule /(.*) http://localhost:9000/$1 [P,L]
# RewriteRule /(.*) http://localhost:9000/socket.io/$1 [P,L]
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/www.example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/keys/0001_key-certbot.pem
SSLCACertificateFile /etc/letsencrypt/ca-bundle.pem
SSLProxyEngine On
ProxyRequests Off
ProxyPreserveHost On
ProxyVia Full
ProxyPass / http://localhost:9000/
ProxyPassReverse / http://localhost:9000/
</VirtualHost>
Success using PHP websockets over SSL
Before switching to node.js for my websocket server, I used the above Apache VirtualHost to successfully route wss://socket.example.com/ws_daemon.php to ws://localhost:9000/ws_daemon.php. In this scenario I 1. removed the rewrite rules and 2. changed the ProxyPass settings to:
ProxyPass / ws://localhost:9000/
ProxyPassReverse / ws://localhost:9000/
But the same logic does not seem to carry over to socket.io.
At this point I've run out of ideas. Any help would be greatly appreciated!
If you are using the <Location> block, you should add the following lines to it:
RewriteEngine on
RewriteCond %{HTTP:UPGRADE} ^WebSocket$ [NC]
RewriteCond %{HTTP:CONNECTION} Upgrade$ [NC]
RewriteRule /socket.io/(.*) ws://localhost:3000/socket.io/$1 [P]

Issue with loadbalancing on apache

Please help with my task.
I'm must to install apache which will be balance connections by nodes (P.S. there is JIRA app nodes, if this is important).
I find how to do this, and once i have do this, and all works correctly. But in this time i put the same part of code in the httpd.conf
Header add Set-Cookie "ROUTEID=.%{BALANCER_WORKER_ROUTE}e; path=/" env=BALANCER_ROUTE_CHANGED
NameVirtualHost *:443
<VirtualHost *:443>
ProxyRequests off
ServerName jira-host
SSLEngine on
SSLCertificateFile "/etc/httpd/certs/jira-host.crt"
SSLCertificateKeyFile "/etc/httpd/certs/jira-host.key"
ProxyTimeout 900
Timeout 900
SSLSessionCacheTimeout 1800
<Proxy balancer://jiracluster>
BalancerMember http://jira-host1:8080 route=node1
BalancerMember http://jira-host2:8080 route=node2
Order Deny,Allow
Deny from none
Allow from all
ProxySet stickysession=ROUTEID
</Proxy>
<Location /balancer-manager>
SetHandler balancer-manager
Order deny,allow
Allow from all
</Location>
ProxyPass /balancer-manager !
ProxyPass / balancer://jiracluster/
ProxyPreserveHost on
</VirtualHost>
<VirtualHost *:80>
ServerName jira-host
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://jira-host [R,L]
</VirtualHost>
And after restart apache get the standart apache hello page, instead balancing.
What i do wrong? please help.
Thx.
Please ensure that you check all your configuration files.
It would seem that another file is still routing traffic to the default apache file.
Please check all your port 80/443 config files.

Resources