Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serve static files using docker, nginx, php-fpm

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
like image 704
IgorDuarteSS Avatar asked Dec 18 '17 17:12

IgorDuarteSS


People also ask

How do I serve a static file in Nginx Docker?

Serving Static Files To deploy the container, use Docker Compose. The Docker Compose output. Your static folder and all of its contents are now being served at http://localhost:8080/ using Nginx running inside Docker. Our static files being served on port 8080.

Can Nginx serve static content?

Configure NGINX and NGINX Plus to serve static content, with type-specific root directories, checks for file existence, and performance optimizations.

Can Nginx serve PHP files?

After the restart, PHP is fully enabled on Nginx. To prove this, create a PHP file in Nginx's /var/www/html folder and test to ensure the page renders properly on the server.


1 Answers

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
like image 63
Muh faris Avatar answered Oct 19 '22 23:10

Muh faris