Wednesday, February 7, 2024

[SOLVED] Apache reverse proxy to nodejs server on CentOS 7 (WHM)

Issue

I'm trying to setup my site on the server. I've uploaded it and it's currently running on the server but the problem comes when I try to setup a reverse proxy on the domain so that I can access the site. I had followed the WHM documentation on how to do a reverse proxy and it worked but it also redirected the subdomains. Below is the tutorial i followed for modifying an individual virtual host.

https://docs.cpanel.net/ea4/apache/modify-apache-virtual-hosts-with-include-files/

In short, I'd like to be able to access my site, example.com, without affecting my subdomains, webmail.example.com. Below is my conf file

   ServerName example.com
    ServerAlias www.example.com

    ProxyRequests Off
    <Proxy *>
       Require all granted
    </Proxy>

   ProxyPass / http://127.0.0.1:3000/
   ProxyPassReverse / http://127.0.0.1:3000/

And below is the default setting for the same virtual host I'm trying to change located in the httpd.conf file

<VirtualHost 196.41.123.76:80>
 ServerName example.com
    ServerAlias mail.example.com www.example.com
  DocumentRoot /home/gusqooqw/public_html
  ServerAdmin [email protected]
  UseCanonicalName Off

  ## User gusqooqw # Needed for Cpanel::ApacheConf
  <IfModule userdir_module>
    <IfModule !mpm_itk.c>
      <IfModule !ruid2_module>
        <IfModule !mod_passenger.c>
          UserDir enabled gusqooqw
        </IfModule>
      </IfModule>
    </IfModule>
  </IfModule>

  # Enable backwards compatible Server Side Include expression parser for Apache versions >= 2.4.
  # To selectively use the newer Apache 2.4 expression parser, disable SSILegacyExprParser in
  # the user's .htaccess file.  For more information, please read:
  #    http://httpd.apache.org/docs/2.4/mod/mod_include.html#ssilegacyexprparser
  <IfModule include_module>
    <Directory "/home/gusqooqw/public_html">
      SSILegacyExprParser On
    </Directory>
  </IfModule>



  <IfModule suphp_module>
    suPHP_UserGroup gusqooqw gusqooqw
  </IfModule>
  <IfModule suexec_module>
    <IfModule !mod_ruid2.c>
      SuexecUserGroup gusqooqw gusqooqw
    </IfModule>
  </IfModule>
  <IfModule ruid2_module>
    RMode config
    RUidGid gusqooqw gusqooqw
  </IfModule>
  <IfModule mpm_itk.c>
    # For more information on MPM ITK, please read:
    #   http://mpm-itk.sesse.net/
    AssignUserID gusqooqw gusqooqw
  </IfModule>
  <IfModule mod_passenger.c>
    PassengerUser gusqooqw
    PassengerGroup gusqooqw
  </IfModule>

  <IfModule alias_module>
    ScriptAlias /cgi-bin/ /home/gusqooqw/public_html/cgi-bin/
  </IfModule>


    # Global DCV Rewrite Exclude
    <IfModule rewrite_module>
        RewriteOptions Inherit
    </IfModule>






  Include "/etc/apache2/conf.d/userdata/std/2_4/gusqooqw/example.com/*.conf"


  # To customize this VirtualHost use an include file at the following location
  # Include "/etc/apache2/conf.d/userdata/std/2_4/gusqooqw/example.com/*.conf"
<VirtualHost>

And below is the default webmail virtual host found in httpd.conf

<VirtualHost 196.41.123.76:443>
  ServerName mtsd.co.za
  ServerAlias mail.mtsd.co.za www.mtsd.co.za cpcalendars.mtsd.co.za webdisk.mtsd.co.za webmail.mtsd.co.za cpcontacts.mtsd.co.za cpanel.mtsd.co.za
  DocumentRoot /home/gusqooqw/public_html
  ServerAdmin [email protected]
  UseCanonicalName Off

  ## User gusqooqw # Needed for Cpanel::ApacheConf
  <IfModule userdir_module>
    <IfModule !mpm_itk.c>
      <IfModule !ruid2_module>
        <IfModule !mod_passenger.c>
          UserDir enabled gusqooqw
        </IfModule>
      </IfModule>
    </IfModule>
  </IfModule>

  # Enable backwards compatible Server Side Include expression parser for Apache versions >= 2.4.
  # To selectively use the newer Apache 2.4 expression parser, disable SSILegacyExprParser in
  # the user's .htaccess file.  For more information, please read:
  #    http://httpd.apache.org/docs/2.4/mod/mod_include.html#ssilegacyexprparser
  <IfModule mod_include.c>
    <Directory "/home/gusqooqw/public_html">
      SSILegacyExprParser On
    </Directory>
  </IfModule>


  <Proxymatch ^https?://127\.0\.0\.1:(2082|2083|2077|2078|2079|2080|2086|2087|2095|2096)/>
       <IfModule security2_module>
          SecRuleEngine Off
       </IfModule>
  </Proxymatch>
  IfModule mod_suphp.c>
    suPHP_UserGroup gusqooqw gusqooqw
  </IfModule>
  <IfModule suexec_module>
    <IfModule !mod_ruid2.c>
      SuexecUserGroup gusqooqw gusqooqw
    </IfModule>
  </IfModule>
  <IfModule ruid2_module>
    RMode config
    RUidGid gusqooqw gusqooqw
  </IfModule>
  <IfModule mpm_itk.c>
    # For more information on MPM ITK, please read:
    #   http://mpm-itk.sesse.net/
    AssignUserID gusqooqw gusqooqw
  </IfModule>
  <IfModule mod_passenger.c>
    PassengerUser gusqooqw
    PassengerGroup gusqooqw
  </IfModule>

  <IfModule alias_module>
    ScriptAlias /cgi-bin/ /home/gusqooqw/public_html/cgi-bin/
  </IfModule>
  <IfModule ssl_module>
    SSLEngine on

    SSLCertificateFile /var/cpanel/ssl/apache_tls/mtsd.co.za/combined

    SetEnvIf User-Agent ".*MSIE.*" nokeepalive ssl-unclean-shutdown
    <Directory "/home/gusqooqw/public_html/cgi-bin">
      SSLOptions +StdEnvVars
    </Directory>
     </IfModule>





    Include "/etc/apache2/conf.d/userdata/ssl/2_4/gusqooqw/mtsd.co.za/*.conf"




  # To customize this VirtualHost use an include file at the following location
  # Include "/etc/apache2/conf.d/userdata/ssl/2_4/gusqooqw/mtsd.co.za/*.conf"

    <IfModule headers_module>
    RequestHeader set X-HTTPS 1
    </IfModule>

    RewriteEngine On
            RewriteCond %{HTTP_HOST} =cpanel.mtsd.co.za [OR]
            RewriteCond %{HTTP_HOST} =cpanel.mtsd.co.za:443
        RewriteCond %{HTTP:Upgrade} !websocket   [nocase]

        RewriteRule ^/(.*) /___proxy_subdomain_cpanel/$1 [PT]
                ProxyPass "/___proxy_subdomain_cpanel" "http://127.0.0.1:2082" max=1 retry=0
            RewriteCond %{HTTP_HOST} =cpcalendars.mtsd.co.za [OR]
            RewriteCond %{HTTP_HOST} =cpcalendars.mtsd.co.za:443
        RewriteCond %{HTTP:Upgrade} !websocket   [nocase]

        RewriteRule ^/(.*) /___proxy_subdomain_cpcalendars/$1 [PT]
                ProxyPass "/___proxy_subdomain_cpcalendars" "http://127.0.0.1:2079" max=1 retry=0
            RewriteCond %{HTTP_HOST} =cpcontacts.mtsd.co.za [OR]
            RewriteCond %{HTTP_HOST} =cpcontacts.mtsd.co.za:443
        RewriteCond %{HTTP:Upgrade} !websocket   [nocase]
RewriteRule ^/(.*) /___proxy_subdomain_cpcontacts/$1 [PT]
                ProxyPass "/___proxy_subdomain_cpcontacts" "http://127.0.0.1:2079" max=1 retry=0
            RewriteCond %{HTTP_HOST} =webdisk.mtsd.co.za [OR]
            RewriteCond %{HTTP_HOST} =webdisk.mtsd.co.za:443
        RewriteCond %{HTTP:Upgrade} !websocket   [nocase]

        RewriteRule ^/(.*) /___proxy_subdomain_webdisk/$1 [PT]
                ProxyPass "/___proxy_subdomain_webdisk" "http://127.0.0.1:2077" max=1 retry=0
            RewriteCond %{HTTP_HOST} =webmail.mtsd.co.za [OR]
            RewriteCond %{HTTP_HOST} =webmail.mtsd.co.za:443
        RewriteCond %{HTTP:Upgrade} !websocket   [nocase]

        RewriteRule ^/(.*) /___proxy_subdomain_webmail/$1 [PT]
                ProxyPass "/___proxy_subdomain_webmail" "http://127.0.0.1:2095" max=1 retry=0

            RewriteCond %{HTTP:Upgrade} websocket   [nocase]
                RewriteCond %{HTTP_HOST} =cpanel.mtsd.co.za [OR]
                RewriteCond %{HTTP_HOST} =cpanel.mtsd.co.za:443

            RewriteRule ^/(.*) /___proxy_subdomain_ws_cpanel/$1 [PT]
            RewriteCond %{HTTP:Upgrade} websocket   [nocase]
                RewriteCond %{HTTP_HOST} =webmail.mtsd.co.za [OR]
                RewriteCond %{HTTP_HOST} =webmail.mtsd.co.za:443

            RewriteRule ^/(.*) /___proxy_subdomain_ws_webmail/$1 [PT]
</VirtualHost>

Solution

So after having contacted the cpanel support, they could not answer why the method I used above wasnt working and they gave an alternative solution. I ended up using an interface called Application manager on Cpanel. It's the easiest way of installing a nodejs application on a cpanel server. Below is the documentation on how to use it to run yourr application

https://docs.cpanel.net/knowledge-base/web-services/how-to-install-a-node.js-application/

Hope this helps someone



Answered By - James
Answer Checked By - David Marino (WPSolving Volunteer)