amazon-web-servicesnginx

AWS Fargate: PHP file not be served


I have this fairly simple Dockerfile:

FROM php:fpm

# Install vim
RUN apt-get update && \
    apt-get install -y nginx supervisor vim && \
    rm -rf /var/lib/apt/lists/* && \
    rm -rf /usr/share/nginx/html/*


COPY ./index.php /var/www/html/index.php
COPY ./nginx.conf /etc/nginx/conf.d/default.conf
COPY ./supervisord.conf /etc/supervisor/conf.d/supervisord.conf

RUN chown -R www-data:www-data /var/www/html

ENV FARGATE_ENV="faaargate"

EXPOSE 80

CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

And Nginx conf:

server {
    listen 80;
    server_name localhost;

    root /var/www/html;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
        include fastcgi_params;
    }
}

The index.php file literally is

<h1>Hello World</h1>

Locally, works like a breeze, but when it gets to ECR and then into a Fargate task, it fails.

I even SSH'd into the container and everything looks like it does in my local. I'm wondering what could be the problem?

Thank you!

EDIT

All the code can be found here.

The main file to look at it is: lib/fargate-service-stack.ts


Solution

  • Fails as in, doesn't render the PHP files, but rather the nginx splash page.

    Your follow up comment clears it up I think - your server block only matches when the Host header is localhost:

    server {
        listen 80;
        server_name localhost;
        ...
    

    Obviously that won't be the case when deployed on Fargate so the server block won't match and you get the nginx splash page. Set server_name to the domain name of your site, or make the block the default with e.g.:

    server {
        listen 80 default_server;
        server_name _;
        ...