skip to Main Content

I have a GoDaddy VPS managed server. I’ve installed Node/NPM, SSL certs, etc. successfully, but running into a couple issues with .htaccess. I can force http to https with the following code in .htaccess:

RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} !^/[0-9]+..+.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/.well-known/pki-validation/[A-F0-9]{32}.txt(?: Comodo DCV)?$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

I can also bypass having to do an Apache reverse proxy so that the server/domain is using NodeJS instead of Apache by adding the following to .htaccess:

RewriteEngine on
RewriteRule  (.*)  http://localhost:3000/$1  [P,L] 

The issue I’m having is getting both to work at the same time. Force SSL AND point to NodeJS for the server.

I’m not sure if .htaccess is the best route to go for both goals.

2

Answers


  1. You should not use .htaccess at all if you have access to Apache configuration files (detailed explanation here).

    In your http (:80) Virtual host configuration you can put your https forcing rules, and in your https (:443) Virtual host you can put your reverse proxy rules. Also, using ProxyPass directive instead of RewriteRule with [P] flag might give some performance benefit:

    ProxyPass         / http://localhost:3000/
    ProxyPassReverse  / http://localhost:3000/
    
    Login or Signup to reply.
  2. If you have access to Apache .conf files, try to create virtual host with following code:

    <VirtualHost *:80>
        ServerName www.example.com
        Redirect permanent / https://www.example.com/
    </VirtualHost>
    
    
    <VirtualHost *:443>
        ServerName www.example.com
        SSLEngine on
        SSLCertificateFile /etc/path/to/fullchain.pem
        SSLCertificateKeyFile /etc/path/to/privkey.pem
    
        ProxyRequests off
        SSLProxyEngine on
    
        ErrorLog /var/log/nodejs/errorLog443.log
        TransferLog /var/log/nodejs/transferLog443.log
    
        <Location />
            ProxyPass http://localhost:3000/
    
            Order deny,allow
            Deny from all
            Allow from all
        </Location>
    
    </VirtualHost>
    

    Then enable just created apache virtual host and don’t forget install all required sub modules:

    sudo a2enmod ssl
    sudo a2enmod proxy
    sudo a2enmod proxy_balancer
    sudo a2enmod proxy_http
    
    sudo a2ensite example.apache.host.conf
    
    sudo service apache2 restart
    

    If you need to create a new apache .conf file, please follow this few steps:

    1. Navigate to apache vhost default directory /etc/apache2/sites-avaliable and create new virtual host configuration file with example code.

    2. Enable this configuration file use linux symlink or use apache tool a2ensite.

    3. Restart or reload apache service.

    Example:

    cd /etc/apache2/sites-avaliable
    vim new-virtual-host.conf
    

    Put example configuration described top to this file.

    Create symlink:

    cd /etc/apache2/sites-enabled/
    ln -s ../sites-available/new-virtual-host.conf new-virtual-host.conf
    

    or

    sudo a2ensite new-virtual-host.conf
    

    Finaly reload new configuration by restarting apache service:

    sudo service apache2 restart
    

    The best way to manage/create web host ssl certificates is use Let’s Encrypt service. Please follow guide here

    If you have already valid certificate, you can of course convert it to pem format by folowing this gude.

    Updated for Centos OS:

    For Centos OS just open apache2 config file at location
    /etc/httpd/conf/httpd.conf, scroll down to the very bottom of the
    document to the section called Virtual Hosts. There you can edit/add
    virtual host section with appropirate configuration you need.

    For addition information how to configure Apache service on Centos OS please read guide How To Set Up Apache Virtual Hosts on CentOS 6

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search