gpt4 book ai didi

python - 不修改 Django wsgi.conf 的 AWS ElasticBeanstalk 更新

转载 作者:太空宇宙 更新时间:2023-11-04 10:09:36 27 4
gpt4 key购买 nike

我使用自动缩放在 AWS EB 中部署了一个 Django 应用程序。此应用程序使用带有 token 身份验证的 Django Rest。为了让它工作,我必须在 etc/httpd/conf.d/wsgi.conf 文件中添加以下行:

RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

WSGIPassAuthorization On

问题是:当 AWS 进行自动缩放或 ElasticBeanstalk 环境升级时,wsgi.conf 文件会更新并删除自定义设置。

我怎样才能避免这种情况?

提前致谢

最佳答案

为避免 ElasticBeanstalk 在自动缩放或重新初始化环境中的任何实例时删除您的自定义设置,您应该使用 .ebextensions对 ec2 实例的 config 进行任何持久修改的脚本文件。

(按照您使用 eb ssh 进行的“手动”测试这些修改后)

在这种情况下,您可以使用例如 sed command编辑您的 wsgi.conf文件。

将以下 container_command 添加到您的 yaml Elastic Beanstalk 配置文件中(即:.ebextension/yourapp.config):

03_wsgipass:
command: 'sed -i -f .ebextensions/wsgi_update.sed ../wsgi.conf'

它应该看起来像这样:

container_commands:
01_migrate:
command: "django-admin.py migrate --noinput"
leader_only: true
02_collectstatic:
command: "django-admin.py collectstatic --noinput"
03_wsgipass:
command: 'sed -i -f .ebextensions/wsgi_update.sed ../wsgi.conf'

wsgi_update.sed 中创建一个新文件在.ebextensions包含以下内容的文件夹:

/<Virtual/ a\
RewriteEngine On\
RewriteCond %{HTTP:Authorization} ^(.*)\
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]

/<\/Virtual/ a\
WSGIPassAuthorization On

这是一个小sed program例如,将添加 apache mod_rewrite <VirtualHost> 里面的规则 block 和 WSGIPassAuthorization结束标记后的行 </VirtualHost>在你的wsgi.conf文件

它将在每个应用程序部署到您的环境中通过自动缩放创建的任何现有或新实例时执行。

参见 Using the AWS Elastic Beanstalk Python Platform了解更多信息

关于python - 不修改 Django wsgi.conf 的 AWS ElasticBeanstalk 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39168351/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com