Nginx container for Symfony 4 application with Docker Compose

How to run Nginx container with Docker Compose?

docker-compose.yml

Example container configuration:

services:
    nginx:
        image: nginx:1-alpine
        container_name: nginx
        working_dir: /application
        volumes:
            - .:/application
            - /application/vendor
            - ./.docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
            - ./.docker/nginx/log/:/var/log/nginx/
        env_file:
            - .env
        ports:
            - "80:80"
        depends_on:
          - php
          - mongodb

What does it all mean?

        image: nginx:1-alpine

Specifies an image to use, in this case the nginx:1-alpine, which is based on the popular Alpine Linux project, available in the alpine official image. Alpine Linux is much smaller than most distribution base images (~5MB), and thus leads to much slimmer images in general.

        container_name: nginx

Specifies a custom container name, rather than a generated default name.

        working_dir: /application

Sets the working directory of the container that is created. It is the same as the WORKDIR command in Dockerfile, and as the --workdir flag to docker run.

        volumes:

Mounts host paths or named volumes, specified as sub-options to a service.

In case of the above configuration:

            - .:/application

will mount current local directory (where docker-compose.yml is located) as /application in the container.

            - /application/vendor

will exclude vendor directory from the above mount.

            - ./.docker/nginx/default.conf:/etc/nginx/conf.d/default.conf

will mount local .docker/nginx/default.conf file as /etc/nginx/conf.d/default.conf in the container, allowing easy modifications to nginx configuration.

            - ./.docker/nginx/log/:/var/log/nginx/

will mount local .docker/nginx/log/ directory as /var/log/nginx/ in the container, allowing easy local access to nginx logs.

        env_file:
            - .env

Add environment variables from a file. Can be a single value or a list.

        ports:
            - "80:80"

Port mapping defined in short syntax: HOST:CONTAINER.

80:80 means that port 80 on host will forward all its traffic to port 80 in the container, served by nginx service (see .docker/nginx/default.conf).

        depends_on:
          - php
          - mongodb

Express dependency between services, so that they are started in the dependency order.

.docker/nginx/default.conf

Example Nginx server configuration for a Symfony 4 application:

server {
    listen 80 default;

    client_max_body_size 108M;

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    root /application/public;

    rewrite ^/index\.php/?(.*)$ /$1 permanent;

    try_files $uri @rewriteapp;

    location @rewriteapp {
        rewrite ^(.*)$ /index.php/$1 last;
    }

    # Deny all . files
    location ~ /\. {
        deny all;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass php-fpm:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_index app.php;
        send_timeout 1800;
        fastcgi_read_timeout 1800;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PHP_VALUE "error_log=/var/log/nginx/application_php_errors.log";
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        include fastcgi_params;
    }

    # Statics
    location /(bundles|media) {
        access_log off;
        expires 30d;
        try_files $uri @rewriteapp;
    }
}