nginx return file not found with nginx-proxy and doesn't load static files
In order to serve the static files from a container using the nginx proxy, the nginx proxy must have access to files statically... it would not 'proxy' but 'read' and serve the files... which means you must mount the app files into the proxy...
I would recommend you put up a new, light and clean nginx behind the nginx proxy along with your php-fpm service so you can have full management of your routes/locations,
First of all I have created a docker-compose.yml
in my app that contains the following:
version: '3.9'services:php-fpm: container_name: boilerplate_app restart: always build: context: . dockerfile: ./docker/php-fpm/Dockerfile volumes: - ./src:/var/www/htmlnginx: image: nginx:stable-alpine container_name: boilerplate_nginx restart: always volumes: - ./src:/var/www/html - ./docker/nginx/nginx.conf:/etc/nginx/nginx.conf - ./docker/nginx/sites/:/etc/nginx/sites-available - ./docker/nginx/conf.d/:/etc/nginx/conf.d depends_on: - php-fpm environment: VIRTUAL_HOST: example.com LETSENCRYPT_HOST: example.com LETSENCRYPT_EMAIL: info@example.com database: container_name: boilerplate_db restart: always build: context: ./docker/database environment: - MYSQL_DATABASE=boilerplate - MYSQL_USER=user - MYSQL_PASSWORD=secret - MYSQL_ROOT_PASSWORD=secret volumes: - ./docker/database/data.sql:/docker-entrypoint-initdb.d/data.sqlphpmyadmin: container_name: boilerplate_phpmyadmin image: phpmyadmin/phpmyadmin restart: always ports: - 8088:80 environment: - PMA_HOST=database - MYSQL_USER=user - MYSQL_PASSWORD=secret - MYSQL_ROOT_PASSWORD=secret depends_on: - database networks:default: external: name: proxy
then, inside my app directory I have created the nginx configurations structure:
app docker nginx conf.d default.conf sites default.conf nginx.conf
where I have conf.d
:
upstream php-upstream { server php-fpm:9000;}
then I have sites
:
server { root /var/www/html/public; index index.php; location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_pass php-upstream; fastcgi_index index.php; }}
and nginx.conf
:
user nginx;worker_processes 4;error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;events { worker_connections 1024;}http { include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; # Switch logging to console out to view via Docker #access_log /dev/stdout; #error_log /dev/stderr; sendfile on; keepalive_timeout 65; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-available/*.conf;}
the site load now but there are some issues:
- Performance issue: we have two nginx layer here and the load time is increased a lot
- If I browse some url like example.com/admin I get 404 from nginx, guess it's a configuration issue on my own