0

I'm setting up a website for my company and I can't find any ways to make a custom webhost for what I'm trying to do. My website has 4 subdomains (including root), and each has its own folder in the host files. These subdomains and their host folders are:

  • triavor.com - /triavor/
  • gamestudio.triavor.com - /gamestudio/
  • workbench.triavor.com - /workbench/
  • partner.triavor.com - /partner/

What I'm trying to do is redirect requests to each individual subdomain and exclude the folder name in the URL. For example, instead of triavor.com/triavor/home.html, it's triavor.com/home.html.

Extra information are:

  • My server is running on Ubuntu Server
  • I'm using CloudFlare as registrar
  • My main script languages are HTML, CSS and JS
  • I have many images and a few videos on the website

I'm sorry if I have used the wrong terminology or I have done something wrong. This is my own company, and I haven't done much backend web development before, only a simple node.js script. If a premade option is available, such as Apache, I would prefer to make my own script, but if what I'm trying to do is too complicated, I can use a premade option. Thanks,

  • Lachlan
4
  • What software are you currently using to host the websites on that Ubuntu Server? Are the websites purely static-file-based or is there any dynamic 'app' backend? (For static files, this is a problem solved 30 years ago so there are good premade options and doing it custom would be reinventing the wheel, unless there is a definite need for it.) Commented Jul 24 at 6:22
  • For now, the only dynamic code is generating a price based off a series of inputs using math in JavaScript, if I understand correctly. Commented Jul 24 at 9:00
  • Is node directly accepting the HTTP connections, without Apache or nginx or something similar in between? If yes, please edit the nodejs config into your question where you defined which site is using which subdomain. Commented Jul 24 at 9:16
  • @GeraldSchneider yes, the original node.js script was directly accepting HTTP connections, if I remember correctly. I can try find the original script and edit it into the question. Commented Jul 24 at 23:12

1 Answer 1

1

The usual web-server term for this is "virtual hosts". It has been present in every HTTP server since probably 1996, so based on that alone I would suggest that existing solutions are a good enough choice and you shouldn't be trying to literally reinvent the wheel just for the sake of avoiding Apache (especially if you have zero experience in this area).

With virtual hosts as they are implemented in major web servers, the path to the host files is configured per-domain rather than globally per-server. There are no "redirects" as the server looks in a different location from the very beginning.

For example, Apache or Nginx (very nearly the same layout) might be configured like this:

# The global setting is only used for requests that didn't match any VHost. DocumentRoot /var/www/default # Normally you would have <VirtualHost> blocks for every recognized domain # (one for HTTP on *:80 and of course an identical one for HTTPS on *:443). <VirtualHost *:80> ServerName gamestudio.example.com DocumentRoot /var/www/gamestudio </VirtualHost> <VirtualHost *:80> ServerName workbench.example.com DocumentRoot /var/www/workbench </VirtualHost> 

If you have Nginx, the same is done using multiple server { ... } blocks. With Caddy, the domain name is literally the main "parameter" within the configuration file:

gamestudio.example.com { root * /var/www/gamestudio } workbench.example.com { root * /var/www/workbench } 

Now if you do want to implement it manually in Node regardless – I actually have no idea how one normally creates web servers in Node to begin with, but the web browser specifies the requested domain name in the Host: header. So upon receiving an HTTP request, you would look at its Host header to determine the host/domain name, and instead of a single fixed "path to the host files" you'd have a Map of domains and their respective paths.

For example, (this is the first thing I've written in Node ever):

var http = require('http'); var fs = require('fs'); http.createServer(function (req, res) { console.log(req.headers); let host = req.headers["host"]; if (host == "workbench.example.com") { res.writeHead(200, {'Content-Type': 'text/html'}); res.end(fs.readFileSync('/var/www/workbench/index.html')); } else if (host == "gamestudio.example.com") { res.writeHead(200, {'Content-Type': 'text/html'}); res.end(fs.readFileSync('/var/www/gamestudio/index.html')); } else { res.writeHead(400, {'Content-Type': 'text/plain'}); res.end('Unrecognized vhost'); } }).listen(8080); 

That said, however, even with webapp backends it is more common to let some external HTTP proxy handle this. Instead of having one big backend process to handle five webapps, one would have e.g. Nginx/Traefik/Haproxy/etc. acting as a proxy to five separate backends – each handling only its own domain.

1
  • Ah okay, that makes more sense than doing it manually. I think the reason why I did it manually in the first place was because I had issues with my original server that prevented me from using nginx or apache. Thank you! Commented Jul 28 at 1:12

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.