Nginx 和 PHP 可以并行运行。 ECS 中的任务定义可以包含这两个容器,其中 nginx 容器公开端口 80,而 PHP 容器公开 9000(可能使用 PHP-FPM)。 Nginx 可以作为代理路由请求到 PHP 进程。
示例task definition(简化值以演示多容器设置):
{
"containerDefinitions": [
{
"portMappings": [
{
"hostPort": 9000,
"protocol": "tcp",
"containerPort": 9000
}
],
"command": [
"php-fpm"
],
"image": "image-url-for-php:latest",
"name": "php-fpm"
},
{
"portMappings": [
{
"hostPort": 80,
"protocol": "tcp",
"containerPort": 80
}
],
"image": "image-url-for-nginx:latest",
"dependsOn": [
{
"containerName": "php-fpm",
"condition": "START"
}
],
"essential": true,
"name": "nginx"
}
]
}
nginx 配置如下所示:
server {
listen 80;
index index.php index.html;
root /var/www/html/public; # change this to application files path
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass localhost:9000; # PHP container accessible via localhost:9000 if running side by side
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
gzip_static on;
}
}
然后可以使用此任务定义创建 ELB 可以用作目标组的服务(路由到端口 80 用于 nginx)。从那里所有请求都将被代理到 PHP 容器。
就 nginx-backend 而言,可以使用相同的设置。但是,如果此服务不会暴露于公共流量,您将需要使用内部 ELB 或Service Discovery。然而,这个想法是 PHP 容器本身不需要专用的 ELB。