运行eb setenv
时,无法在.ebextensions中运行挂钩。在我的.ebextensions中,我将删除默认的nginx配置并将其替换为新的配置。每次应用启动时都会运行此挂钩,以便nginx不会崩溃。这是我的.ebextensions / proxy.config文件(我也附上了它)。
当我上传压缩的应用程序时,钩子按预期运行。只有当通过eb
api重新启动应用程序时,文件/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
才会重新出现
如果我做错了,或者这是ElasticBeanstalk的限制,请告诉我。
files:
/etc/nginx/conf.d/proxy.conf:
mode: "000644"
owner: root
group: root
content: |
upstream nodejs {
server 127.0.0.1:8081;
keepalive 256;
}
server {
listen 8080;
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
}
access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
access_log /var/log/nginx/access.log main;
location ~* ^/(api) {
proxy_pass http://nodejs;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
gzip on;
gzip_min_length 10240;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
location / {
alias /var/app/current/public/;
try_files $uri /index.html;
}
}
/opt/elasticbeanstalk/hooks/appdeploy/post/97_kill_default_nginx.sh:
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
set -x
rm -f /tmp/deployment/config/#etc#nginx#conf.d#* /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
initctl stop nginx || true
initctl start nginx
我最终重新阅读Platform Hooks docs更接近,发现appdeploy
和configdeploy
之间的区别
appdeploy - 在应用程序部署期间运行的脚本。 Elastic Beanstalk在启动新实例和客户端启动新版本部署时执行应用程序部署。
configdeploy - 当客户端执行影响实例上软件配置的配置更新时,脚本会运行,例如,通过设置环境属性或启用日志轮换到Amazon S3。
阅读完这些描述之后,我很清楚我需要移动改变我的proxy.config
文件,以便/opt/elasticbeanstalk/hooks/appdeploy/post/97_kill_default_nginx.sh
成为/opt/elasticbeanstalk/hooks/configdeploy/post/97_kill_default_nginx.sh
。为了安全并确保在启动应用程序的新版本以及更新env变量时运行此脚本,我将其复制/粘贴为BOTH appdeploy
和configdeploy
。所以最终的proxy.config
看起来像
files:
/etc/nginx/conf.d/proxy.conf:
mode: "000644"
owner: root
group: root
content: |
upstream nodejs {
server 127.0.0.1:8081;
keepalive 256;
}
server {
listen 8080;
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
}
access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
access_log /var/log/nginx/access.log main;
location ~* ^/(api) {
proxy_pass http://nodejs;
proxy_set_header Connection "";
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
gzip on;
gzip_min_length 10240;
gzip_comp_level 4;
gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
location / {
alias /var/app/current/public/;
try_files $uri /index.html;
}
}
/opt/elasticbeanstalk/hooks/appdeploy/post/97_kill_default_nginx.sh:
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
set -x
rm -f /tmp/deployment/config/#etc#nginx#conf.d#* /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
initctl stop nginx || true
initctl start nginx
/opt/elasticbeanstalk/hooks/configdeploy/post/97_kill_default_nginx.sh:
mode: "000755"
owner: root
group: root
content: |
#!/usr/bin/env bash
set -x
rm -f /tmp/deployment/config/#etc#nginx#conf.d#* /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
initctl stop nginx || true
initctl start nginx
调试此问题(以及任何钩子脚本)的一个好方法是ssh到由beanstalk管理的EC2并创建文件touch /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
。然后在单独的终端窗口中,运行eb setenv
(或任何命令以重新启动应用程序)。然后在ssh'd窗口中,一旦eb setenv
命令完成,检查文件被删除ls -l /etc/nginx/conf.d
这是一个有趣的方法。我按照亚马逊在这里发布的示例:AWS example,建议使用容器命令。
container_commands:
removeconfig:
command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"
但是,在更新压缩文件时(我认为与您的方法相同),配置更新总是崩溃。
我不得不按照这个答案中概述的方法来使用它来处理容器命令:stackoverflow restart script help
我的postcongif部署钩子现在看起来像(我使用容器命令):
/opt/elasticbeanstalk/hooks/configdeploy/post/99_kill_default_nginx.sh:
owner: root
group: root
mode: "000755"
content: |
#!/bin/bash -xe
set -x
echo "starting post config script"
status=`/sbin/status nginx`
if [[ -e "/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf" ]];
then
echo "default config file exists - we should delete it"; fi
rm -f /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
echo "restart nginx after deletion"
if [[ $status = *"start/running"* ]]; then
echo "stopping nginx..."
stop nginx
echo "starting nginx..."
start nginx
else
echo "nginx is not running... starting it..."
start nginx
fi
else
echo "no default config file exists - no need to delete it"
fi