9

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
7
  • 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. Commented Dec 18, 2017 at 20:49
  • @IgorDuarte You need to add your docker compose file so that we can see what your docker environment looks like. Commented Dec 19, 2017 at 10:30
  • @IgorDuarteSS have you solved this issue? Can you share any results please? Commented Aug 20, 2018 at 16:03
  • @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? Commented Aug 20, 2018 at 16:05
  • 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. Commented Aug 21, 2018 at 18:31

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

There is no need to COPY the contents of . to /var/www/html/, this will include all those files in the image of your nginx, and you will have duplicates in 2 images. Ideally use the volumes approach. Although I dont like to share state, there is no way around when using wordpress where static and dynamic files are mixed up.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.