Multiple Django apps on same port
Introduction The focus of this article is to run multiple services on a single port, such as hosting two websites on port 80 and port 443. Although docker allows us to map the ports from container to the host as we want, and eliminates the need to run multiple services on a single port, But there are lot of legacy Business Applications being used where docker is not supported. Ports As we all know from the facts that in a system there are total 65536 ports that is from 0 - 65535 ports that are being used, out of this many are system reserved ports and only small amount of ports are available on which we can expose services. Such as port 80 for HTTP, 443 for HTTPS, 22 for SSH , 3389 for RDP and so on. Network Adaptors By default every system has at least 2 network cards, one is the loopback or the localhost network card (127.0.0.1), and another one would be either WiFi or the LAN network card (192.168.1.1). This can be checked using the command ipconfig if you are using the windows system or if you are Debian/Ubuntu distribution use ifconfig. I have docker installed on my system so one more network adaptor is visible for the docker. You can create multiple network adaptors if needed, simplest way is to use the docker or the virtual box to create network cards. Setup Django applications I have forked two different Django applications from GitHub for this demo, or you can simply create one if needed. We are runing: One Django app with devops.settings on http://localhost:3000, having the route demo Another Django app with mysite.settings on http://10.160.0.4:3000, does not have the route demo Despite both using port 3000, they operate without conflict because they are bound to different network interfaces Localhost is bound to the loopback interface 10.160.0.4 binds to the machine's network interface IP. Running the Applications Please ensure you have activated the required virtual environment, and are already in the correct directory Starting the application only on Localhost, python3 manage.py runserver localhost:3000 Starting the application only the IP address python3 manage.py runserver 10.160.0.4:3000 Observing the Behavior Both the servers start successfully on port 3000 The Localhost redirected the requests for the route /demo, here I was not following the redirects, as redirects on Django mostly indicates its a custom route, but you can follow the redirects using the curl -L The IP address on /demo path returned 404, as demo path is not defined on this application. The difference in the behavior for the same route is a clear indicator that 2 different applications are getting called. Observe the server URL and debug logs. Conclusion Just in case if you want to expose the service to all the network adaptors you can use 0.0.0.0:. From what we saw, the ports can be scoped to the network adaptor level and can be used parallelly without any conflicts. So if we assume there are x number of usable ports in the system, and there are y network interfaces on the system, so its safe to say that we can actually expose x*y services. Please feel free to add or discuss, that you feel I might have missed out.

Introduction
The focus of this article is to run multiple services on a single port, such as hosting two websites on port 80 and port 443.
Although docker allows us to map the ports from container to the host as we want, and eliminates the need to run multiple services on a single port, But there are lot of legacy Business Applications being used where docker is not supported.
Ports
As we all know from the facts that in a system there are total 65536 ports that is from 0 - 65535 ports that are being used, out of this many are system reserved ports and only small amount of ports are available on which we can expose services. Such as port 80 for HTTP, 443 for HTTPS, 22 for SSH , 3389 for RDP and so on.
Network Adaptors
By default every system has at least 2 network cards, one is the loopback or the localhost network card (127.0.0.1), and another one would be either WiFi or the LAN network card (192.168.1.1).
This can be checked using the command ipconfig
if you are using the windows system or if you are Debian/Ubuntu distribution use ifconfig
.
I have docker installed on my system so one more network adaptor is visible for the docker. You can create multiple network adaptors if needed, simplest way is to use the docker or the virtual box to create network cards.
Setup Django applications
I have forked two different Django applications from GitHub for this demo, or you can simply create one if needed.
We are runing:
- One Django app with
devops.settings
onhttp://localhost:3000
, having the route demo - Another Django app with
mysite.settings
onhttp://10.160.0.4:3000
, does not have the route demo
Despite both using port 3000, they operate without conflict because they are bound to different network interfaces
-
Localhost
is bound to the loopback interface -
10.160.0.4
binds to the machine's network interface IP.
Running the Applications
Please ensure you have activated the required virtual environment, and are already in the correct directory
Starting the application only on Localhost,
python3 manage.py runserver localhost:3000
Starting the application only the IP address
python3 manage.py runserver 10.160.0.4:3000
Observing the Behavior
- Both the servers start successfully on port 3000
- The Localhost redirected the requests for the route /demo, here I was not following the redirects, as redirects on Django mostly indicates its a custom route, but you can follow the redirects using the
curl -L
- The IP address on /demo path returned 404, as demo path is not defined on this application.
The difference in the behavior for the same route is a clear indicator that 2 different applications are getting called.
Observe the server URL and debug logs.
Conclusion
Just in case if you want to expose the service to all the network adaptors you can use 0.0.0.0:
.
From what we saw, the ports can be scoped to the network adaptor level and can be used parallelly without any conflicts. So if we assume there are x number of usable ports in the system, and there are y network interfaces on the system, so its safe to say that we can actually expose x*y
services.
Please feel free to add or discuss, that you feel I might have missed out.