Search code examples
node.jsapacheserverhttpd.confproxypass

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????


Solution

  • 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