我在 AWS Elastic Beanstalk 中的网站在使用 nginx 安装在 php 8.1 中后运行正常。然而几天后,AWS 更新了 Elastic Beanstalk 的环境配置后,由于 nginx 错误,该网站关闭了。网站错误如下所示:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
我必须使用以下 shell 命令通过 ssh 进入 AWS ec2 实例并手动重新启动 nginx 才能使其正常工作:
sudo service nginx restart
我将 nginx.conf 存储在 .platform/nginx/nginx.conf 中。 nginx.conf如下:
#Elastic Beanstalk Nginx Configuration File
user nginx;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
worker_processes auto;
worker_rlimit_nofile 31486;
events {
worker_connections 1024;
}
http {
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
include conf.d/*.conf;
map $http_upgrade $connection_upgrade {
default "upgrade";
}
server {
location ^~ /pv/ {
# allow 192.168.1.1/24;
deny all;
return 404;
}
listen 80 default_server;
access_log /var/log/nginx/access.log main;
client_header_timeout 60;
client_body_timeout 60;
client_max_body_size 50M;
keepalive_timeout 60;
gzip off;
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;
# Include the Elastic Beanstalk generated locations
include conf.d/elasticbeanstalk/*.conf;
server_name mywebsitename.com;
index index.php;
error_page 404 /index.php;
location /app/upload_video.php {
client_max_body_size 2084M;
}
if ($http_user_agent ~ "Snabcd"){
rewrite ^/?cup/product/([^/]+)/([^/]+)/([^/]+)/location/([^/]+)/([^/]+)/([^/]+)(?:/(menu-selected)/([^/]+)|)/?$ https://$host/indexapp.php?c1=$1&c2=$2&c3=$3&r2=$4&r3=$5&r4=$6&f_urlseo=1&f_idxt=cpn&$7=$8 last;
rewrite ^/?cup/product/([^/]+)/([^/]+)/([^/]+)(?:/(menu-selected)/([^/]+)|)/?$ https://$host/indexapp.php?c1=$1&c2=$2&c3=$3&f_urlseo=1&f_idxt=cpn&$4=$5 permanent ;
}
if ($http_user_agent !~ "Snabcd"){
rewrite ^/?cup/product/([^/]+)/([^/]+)/([^/]+)/location/([^/]+)/([^/]+)/([^/]+)(?:/(menu-selected)/([^/]+)|)/?$ https://$host/indexpc.php?c1=$1&c2=$2&c3=$3&r2=$4&r3=$5&r4=$6&f_urlseo=1&f_idxt=cpn&$7=$8 last;
rewrite ^/?cup/product/([^/]+)/([^/]+)/([^/]+)(?:/(menu-selected)/([^/]+)|)/?$ https://$host/indexpc.php?c1=$1&c2=$2&c3=$3&f_urlseo=1&f_idxt=cpn&$4=$5
}
rewrite ^/?ld/([^/]+).*/?$ https://$host/ad_detail.php?ldasid=$1 permanent;
rewrite ^/?job/sales/location/([^/]+)/([^/]+)/([^/]+).*/?$ https://$host/users/jobs/job_sales.php?r2=$1&r3=$2&r4=$3 permanent;
https://$host/ect/gamesrc/javascript/tw/index_all_games.php [L,R=301]
if ($host ~ "192.168.|172.20.|10.0."){
rewrite ^/?games/?$ https://$host/ect/gamesrc/javascript/tw/index_all_games.php permanent;
}
rewrite ^/?games/?$ https://$host/gamesrc/javascript/tw/index_all_games.php permanent;
}
}
文件夹 .ebextensions 内的 AWS EBS 配置文件如下:
packages:
yum:
sysstat: []
Resources:
sslSecurityGroupIngress:
Type: AWS::EC2::SecurityGroupIngress
Properties:
GroupId: {Ref : AWSEBSecurityGroup}
IpProtocol: tcp
ToPort: 443
FromPort: 443
CidrIp: 0.0.0.0/0
container_commands:
01nginxrestart:
command: "sudo service nginx restart"
这里有什么问题吗? AWS 为什么不自动重启 nginx?
我认为必须手动重新启动 Nginx 服务的原因是因为
container_command
在部署应用程序之前运行。
查看有关 应用程序部署平台挂钩的 EB 开发人员指南、
预部署文件在运行任何配置文件的 container_commands 部分中找到的命令之后、运行 Procfile 命令之前运行。
Nginx 服务应该在
postdeploy
钩子中重新启动:
postdeploy – 此处的文件在 Elastic Beanstalk 平台引擎部署应用程序和代理服务器后运行。
这是部署工作流程的最后一个步骤。
在项目中创建 postdeploy 钩子以重新启动 Nginx 服务。
mkdir -p .platform/hooks/postdeploy && touch .platform/hooks/postdeploy/01_restart_nginx.sh
在hook脚本中写入重启Nginx服务的指令。
cat > .platform/hooks/postdeploy/01_restart_nginx.sh <<EOF
#!/bin/bash
sudo service nginx restart
EOF
然后使文件可执行
chmod +x .platform/hooks/postdeploy/01_restart_nginx.sh