-2

I wan to install many Apache with php servers in the same virtual machine running in different ports because I want to host N number of different websites into the same virtual machine to avoid creating N number of virtual machines to host N number of websites.

is there any easy DNS configuration for redirect a different domain to same ip in different port?

Example:

  • www.example1.com 🡲 172.217.14.100:80
  • www.example2.com 🡲 172.217.14.100:81
  • www.example3.com 🡲 172.217.14.100:82
  • www.example4.com 🡲 172.217.14.100:83
2
  • 1
    DNS is for IP addresses, not transport protocol port numbers.
    – Ron Maupin
    May 10 '20 at 0:44
  • Why do you believe multiple ports are needed? They almost certainly aren't. May 10 '20 at 3:16
3

Achieving what you want to do via DNS Records cannot be done as far as I know.

Apache

Apache, through Name Base Virtual Host blocks and configuring Apache to listen on the required ports.

# Listens on all IP Addresses and ports 80 - 83    
  Listen 80
  Listen 81
  Listen 82
  Listen 83

Using your Name Based virtual hosts allows you to catch each request based on name passed in the host value;

<virtualhost example1.com:80>

   # all your configuration for example1.com

</virtualhost>

<virtualhost example2.com:81>

   # all your configuration for example2.com

</virtualhost>

Ports

Requiring users to enter a port is not a great solution and is likely to fail in the long run. We cannot rely on users knowing how to add a port or to even remember they need to add a port.

Nginx Reverse Proxy

A better solution would be to have Nginx as your front end which receives requests and passes that request to Apache using the port structure you require.

Server {

         listen 80;
         server_name example1.com www.example1.com

         # other stuff as required

         proxy_pass http://192.168.100:80;
}

Server {

         listen 81;
         server_name example2.com www.example2.com

         # other stuff as required

         proxy_pass http://192.168.100:81;
} 

By using Nginx the client enters example2.com (without port) and Nginx receives it, processes it and gets results from Apache listening on port 81.

Nginx Reverse Proxy

Nginx will also give you a performance boost. There is a learning curve and some configuration fine tuning to do but in the long run it would be a better solution.

There are hundreds of examples of Reverse Proxy using Nginx. Google is your friend.

1
  • Shouldn't it be "listen 80" instead of "listen 81" for the example2.com?
    – AtliB
    Apr 11 at 21:44
0

This can not be done with DNS. The mechanism required & srv records - are not part of the http(s) spec.

It us unclear why you believe you need multiple ports for multiple websites. You can have multiple websites on one machine with 1 port and 1 address. The technology here is called Name Based Virtual Hosting, and is part of the ubiquitous http 1.1 spec. If you are using Apache you just create virtualhost containers for each domain. You have been able to do the same with https hosts since Windows XP was declared dead (the technology is called sni - server name indicator)

If multiple ports are not negotiable, you need to set up a reverse proxy. You can use mod_proxy and ProxyPass/ProxypassReverse pairs in appropriate Virtualhost containers in Apache to "loop back" to a different port on localhost.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.