skip to Main Content

Using forever to forever run the node server on the virtual machine, I am unable to get the app to run without explicitly adding the port in the url like so: URL.com:8080
If I don’t use the port in the URL, I do load up the file structure of the application.

Steps to reproduce: I have a create-react-app application.On the virtual server I run ‘npm run build’ to make sure I have a build to serve. I then run forever start on the root of the application.

The code below should give all the necessary details. I can provide more if you need.

I have spent so much time tweaking the .conf file to try different configurations but I can’t seem to get it. I am using it and successfully hosting two static html sites but not this node application.

Package.json:

...
"main":"server/index.js",
"proxy":"http://localhost:8080"
...

Apache url.conf:

<VirtualHost *:80>
    ServerName URL.com
    ServerAlias URL.com:8080/
    DocumentRoot /var/www/nameOfApp/
<Directory />
    Options -Indexes +FollowSymLinks
    AllowOverride All
</Directory>
<Directory /var/www/nameOfApp/public>
    Options +Indexes +FollowSymLinks +MultiViews
    AllowOverride All
</Directory>
</VirtualHost>

Node server file using express:

app.use(express.static(`${__dirname}/../build`));

I’ve also made sure I have the modules enabled to allow for proxying. So I think essentially, what I need is to request this site and not need the :8080 at the end.

2

Answers


  1. Chosen as BEST ANSWER

    The configuration that eventually worked was as simple as this:

    <VirtualHost *:80>
        ServerName yourdomain.com
        ProxyPreserveHost on
        ProxyPass / http://localhost:8080/
    </VirtualHost>
    

    Looks like I was over complicating it trying to create a complicated proxy but the solution was very simply adding this to the config for the node application and then running sudo systemctl restart apache2 and everything worked beautifully.


  2. Apache and NodeJS are 2 different and separate application.
    You interact with them by sending request to the port that they are listening to. In your case here,

    Apache is listening at port 80

    Your NodeJS application is listening at port 8080

    So all request to port 80 will be handled by Apache, and since you do not has an index.html, Apache will default to just list out the files and directory (Options Indexes). Up until this point, your node application do not know anything about your request.

    So what you need to do is define some endpoint, say url.com/node, and tell Apache to forward all request of this endpoint to port 8080 and let your node application to do the job.

    How to do this?

    http://httpd.apache.org/docs/2.2/mod/mod_proxy.html#proxypass

    Best practices when running Node.js with port 80 (Ubuntu / Linode)

    Node.js + Nginx – What now?

    Hope this points you to the right direction.

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