502 from Apache over HTTPS with reverse proxy to Node server on different port

17,190

Your nodejs expects HTTP on it's port, as is evident by your working test:

If I visit http://api.example.com:3030/ I can load a 200 response with no problem

But you configured your Apache to access it via https

ProxyPass / https://api.example.com:3030/
ProxyPassReverse / https://api.example.com:3030/

Change your Apache configuration to use http and it will work.

ProxyPass / http://api.example.com:3030/
ProxyPassReverse / http://api.example.com:3030/

An additional note:
It is common practice not to make the ports of application servers public. You should configure nodejs to only listen on your loopback device (127.0.0.1) and then change your Proxy directives to connect to localhost:3030 instead of api.example.com.

Share:
17,190

Related videos on Youtube

Robert
Author by

Robert

Updated on September 18, 2022

Comments

  • Robert
    Robert over 1 year

    I have a node application running on port 3030 for an API which I want to access over HTTPS.

    If I visit http://api.example.com:3030/*** I can load a 200 response with no problem so the node application is definitely running, however if I visit https://api.example.com/**** I get the following error in the browser:

    Proxy Error
    The proxy server received an invalid response from an upstream server.
    The proxy server could not handle the request GET /api/posts.
    

    and the following in the apache error log:

    [Sun Jun 03 15:30:47.942229 2018] [proxy_http:error] [pid 30884] (103)Software caused connection abort: [client xx.xx.xx.xx:xxxxx] AH01102: error reading status line from remote server api.example.com:3030
    [Sun Jun 03 15:30:47.942414 2018] [proxy:error] [pid 30884] [client xx.xx.xx.xx:xxxxx] AH00898: Error reading from remote server returned by /***
    

    This is my Apache config:

    <IfModule mod_ssl.c>
    <VirtualHost *:443>
            ServerName www.api.example.com
            ServerAlias api.example.com
            ServerAdmin webmaster@localhost
            DocumentRoot /var/www/html/xxxxxx/
    
            ErrorLog ${APACHE_LOG_DIR}/error.log
            CustomLog ${APACHE_LOG_DIR}/access.log combined
    
    <Directory /var/www/html/xxxxxx/>
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
        </Directory>
    RewriteEngine on
    
    SetEnv proxy-initial-not-pooled 1
    SSLEngine On
    SSLProxyEngine on
    SSLCertificateFile /etc/letsencrypt/live/example.com/cert.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateChainFile /etc/letsencrypt/live/example.com/chain.pem
    ProxyPreserveHost On
    ProxyPass / https://api.example.com:3030/
    ProxyPassReverse / https://api.example.com:3030/
    
    </VirtualHost>
    </IfModule>
    

    Versions of Apache and Node:

    • node 8.11.2
    • Apache 2.4.7

    Any support is greatly appreciated, thanks.