从同一个域提供多个Django项目,使用路径而不是子域进行路由

时间:2018-02-14 16:15:47

标签: django nginx

我想服务" app1"和" app2"来自同一个域使用路径来路由应用程序:

https://exampe.com/app1/https://exampe.com/app2/

通常我会使用子域进行此操作,但这是尝试为用户提供更加无缝的体验 - 所以它看起来像一个应用程序(app1和app2具有相同的页眉和页脚)

DNS无法实现这一点,但也许有nginx或其他解决方案?

2 个答案:

答案 0 :(得分:0)

您可以通过Apache2使用WSGIScriptAlias选项 这就是我在本地主机上托管四个Django项目的方式:

<VirtualHost *:80>
    #My site Name

    ServerName localhost

#Demon process for multiple virtual hosts
    WSGIDaemonProcess subzero python-home=/var/www/env1 python-path=/var/www/env1/subzero threads=5


    #Pointing wsgi script to config file
    WSGIScriptAlias /subzero /var/www/env1/subzero/subzero/wsgi.py

    WSGIProcessGroup subzero
 #Your static files location
   Alias /subzero/static/ "/var/www/env1/subzero/static/"
     <Location "/media">
        SetHandler None
   </Location>
   <LocationMatch "\.(jpg|gif|png|js|css)$">
       SetHandler None
   </LocationMatch>
   <Directory /var/www/env1/subzero >
       WSGIProcessGroup subzero
       Order deny,allow
       Allow from all
   </Directory>

  #Demon process for multiple virtual hosts
    WSGIDaemonProcess gastronomy python-home=/var/www/env1 python-path=/var/www/env1/gastronomy threads=5


    #Pointing wsgi script to config file
    WSGIScriptAlias /gastronomy /var/www/env1/gastronomy/gastronomy/wsgi.py
    WSGIProcessGroup gastronomy
 #Your static files location
   Alias /gastronomy/static/ "/var/www/env1/gastronomy/static/"
     <Location "/media">
        SetHandler None
   </Location>
   <LocationMatch "\.(jpg|gif|png|js|css)$">
       SetHandler None
   </LocationMatch>
   <Directory /var/www/env1/gastronomy >
       WSGIProcessGroup gastronomy
       Order deny,allow
       Allow from all
   </Directory>



  #Demon process for multiple virtual hosts
    WSGIDaemonProcess astronomy python-home=/var/www/env1 python-path=/var/www/env1/astronomy threads=5


    #Pointing wsgi script to config file
    WSGIScriptAlias /astronomy /var/www/env1/astronomy/astronomy/wsgi.py
    WSGIProcessGroup astronomy

    #Your static files location
   Alias /astronomy/static/ "/var/www/env1/astronomy/static/"
     <Location "/media">
        SetHandler None
   </Location>
   <LocationMatch "\.(jpg|gif|png|js|css)$">
       SetHandler None
   </LocationMatch>
   <Directory /var/www/env1/astronomy >
       WSGIProcessGroup astronomy
       Order deny,allow
       Allow from all
   </Directory>


  #Demon process for multiple virtual hosts
    WSGIDaemonProcess mysite python-home=/var/www/env1 python-path=/var/www/env1/mysite threads=5


    #Pointing wsgi script to config file
    WSGIScriptAlias /mysite /var/www/env1/mysite/mysite/wsgi.py
    WSGIProcessGroup mysite
  #Your static files location

  Alias /mysite/media/ "/var/www/env1/mysite/media/"
  Alias /mysite/static/ "/var/www/env1/mysite/static/"
     <Location "/static">
        SetHandler None
   </Location>

<Location "/media">
        SetHandler None
   </Location>
<LocationMatch "\.(jpg|gif|png|js|css)$">
       SetHandler None
   </LocationMatch>


   <Directory /var/www/env1/mysite >
       WSGIProcessGroup mysite
       Order deny,allow
       Allow from all
   </Directory>



</VirtualHost>

希望对您有帮助!

答案 1 :(得分:0)

我可能没有仔细阅读您的答案。 如果您不想使用子域,则可以考虑使用django网站框架: https://docs.djangoproject.com/en/dev/ref/contrib/sites/

这是一种更干净但更复杂的解决方案。