为避免 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'
在.ebextensions文件夹的wsgi_update.sed中创建一个新文件,内容如下:
/<Virtual/ a\
RewriteEngine On\
RewriteCond %{HTTP:Authorization} ^(.*)\
RewriteRule .* - [e=HTTP_AUTHORIZATION:%1]
/<\/Virtual/ a\
WSGIPassAuthorization On
这是一个小的sed program 例如,它将在您的<VirtualHost> 块中添加apache mod_rewrite 规则,并在您的wsgi.conf 文件中的结束标记</VirtualHost> 之后添加WSGIPassAuthorization 行
它将在每个应用程序部署到您的环境中通过自动缩放创建的任何现有或新实例时执行。
查看Using the AWS Elastic Beanstalk Python Platform了解更多信息