简体   繁体   English

如何使用 DRY 配置部署多个 Django 站点?

[英]How to deploy multiple Django sites with DRY configuration?

Currently I am deploying more than 30 websites which are operated by a single application, written in Django.目前我正在部署 30 多个由单个应用程序操作的网站,这些应用程序是用 Django 编写的。 The sites are combined into smaller groups.这些站点被组合成更小的组。 For example, there is a group Europe, which includes websites Germany, France, Switzerland, etc. Those groups are 7.例如,有一个欧洲组,其中包括德国、法国、瑞士等网站,这些组是 7。

For deployment uwsgi (operating in Emperior mode), nginx (uwsgi passing to the group socket) and supervisord (for task workers) are used.对于部署 uwsgi(在 Emperior 模式下运行),使用 nginx(uwsgi 传递到组套接字)和 supervisord(用于任务工作人员)。 Application is using a single database for all websites.应用程序为所有网站使用单一数据库。

The structure of settings files in the project currently is as follows:目前项目中设置文件的结构如下:

  • project.settings.general_settings
  • project.settings.<group_name>

So far I thought that it would be enough to simply have 7 uwsgi and 7 nginx configuration files (for each group), until I realized some of the websites will also need custom Django settings, and separate DJANGO_SETTINGS_MODULE will have to be defined for each website (some of those websites have different languages lets say).到目前为止,我认为只需要 7 个 uwsgi 和 7 个 nginx 配置文件(每个组)就足够了,直到我意识到某些网站还需要自定义 Django 设置,并且必须为每个网站定义单独的DJANGO_SETTINGS_MODULE (其中一些网站有不同的语言,可以说)。

Things to keep in mind:要记住的事情:

  • What would be your strategy for maintaining the all those ~30 different website settings?您维护所有这些约 30 个不同的网站设置的策略是什么? They should be importing the group settings as well.他们也应该导入组设置。
  • Currently the DJANGO_SETTINGS_MODULE is specified in both Nginx and uwsgi configuration files.目前DJANGO_SETTINGS_MODULE在 Nginx 和 uwsgi 配置文件中都有指定。 Is there a way to avoid this duplication?有没有办法避免这种重复?
  • Is there a way to keep the amount of uwsgi apps reduced (maybe have uwsgi only 7 uwsgi groups, and specify the settings module only from nginx)?有没有办法减少 uwsgi 应用程序的数量(也许 uwsgi 只有 7 个 uwsgi 组,并且仅从 nginx 指定设置模块)?
  • Bonus: in addition to the production environment there is a staging environment as well.奖励:除了生产环境外,还有一个临时环境。 What would be the best way to avoid duplication there as well?避免重复的最佳方法是什么?

What was considered so far:到目前为止的考虑:

  • Have some of the website settings ( LANGUAGES , LANGUAGE_CODE , etc.) stored in the database and dynamically modified for each request.将一些网站设置( LANGUAGESLANGUAGE_CODE等)存储在数据库中,并针对每个请求进行动态修改。 The idea was rejected, as it obviously sucks .这个想法被拒绝了,因为它显然很糟糕
  • Have uwsgi in Virtual Hosting Mode.在虚拟主机模式下使用 uwsgi。 It would allow to have only 7 uwsgi apps, and specify different settings from Nginx.它只允许有 7 个 uwsgi 应用程序,并从 Nginx 指定不同的设置。 Rejected: insecure, unreliable, scaling issues .拒绝:不安全、不可靠、扩展问题

If you're using emperor mode and your uWSGI configs differs only in socket and DJANGO_SETTINGS_MODULE , consider using one config file for all sites.如果您使用的是皇帝模式并且您的 uWSGI 配置仅在套接字和DJANGO_SETTINGS_MODULE有所不同,请考虑对所有站点使用一个配置文件。 You will start new site basically by creating symlink in uWSGI emperor directory and special variables inside that config will construct socket names and DJANGO_SETTINGS_MODULE variable based on symlink name.您将基本上通过在 uWSGI 皇帝目录中创建符号链接来启动新站点,并且该配置中的特殊变量将基于符号链接名称构造套接字名称和DJANGO_SETTINGS_MODULE变量。

If you're only worried about urlpatterns, language and other values that can be determined at request, you can use django-hosts instead of built-in sites module.如果您只担心可以根据请求确定的 urlpatterns、语言和其他值,您可以使用django-hosts而不是内置的站点模块。 That will also allow you to serve multiple sites on one workers pool.这也将允许您在一个工作人员池上为多个站点提供服务。 You can also create some middleware for that purpose.您还可以为此创建一些中间件。

Note that you can also use environment variables (which can be set in uWSGI config file) to set some of settings.请注意,您还可以使用环境变量(可以在 uWSGI 配置文件中设置)来设置一些设置。 Just use os.environ in your settings file to extract them.只需在您的设置文件中使用os.environ来提取它们。

And last, but not least - settings file can be set dynamically in wsgi.py file based on some other variables.最后但并非最不重要的 - 设置文件可以根据其他一些变量在 wsgi.py 文件中动态设置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM