8

Could someone tell me how I can run Django on two ports simultaneously? The default Django configuration only listens on port 8000. I'd like to run another instance on port xxxx as well. I'd like to redirect all requests to this second port to a particular app in my Django application.

I need to accomplish this with the default Django installation and not by using a webserver like nginx, Apache, etc.

Thank you


Let's say I two applications in my Django application. Now i don't mean two separate Django applications but the separate folders inside the 'app' directory. Let's call this app1 and app2

I want all requests on port 8000 to go to app1 and all requests on port XXXX to go to app2

HTH.

Mridang Agarwalla
  • 43,201
  • 71
  • 221
  • 382

4 Answers4

17

Just run two instances of ./manage.py runserver. You can set a port by simply specifying it directly: ./manage.py runserver 8002 to listen on port 8002.

Edit I don't really understand why you want to do this. If you want two servers serving different parts of your site, then you have in effect two sites, which will need two separate settings.py and urls.py files. You'd then run one instance of runserver with each, passing the settings flag appropriately: ./manage.py runserver 8002 --settings=app1.settings

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
7

One other thing to consider - django's session stuff will use the same session cookie for each site, and since cookies are not port specific, you'll have issues with getting logged out every time you switch between windows unless you use multiple browser sessions/private browsing during development.

Although this is what you need to do when logging in as 2 different users on the same site, logging into 2 different sites both running django on different localhost ports doesn't have to work like this.

One easy solution is to write a simple middleware to fix this by appending the port number to the variable name used to store your session id. Here's the one I use.

Community
  • 1
  • 1
turtlemonvh
  • 9,149
  • 6
  • 47
  • 53
  • I wonder if this will resolve my issue. I am running multiple docker containers on an instance, so django is running on multiple host ports. When I log in, if response is served from another container, it responds with login again as its unaware I just authenticated with another server and so it can't retrieve the session. – radtek Aug 18 '18 at 01:41
  • Thank you! I had this problem and I thought it was the new Firefox version! – Genarito Nov 19 '20 at 20:05
2

The built-in web-server is intended for development only, so you should really be using apache or similar in an situation where you need to run on multiple ports.

On the other hand you should be able to start up multiple servers just by starting multiple instances of runserver. As long as you are using a separate database server I don't think that will have any extra problems.

Douglas Leeder
  • 52,368
  • 9
  • 94
  • 137
0

If you need more information about the configuration of server/servers you can check out Django documentation related to this topic.

Paweł Pedryc
  • 368
  • 2
  • 5
  • 19