I have the following setup: windows 10 pro running docker for windows (latest versions of both).
I have 2 containers/images "nginx_contianer" and "php_container" which are built by two docker files via a docker-compose.yml file.
I have the following files which are placed into the php_container at /var/www/html/
index.php (phpinfo()), test.html (html hello world) and test.php (echo’s hello world)
The nginx.conf I use is serving php files (index.php / and test.php work) but wont serve html files… test.html results with a 404. The file is there with the php files in the php_container.
Anyone have any ideas?
UPDATE
It looks like html files need to be served from the nginx_contianer at /var/www/html
How can I get html and php files to be served from the same directory?
Here are my docker files.
file: Dockerfile.nginx
FROM nginx:alpine
# RUN rm /etc/nginx/conf.d/default.conf
RUN mv /etc/nginx/conf.d/default.conf /etc/nginx/conf.d/default.conf.bak
# USER root
# Ensure nginx user has permissions to the cache and pid directory
RUN mkdir -p /var/cache/nginx/client_temp &&
chown -R nginx:nginx /var/cache/nginx &&
mkdir -p /var/run/nginx &&
chown -R nginx:nginx /var/run/nginx
# Update, upgrade and install some things
RUN apk update &&
apk upgrade &&
apk add --no-cache
nano
ncdu
# Switch back to nginx user
# USER nginx
# Copy a new configuration file from your local to the container
COPY ./nginx/nginx.conf /etc/nginx/conf.d
RUN chown -R nginx:nginx /etc/nginx/conf.d
# Expose port 80 and 443 to the host
EXPOSE 80 443
WORKDIR /etc/nginx/conf.d
file: Dockerfile.php
FROM php:8.3-fpm-alpine
# Update, upgrade and install some things
RUN apk update &&
apk upgrade &&
apk add --no-cache
nano
ncdu
libzip-dev
zip
# Install some stuff
RUN docker-php-ext-install
zip
# Clean up
RUN rm -rf /var/cache/apk/*
COPY --chown=www-data:www-data ./src/ /var/www/html/
RUN chmod -R 755 /var/www/html
WORKDIR /var/www/html/
file: docker-compose.yml
version: '3.8'
services:
php_container:
container_name: php_container
build:
context: .
dockerfile: Dockerfile.php
restart: on-failure:3
networks:
- backend
nginx_container:
container_name: nginx_container
build:
context: .
dockerfile: Dockerfile.nginx
ports:
- "80:80"
- "443:443"
restart: on-failure:3
depends_on:
- php_container
networks:
- backend
- default
networks:
backend:
file: nginx.conf
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.php index.html index.htm;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
# error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location / {
try_files $uri $uri/ =404;
# try_files $uri $uri/ /index.php?$query_string; # works but everything goes to index.php
}
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass php_container:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
2
Answers
Answer was to use volumes...
modified my docker-compose.yml to this and it now works fine.
The non-PHP source files (the HTML, CSS, etc) need to be accessible to the nginx container because nginx can’t serve them out of the PHP container, which it doesn’t have access to. If nginx then encounters a PHP file, it will pass the path of that file to the PHP container via the FPM socket, and the PHP container will then access it locally. So, simple solution, just add the files to the nginx container as well:
Ideally however, you’d want them in just once place, which you could do with another static container or a volume mount.