I faced with strange behaviour and did not understand what need to fix it, I have laravel with nginx and by some reason faced with 404 Not Found
so, my docker compose
version: '3.7'
services:
nginx-laratest:
build:
context: .
dockerfile: ./nginx/Dockerfile
restart: on-failure
volumes:
- "../:/var/www"
ports:
- "80:80"
- "443:443"
depends_on:
- php-laratest
networks:
- nginx-laratest-networks
- php-laratest-networks
php-laratest:
build:
context: .
dockerfile: ./php/Dockerfile
restart: on-failure
volumes:
- "../:/var/www"
- "./php/php.ini:/usr/local/etc/php/conf.d/custom.ini"
- "./data/php:/var/laratest"
networks:
- php-laratest-networks
networks:
php-laratest-networks:
nginx-laratest-networks:
and docker/nginx/Dockerfile
# Use Alpine Linux
FROM alpine:latest
RUN apk add --update --no-cache nginx
# Timezone
ENV TIMEZONE Europe/Kiev
COPY nginx/zt.conf /etc/nginx/conf.d/
COPY nginx/zt.conf /etc/nginx/sites-enable/
RUN adduser -D -g '' -G www-data www-data
RUN mkdir -p /run/nginx
#CMD ["nginx"]
CMD [ "nginx", "-g", "daemon off;" ]
EXPOSE 80
EXPOSE 443
an docker/nginx/zt.conf
server {
server_name laratest.local.com;
listen 80;
root /var/www/public;
index index.php index.html index.htm;
location /storage/ {
alias /var/www/storage/app/public/;
autoindex off;
}
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ .php$ {
try_files $uri /index.php =404;
client_max_body_size 50m;
fastcgi_pass php-laratest:9000;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_read_timeout 3600;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/public/index.php;
fastcgi_param PHP_VALUE "error_log=/var/log/nginx/php-laratest_errors.log";
}
access_log /var/log/nginx/php-laratest_service.access.log;
error_log /var/log/nginx/php-laratest_service.error.log crit;
location ~ /.ht {
deny all;
}
}
the I added laratest.local.com
to my hosts 127.0.0.1 laratest.local.com
and try to open – faced with
404 Not Found
nginx
in nginx container I have only two files error.log
is empty, access.log is filling by each request
/var/www/public # tail -f /var/log/nginx/
access.log error.log
docker compose ps looks like that
ivan@ivan-laptop:~/hosts/test_laravel/docker$ docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------------------------------------------------
docker_nginx-laratest_1 nginx -g daemon off; Up 0.0.0.0:443->443/tcp,:::443->443/tcp, 0.0.0.0:80->80/tcp,:::80->80/tcp
docker_php-laratest_1 bash /usr/local/bin/docker ... Up 9000/tcp
in nginx container I checked path to the app and looks like correct
ivan@ivan-laptop:~/hosts/test_laravel/docker$ docker-compose exec nginx-laratest ash
/ # cd /var/www/public/
/var/www/public # ls
favicon.ico index.php robots.txt web.config
/var/www/public # ls -la
total 24
drwxrwxr-x 2 www-data 1000 4096 Sep 2 08:44 .
drwxrwxr-x 16 www-data 1000 4096 Sep 2 08:10 ..
-rw-rw-r-- 1 www-data 1000 603 Sep 2 07:19 .htaccess
-rw-rw-r-- 1 www-data 1000 0 Sep 2 07:19 favicon.ico
-rw-rw-r-- 1 www-data 1000 1984 Sep 2 08:44 index.php
-rw-rw-r-- 1 www-data 1000 24 Sep 2 07:19 robots.txt
-rw-rw-r-- 1 www-data 1000 1183 Sep 2 07:19 web.config
/var/www/public #
I checked nginx version
/var/www/public # nginx -v
nginx version: nginx/1.20.1
I checked conf file in nginx continer
docker-compose exec nginx-laratest ash
/var/log/nginx # cd /etc/nginx/
conf.d/ http.d/ modules/ sites-enable/
/var/log/nginx # cat /etc/nginx/conf.d/zt.conf
server {
server_name laratest.local.com;
listen 80;
root /var/www/public;
index index.php index.html index.htm;
location / {
try_files $uri /index.php?$args;
}
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass php-laratest:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
access_log /var/log/nginx/php-laratest_service.access.log;
error_log /var/log/nginx/php-laratest_service.error.log crit;
location ~ /.ht {
deny all;
}
}
/var/log/nginx # cat /etc/nginx/s
scgi_params sites-enable/
/var/log/nginx # cat /etc/nginx/s
scgi_params sites-enable/
/var/log/nginx # cat /etc/nginx/sites-enable/zt.conf
server {
server_name laratest.local.com;
listen 80;
root /var/www/public;
index index.php index.html index.htm;
location / {
try_files $uri /index.php?$args;
}
location ~ .php$ {
fastcgi_split_path_info ^(.+.php)(/.+)$;
fastcgi_pass php-laratest:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
access_log /var/log/nginx/php-laratest_service.access.log;
error_log /var/log/nginx/php-laratest_service.error.log crit;
location ~ /.ht {
deny all;
}
}
/var/log/nginx #
2
Answers
I fixed it when changed nginx image to
FROM nginx:stable-alpine
My understanding of
Docker
is little but I think that your problem here is that you forgot to copy thedocker/nginx/zt.conf
file into yournginx-laratest
container. You can verify this by going inside of the docker container and doingls -la /etc/nginx/sites-available
. Ifzt.conf
is not in the list, then you forgot to copy it.To solve the issue you have to map the file from your local machine into the docker container: