Apache2 Port Rerouting

I wanted to run a server written in NodeJS and have it accessible on localhost port 80 without having to run the server as root (because ports below 1024 iirc are reserved). You can get the server to listen on a different port such as 8080. You need to ask Apache to send anything it receives on 80 to 8080 and also anything going out of 8080 to go out of 80.

If you look online for an explanation for how to do this, often people will show code without an explanation of where it goes and some will reference a httpd.conf file, which didn't even exist on my machine.

I'm running Ubuntu 16.04. Things may be different on your machine.

1. Add the module that allows rerouting

sudo a2enmod proxy_http

2. Set up the config

/etc/apache2/sites-available has a number of config files for websites, including 000-default.conf, which is the default.

Apache2 only looks at the config files in /etc/apache2/sites-enabled. This folder should contain symlinks to sites-available.

You can either edit the default because the edit is simple or you can create a copy of it, edit that, and put a symlink to it in sites-available. The following command can be used to make the symlink: sudo a2ensite sitename, while a2dissite can be used to disable it.

Apache will have to be restarted to update the config: sudo service apache2 restart.

The following lines should be added between the <VirtualHost *:80> tags:

ServerName localhost
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/

From what I understand, the server name should be the same as the domain written in the proxy lines.

You can edit /etc/hosts/ to add more valid server names. More info [x].

The forward slash in the proxy lines means that all traffic to localhost will be rerouted. If you wrote /api, then only traffic requesting that path goes to a different port. Therefore you can for example have a website served by apache2 in /var/www/html (or wherever) and a NodeJS server controlling the same external website.

I think you can wrap the 2 proxy lines in a <Location /some/file/path> tag in order to make the 'proxy' paths relative to the 'location' path.



More Info: [x]

Comments

Popular posts from this blog

Fix Controller Input when a Game Doesn't Use Steam Input

Fix Inability to Move Files to Trash in VSC

VSC Vanishing Go Imports