Home > Enterprise >  502: Proxy Error from Express app on Ubuntu 18.04 & Apache2
502: Proxy Error from Express app on Ubuntu 18.04 & Apache2

Time:09-24

I'm trying to create a simple Express & Node app on an Ubuntu 18.04 that should act like an API. Problem is, I only get 502 error whenever trying to access the url. Below is the code used to configure Apache and the express app itself, as well as entries from the log.

App.js

const express = require('express')
const http = require('http')
const cors = require('cors')

const app = express()
app.use(cors())
app.enable('trust proxy')

const port = 3500

app.get('/', function (req, res) {
        console.log('asdfasdf')
})

http.createServer(app).listen(port, () => {
        console.log('Running http server on port %s', port);
});

Apache config (non-SSL):

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/node2.domain.io
        ServerName node2.domain.io
        ServerAlias www.node2.domain.io

   ProxyRequests Off
   ProxyPreserveHost On
   ProxyVia Full

   <Proxy *>
      Require all granted
   </Proxy>

   <Location />
      ProxyPass https://127.0.0.1:3500/
      ProxyPassReverse https://127.0.0.1:3500/
   </Location>

        <Directory /var/www/node2.domain.io>
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
        </Directory>

        RewriteEngine on
        RewriteCond %{SERVER_NAME} =node2.domain.io [OR]
        RewriteCond %{SERVER_NAME} =www.node2.domain.io
        RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

Apache config (SSL):

</VirtualHost>
</IfModule>

<IfModule mod_ssl.c>
<VirtualHost *:443>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/node2.domain.io
        ServerName node2.domain.io
        ServerAlias www.node2.domain.io

 SSLProxyEngine On
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off

   ProxyRequests Off
   ProxyPreserveHost On
   ProxyVia Full

   <Proxy *>
      Require all granted
   </Proxy>

   <Location />
      ProxyPass https://127.0.0.1:3500/
      ProxyPassReverse https://127.0.0.1:3500/
   </Location>

        <Directory /var/www/node2.domain.io>
                Options Indexes FollowSymLinks
                AllowOverride All
                Require all granted
        </Directory>

SSLCertificateFile /etc/letsencrypt/live/node2.domain.io/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/node2.domain.io/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
</VirtualHost>
</IfModule>

Log entries:

AH01102: error reading status line from remote server 127.0.0.1:3500, referer: https://node2.domain.io/

^ this one x 200

As a side note, when I'm starting the frontend React app, it works as it should.

CodePudding user response:

In your code, you never return a response to the request :

app.get('/', function (req, res) {
        console.log('asdfasdf')
})

When we call /, you tell express to do console.log('asdfasdf') and nothing else.

return something like this and you will get your response :

app.get('/', function (req, res) {
        console.log('asdfasdf')
        res.send('ok') // <-- add this line
})

In addition, if you want to create an API, you probably want to return JSON response, so you can use the res.json() method of Express :

app.get('/', function (req, res) {
        console.log('asdfasdf')
        res.json({
            name: 'toto',
            foo: 'bar'
        })
})

CodePudding user response:

I've replaced http.createServer(/* .. */) with app.listen(port) and it works now.

  • Related