8
votes

I'm working with containers in docker
Where I have one from PHP-FPM and another from Nginx.
But I'm having problems with Nginx to serve the static files (css, js)
Return Status Code: 404 Not Found

Nginx configuration

server {
  # Set the port to listen on and the server name
  listen 80;
  listen [::]:80;

  # Set the document root of the project
  root /var/www/html;

  # Set the directory index files
  index index.php;

 #Set server name
 server_name myproject;

  # Specify the default character set
  charset utf-8;

  # Specify the logging configuration
  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  # Specify what happens when PHP files are requested
  location ~* \.php$ {
      #try_files $uri =404;
      #try_files /index.php = 404;
      fastcgi_split_path_info ^(.+\.php)(/.+)$;
      fastcgi_pass    myproject:9000;
      fastcgi_index   index.php;
      include         fastcgi_params;
      fastcgi_param   SCRIPT_FILENAME     $document_root$fastcgi_script_name;
      fastcgi_param   SCRIPT_NAME         $fastcgi_script_name;
      fastcgi_param   PATH_INFO           $fastcgi_path_info;
  }

  location / {
      index index.php;
      try_files $uri $uri/ /index.php;
      include /etc/nginx/mime.types;
  } 

  location ~* \.(jpg|jpeg|png|css|js|ico|xml)$ {
      access_log        off;
      log_not_found     off;
     expires           360d;
     add_header Cache-Control "public";
  }

  # Specify what happens what .ht files are requested
  location ~ /\.ht {
      deny all;
  }
}

PHP Dockerfile

FROM php:7-fpm
RUN docker-php-ext-install pdo_mysql 
COPY . /var/www/html/
EXPOSE 9000

Nginx Dockerfile

FROM nginx:1.12.2
COPY ./default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
1
Do you have docker a volume in both containers to share the files? -v mysharedvolume:/usr/share/nginx/html which is writeable and -v mysharedvolume:/usr/share/nginx/html:ro which is readonly. - dskow
@IgorDuarte You need to add your docker compose file so that we can see what your docker environment looks like. - Bernard
@IgorDuarteSS have you solved this issue? Can you share any results please? - Roma Rush
@dskow if with docker-compose create a volume for php container for example volumename:/var/www - it won't be updated when restart docker compose, do you know how to force re-creating it? - Roma Rush
Not sure of your question in the comment above. The default 'local' driver for volumes are like Unix mount points. The mounted file/folder hides what they mount on top of. If you need the hidden files, you will need to mount to a different location and copy the content to the volume and then mount in the original location. The original files will still be hidden but you will have copies of them. The folder path will be created if it does not exist and you cannot mount a folder on top of a file. - dskow

1 Answers

3
votes

I think the problem, cause service nginx can not find your web project. If you use docker-compose you can use volume, but if not you can add folder project in nginx Dockerfile to /var/www/html

nginx dockerfile

ROM nginx:1.12.2
COPY . /var/www/html/
COPY ./default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80

for docker compose like this :

services:
  nginx:
    images: nginx:latest
    ...
    ...
    volumes:
           - ./:/var/www/html
  php:
    images: php
    ...
    ...
    volumes:
           - ./:/var/www/html