Search code examples
phplaraveldockernginxdocker-desktop

Nginx inside docker container downloads all files instead of serving them but works on server


I have a laravel app that was create some time ago and the image for it is currently running and working on a Google Cloud Run container. I need to make some changes to it so I tried to load the container up on my Windows 11 workstation with Docker Desktop but for some reason when trying to open a webpage it is downloading a binary file instead of serving a page. I suspect it is not even getting to Nginx as nothing is showing up in the logs to says that access is taking place. I'm stumped as to why it is behaving in a completely different manner on my desktop to the server, I thought this was why we started using containers (to avoid these issues). Here are the relevant files.. Dockerfile

FROM php:8.1.15-fpm-alpine3.17

RUN apk add libzip-dev zip libpq-dev libmcrypt-dev gmp-dev jpegoptim optipng pngquant gifsicle icu-dev git openssh curl nano \
  && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \ 
  && docker-php-ext-install pdo pdo_pgsql pgsql \
  && docker-php-ext-install zip bcmath gmp

RUN apk --no-cache add pcre-dev ${PHPIZE_DEPS} \
  && pecl install redis \
  && docker-php-ext-enable redis \
  && pecl install mcrypt \
  && docker-php-ext-enable mcrypt \
  && apk del pcre-dev ${PHPIZE_DEPS} \
  && rm -rf /tmp/pear

RUN docker-php-ext-configure intl && docker-php-ext-install intl

RUN apk add --update freetype libpng libjpeg-turbo freetype-dev libpng-dev libjpeg-turbo-dev libwebp-dev \
    && docker-php-ext-configure gd \
    --with-freetype \
    --with-jpeg \
    --with-webp \
    NPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) \
    && docker-php-ext-install -j$(nproc) gd \
    && apk del freetype-dev libpng-dev libjpeg-turbo-dev \
    && docker-php-ext-configure exif \
    && docker-php-ext-install exif \
    && docker-php-ext-enable exif \
    && mkdir /var/lib/php  \
    && chown www-data:www-data /var/lib/php -R

RUN apk add wget nginx nodejs npm brotli nginx-mod-http-brotli

RUN mkdir -p /run/nginx

COPY docker/nginx.conf /etc/nginx/nginx.conf

RUN mkdir -p /docker
COPY ./docker /docker
RUN mkdir -p /app
COPY ./src /app

RUN sh -c "wget http://getcomposer.org/composer.phar && chmod a+x composer.phar && mv composer.phar /usr/local/bin/composer"
RUN cd /app && \
    /usr/local/bin/composer install --optimize-autoloader --no-dev && \
    php artisan storage:link

RUN chown -R www-data: /app
RUN chgrp -R www-data /app/storage /app/bootstrap/cache
RUN chmod -R ug+rwx /app/storage /app/bootstrap/cache

RUN sed -i 's/\r$//' /docker/startup.sh  && \  
        chmod +x /docker/startup.sh


CMD sh /docker/startup.sh

nginx.conf

worker_processes  1;
load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen LISTEN_PORT http2 default_server;
        server_name _;
        root /app/public;

        add_header X-Frame-Options "SAMEORIGIN";
        add_header X-Content-Type-Options "nosniff";
        
        index index.php;
        charset utf-8;
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }

        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }
        access_log /dev/stdout;
        error_log /dev/stderr;
        sendfile off;
        client_max_body_size 200m;
        brotli on;
        brotli_static on;
        brotli_types *;

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors off; 
            fastcgi_buffering off; 
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }
    #include /etc/nginx/sites-enabled/*;
}

daemon off;

startup.sh

#!/bin/sh

sed -i "s,LISTEN_PORT,$PORT,g" /etc/nginx/nginx.conf

php-fpm -D

nginx

Anyone have any suggestions on where to start looking for the issue?

added Ok I'm not sure what is going on, ipv6 is disabled in nginx but my browser is connecting to nginx when I send a request to localhost. If I check in the terminal of the docker there is nothing user port 80 on ipv6?


Solution

  • OK for anyone who comes accross this same issue I tracked the problem down to the fact that http2 can only be used over SSL connections. When running on Google Cloud Run the internals of their system takes care of that but on my local Docker install it was not.