Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pipe all requests to a particular port - node js application

I am trying to run node js and apache in a same server for that

I am trying to pipe all requests to a particular port (eg: example.com:80 to example.com:3000).

For that, I changed httpd.conf file located in "/etc/httpd/conf/httpd.conf"

Added these lines st the end

<VirtualHost *:80>
    ServerName mysite.com
    ProxyPreserveHost on
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
</VirtualHost>

and restarted using sudo service httpd restart

but nothing changed.

There are more 2 httpd.conf files available ->

/usr/local/apache/conf/httpd.conf
/etc/httpd/conf/httpd.conf
/etc/apache2/conf/httpd.conf

at the end of /etc/apache2/conf/httpd.conf file I saw this:

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#
#   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#   DO NOT EDIT. AUTOMATICALLY GENERATED.  USE INCLUDE FILES IF YOU NEED TO MAKE A CHANGE
#   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

I cannot understand the line USE INCLUDE FILES IF YOU NEED TO MAKE A CHANGE.

What should I do????

like image 818
Hello World Avatar asked Sep 11 '18 14:09

Hello World


People also ask

How NodeJS handles multiple requests with single thread?

Multiple clients make multiple requests to the NodeJS server. NodeJS receives these requests and places them into the EventQueue . NodeJS server has an internal component referred to as the EventLoop which is an infinite loop that receives requests and processes them. This EventLoop is single threaded.

How many requests per second can NodeJS handle?

However, considering that a “Hello World” Node. js server is easily capable of thirty thousand requests per second on that machine that produced these results, 23 requests per second with an average latency exceeding 3 seconds is dismal.

How does NodeJS handle multiple request?

How NodeJS handle multiple client requests? NodeJS receives multiple client requests and places them into EventQueue. NodeJS is built with the concept of event-driven architecture. NodeJS has its own EventLoop which is an infinite loop that receives requests and processes them.


2 Answers

I haven't used Apache2 in a while as I would usually use Nginx to proxy my Node.JS applications.

You should copy the default site config from /etc/apache2/sites-available/000-default.conf. (Command: sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/mysite.com.conf).

Then place the configuration that you have there into the new file. Edit what needs editing then enable the new site configuration by running sudo a2ensite mysite.com.conf. Then restart the apache2 process by running sudo service apache2 restart.

You should now be able to test it and it should work if your configuration syntax is correct.

like image 51
Nicholis Avatar answered Oct 13 '22 01:10

Nicholis


I personally prefer Nginx over Apache2 but you can use any of them.

for apache2 you should enable proxy and proxy_http modules:

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo service apache2 restart

then you should add a config file for apache usually named /etc/apache2/sites-available/example.com.conf: (you can remove Directory part if you don't need it)

<VirtualHost *:80>
ServerName example.com

   ProxyRequests Off
   ProxyPreserveHost On
   ProxyVia Full
   <Proxy *>
      Require all granted
   </Proxy>

   <Location / >
      ProxyPass http://127.0.0.1:3000
      ProxyPassReverse http://127.0.0.1:3000
   </Location>

    <Directory "/var/www/example.com/html">
    AllowOverride All
    </Directory>
</VirtualHost>

then enable the config and restart apache2 with:

sudo a2ensite example.com
sudo services apache2 restart

for Nginx config you should add these lines to /etc/nginx/site-available/your-app.conf:

upstream app {
server 0.0.0.0:3000;
}

server {
    listen 80;
    listen [::]:80;
    server_name example.com;


    location / {
        proxy_pass http://app/;
        proxy_set_header Host $http_host;
    }
    access_log /var/log/nginx/app-access.log;
    error_log /var/log/nginx/app-error.log info;
}

then you should run:

sudo ln -s /etc/nginx/sites-available/your-app.conf /etc/nginx/sites-enabled/

and then:

# remove default config symlink from sites-enabled dir
rm /etc/nginx/sites-enabled/default
# test if config is OK:
sudo nginx -t
# restart the nginx:
sudo systemctl restart nginx

P.S: I used the apache conf example from this link

like image 40
Amir Masud Zare Bidaki Avatar answered Oct 13 '22 00:10

Amir Masud Zare Bidaki