Running Apache and Node on the same server

Option 1. Apache in the front, /node/ handled by local Node.

STEP 1: Add this to some .conf file:
ProxyPass /node/ http://localhost:8000/
(e.g. save it in some my_proxy_forward_node_to_8000.conf, then a2enconf it) More notes for this to work: (from ProxyPass documentation)
  • Ensure the mods proxy and proxy_http are enabled (a2enmod).
  • If the first argument ends with a trailing /, the second argument should also end with a trailing /, and vice versa. Otherwise, the resulting requests to the backend may miss some needed slashes and do not deliver the expected results.
  • The ProxyRequests directive should usually be set off when using ProxyPass.
STEP 2: Then run Node locally on port 8000:
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello Apache!\n');
}).listen(8000, '127.0.0.1');

Option 2. TrafficServer in the front, Apache and Node behind it.

STEP 0: Install TrafficServer. (Note: As of 4/2017, this installed version 5.3.x from the repositories, though the real latest version is 7.2.x)
apt-get install trafficserver
IMPORTANT: Edit records.config and add this line (I added in the beginning; I don’t understand why this isn’t the default configuration). If you don’t specify such user_id, it will not agree to run itself at all, because it doesn’t want to run as root.
CONFIG proxy.config.admin.user_id STRING trafficserver
STEP 1: Enable Reverse Proxying (pass incoming traffic to e.g. another port in localhost, preserving “Host:” headers etc.). Edit records.config: (the underlined value here is the only change needed for the default value, as of 4/2017)
CONFIG proxy.config.http.cache.http INT 1
CONFIG proxy.config.reverse_proxy.enabled INT 1
CONFIG proxy.config.url_remap.remap_required INT 1
CONFIG proxy.config.url_remap.pristine_host_hdr INT 1
CONFIG proxy.config.http.server_ports STRING 8080
Edit remap.config:
regex_map http://(.*):8080/ http://localhost:80/
Finally, reread the config files (traffic_line --reread_config or equivalently -x). IMPORTANT: The 8080 and 80 above (in both records.config and remap.config) should be switched if you really want TrafficServer in the front… STEP 2: Log files, monitoring, security: https://docs.trafficserver.apache.org/en/5.3.x/admin/working-log-files.en.html https://docs.trafficserver.apache.org/en/5.3.x/admin/monitoring-traffic.en.html https://docs.trafficserver.apache.org/en/5.3.x/admin/security-options.en.html STEP 3: If you want to disable cache completely: (didn’t check this) Edit record.config:
CONFIG proxy.config.http.cache.http INT 0

(BONUS) STEP: Cache configuration: storage.config. E.g. its default contents were simply a one-liner /var/cache/trafficserver 256M. Changes require restart of trafficserver. It is recommended to use raw devices; see documentation. E.g. how to cache everything (e.g. if you’re serving only static files): By default, Traffic Server will cache an HTTP response only if it contains a Cache-Control or Expires header explicitly specifying how long the item should be stored in the cache. To disable that need for those required headers:
sudo traffic_line --set_var proxy.config.http.cache.required_headers --value 0
sudo traffic_line --reread_config
(BONUS) STEP: Use a tool called Cache Inspector:
sudo traffic_line --set_var proxy.config.http_ui_enabled --value 1
Edit remap.config, add this line at the top of the file:
map http://your_server_ip:8080/inspect http://{cache}
Then restart the trafficserver service.

Leave a Reply

Your email address will not be published. Required fields are marked *