Forum

Port-based virtual hosting on home network?

raspiham
12 August 2017, 12:33
Hi everyone,
I have been trying to teach myself about webservers and networking over the past couple of years. I now have Hiawatha set-up as a home sever on a raspberrypi through my home router. Everything appears to work as it should and I am very pleased with Hiawatha.

I was recently reading about "virtual servers" and "Port-based" virtual servers and I would like to try this with Hiawatha. So my question is will port based virtual servers work with Hiawatha such that I could host 2 or more virtual websites on the same raspberrypi all with the same basic network address of 192.168.1.37 and use different port numbers to reach the different virtual websites.

e.g 192.168.1.37 (default website), 192.168.1.37:998 (a second "virtual" website) and 192.168.1.37:999 for a possible third virtual website.
Thanks for reading this,
raspiham
Nicolas
13 August 2017, 09:48
Hello,

Did you try Virtual Hosts [www.hiawatha-webserver.org] ? It should answer your need to have multiple websites behind the same IP address but without opening multiples ports in your home router :

Which host configuration will Hiawatha be using for a certain request? In this selection process, Hiawatha starts by looking for the hostname in a request. This will be the hostname part of an URL. For example, in the request for this page (http://www.hiawatha-webserver.org/howto/websites) the hostname part is www.hiawatha-webserver.org.

Hiawatha will try to find a matching hostname in each host configuration (both default and virtual). Finally, if no matching hostname is found, the default hostname will be used.
raspiham
13 August 2017, 12:11
Hi Nicolas,
Thankyou for taking the time to respond. I read the section on virtual hosts but it seems to refer to the hosting of websites on the WWW.

My idea was to host multiple websites using a single local IP address but only on my own local network by using multiple ports. Traffic between computers connected on my home network do not seem to have any ports blocked by the router.

I understand that if I wanted to expose my website to the WWW then I would have to open more than one port on my home router using the port based option but thats not what I want to do at the moment. I just want to keep it lcoal for experimentation.

As I understand the only way I could make matching hostnames work as described in the Hiawatha documentation with my local network would be to have some sort of local DNS service or edit the "hosts" files in the individual machines connected to my local network. I would prefer avoid this and test a port based option first.

Perhaps I have missed something and a simpler solution exists?
Kind regards,
raspiham
Hugo Leisink
13 August 2017, 14:06
Hi raspiham,

You could use the BindingID and RequiredBinding settings for that.
Binding {
Port = 80
}

Binding {
Port = 998
BindingID = port_998
}

Binding {
Port = 999
BindingID = port_999
}

VirtualHost {
Hostname = 192.168.1.37
RequiredBinding = port_998
WebsiteRoot = /var/www/website_one
...
}

VirtualHost {
Hostname = 192.168.1.37
RequiredBinding = port_999
WebsiteRoot = /var/www/website_two
...
}

Hostname = 192.168.1.37
WebsiteRoot = /var/www/default
...
raspiham
13 August 2017, 15:15
Hi Hugo,

Many thanks for your reply and for the sample of code.

I edited my hiawatha.config file using the example you gave as a guide and re-started hiawatha. When I enter my raspi server network address on another machine connected through my local router I see my default web page as expected but when I enter the same address with one of the two port numbers appended I still see my default web page. It seems to direct the browser to the default page regardless of the port number. Valid address/port numbers are 192.168.1.35, 192.168.1.35:80, 192.168.1.35:998 and 192.168.1.35:999 Perhaps I have entered something incorrectly in the config file?

If you have the time perhaps you could have a quick look at my current (experimental) cofiguration file?
# Hiawatha main configuration file
#
# GENERAL SETTINGS
#
ServerId = www-data
ConnectionsTotal = 1000
ConnectionsPerIP = 25
SystemLogfile = /var/log/hiawatha/system.log
GarbageLogfile = /var/log/hiawatha/garbage.log
#
#
# BINDING SETTINGS
# A binding is where a client can connect to.
#
Binding {
Port = 80
}

Binding {
Port = 998
BindingID = port_998
}

Binding {
Port = 999
BindingID = port_999
}

#
#
# DEFAULT WEBSITE
#
Hostname = 192.168.1.35
WebsiteRoot = /var/www/hiawatha
StartFile = index.html
AccessLogfile = /var/log/hiawatha/access.log
ErrorLogfile = /var/log/hiawatha/error.log
ErrorHandler = 404:/error_default.html
# VIRTUAL HOSTS
# Use a VirtualHost section to declare the websites you want to host.
#
#Virtual host 1
#
VirtualHost {
Hostname = 192.168.1.35
WebsiteRoot = /var/www/v1
RequiredBinding = port_998
AccessLogfile = /var/log/hiawatha/virtual1_access.log
ErrorLogfile = /var/log/hiawatha/virtual1_error.log
ShowIndex = yes
ErrorHandler = 404:/error_v1.html
}
#
#Virtual host 2
#
VirtualHost {
Hostname = 192.168.1.35
WebsiteRoot = /var/www/v2
RequiredBinding = port_999
AccessLogfile = /var/log/hiawatha/virtual2_access.log
ErrorLogfile = /var/log/hiawatha/virtual2_error.log
ShowIndex = yes
ErrorHandler = 404:/error_v2.html
}
#

I also found a reference to port based virtual hosts in a message from Douglas Hubler (time-stamp 3 February 2012, 10:03) but the sysntax was slightly different and perhaps for an earlier version of Hiawatha?

Kind regards,
raspiham
Hugo Leisink
14 August 2017, 22:15
Change the hostname of your default website to 127.0.0.1. That should work.
raspiham
15 August 2017, 15:35
Hi again Hugo,

My thanks for taking the time to look over my script and for rescuing me :-) Woohoo! It works a treat now. Its exactly what I was looking for, thanks.

I have to add that I have learnt more in the last couple of weeks from my numerous errors than I had ever imagined I would. I hope you dont mind if I pose another question in the future? I have been playing with name-based virtual servers which also works well allbeit with making additions to the "Hosts" files on my local client machines. Great fun!
Kind regards,
raspiham
Hugo Leisink
15 August 2017, 15:43
\o/ That's good to hear! Yes, that's the good part of making mistakes: you learn from it. And of course you can ask more questions in the future. That's what this forum is for!
raspiham
15 August 2017, 16:30
Many thanks Hugo.
Nicolas
16 August 2017, 11:46
Hi raspiham,

Sorry, I thought you wanted to have websites reachable from outside your home network. I also didn't know about the RequireBinding setting (I never thought of doing it that way because I'm the only one reaching my local websites).
Erik S
16 August 2017, 21:12
use a reverse proxy. One machine runs hiawatha and has the virtual hosts configiured to send the incoming request to the correct hiawatha machine on the local network. so one could use http://example.dyndns.org and http://another.example.dyndns.org both
going to the same IP. Your gateway is a reverse proxy (Hiawatha) that gets the incoming request and sends one domain to machine 192.168.0.10 or another machine on 192.168.0.20. Both of these run a HAMP stack and host the websites.
raspiham
19 August 2017, 11:15
Hi again Nicolas,

Thanks for the follow-post and no need to apologise, I probably did not make my intention to make a local Intranet server clear enough in my first posting.
regards,
raspiham
raspiham
19 August 2017, 11:25
Hi Erik S,

Thanks for the suggestion of the reverse proxy. This is something I have read about and still need to "get my head around" but for my current needs a single raspberrypi will sufice. If I understand correctly, in order to implement a reverse-proxy as you describe I would need to add additional servers (and posibly raspis) for each domain?

BTW, what is a HAMP stack? Is this a variation of a LAMP stack? I think I am currently running what might be called a LHMP stack on the raspi :-)

Thanks again for the feedback.
Regards,
raspiham
Erik S
1 September 2017, 14:58
Yes the HAMP is a LAMP and when actually called correctly, would be a LHMP stack! Just Hiawatha not Apache as the web server.
I use 1 machine that all requests come into. This then according to the host/domain name sends the request to another machine on the subnet that is running a LAMP as well that actually is hosting the website. It is like all the mail coming to a large company, which goes to the mail room and then gets sorted and delivered to each department by the mail room people. The company is your router, the mail room is your reverse proxy machine, and the individual domains/hosts run on separate machines which are the departments. There are other methods including the assignment by ports...but I have had far more success and reliability using the reverse proxy method.
raspiham
2 September 2017, 11:08
Hi again Erik S,

All understood regarding HAMP, LAMP, LHMP stacks and the enalogy of the mailroom helped me understand the concepts of the host and subnet model much better, thankyou.

A question if I may?
If for example I had a small server running through an open port on my home router exposed to the wider Internet would there be any advantages in having a router, mail-room-server and subnet-server model as you describe or would it be just as safe to adopt the more simple home-router and small web-server model? Or to put it another way, would it provide greater security to use the more complex model?
Regards,
raspiham
This topic has been closed.