skip to Main Content

Installing nginx in docker from php:8.0.2-fpm I could not find error.log and access.log files, which I defined in nginx.conf :

server {
    listen 80;
    index index.php;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    error_page 404 /index.php;

    root /var/www/lar-nginx_docker_root/public;

    location ~ .php$ {
        try_files $uri $uri/ =404;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location  / { # catch any non php files
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }

}

But entering in bash I can not opened these files and checking I do not see /var/log/nginx/ subdirectory:

master@master-laptop:/mnt/_work_sdb8/wwwroot/lar/lar-nginx/DOCKER$ docker-compose exec web bash
root@7f85f7225617:/var/www/lar-nginx_docker_root# cat /var/log/nginx/error.log
cat: /var/log/nginx/error.log: No such file or directory
root@7f85f7225617:/var/www/lar-nginx_docker_root# cat /var/log/nginx/access.log
cat: /var/log/nginx/access.log: No such file or directory
root@7f85f7225617:/var/www/lar-nginx_docker_root# cd /var/log                 
root@7f85f7225617:/var/log# ls -la
total 152
drwxr-xr-x 1 root root  4096 Feb  9  2021 .
drwxr-xr-x 1 root root  4096 Feb  9  2021 ..
-rw-r--r-- 1 root root  5883 Sep 26 07:52 alternatives.log
drwxr-xr-x 1 root root  4096 Sep 26 07:52 apt
-rw-rw---- 1 root utmp     0 Feb  8  2021 btmp
-rw-r--r-- 1 root root 93784 Sep 26 07:52 dpkg.log
-rw-r--r-- 1 root root  3232 Feb  8  2021 faillog
-rw-rw-r-- 1 root utmp 29492 Feb  8  2021 lastlog
-rw-rw-r-- 1 root utmp     0 Feb  8  2021 wtmp

In DOCKER/docker-compose.yml I have :

    nginx:
        image: nginx:1.19-alpine
        container_name: lar_nginx_nginx
        # restart: always
        ports:
            - '8084:80'

        volumes:
            - ../:/var/www/lar-nginx_docker_root
            - ./nginx:/etc/nginx/conf.d

Is something is wrong in my configurations and how it can be fixed?

UPDATED BLOCK # 2 :

I fixed some invalid paths I had, but running

docker-compose up -d --build

with success I found some messages in nginx logs :

master@master-laptop:/mnt/_work_sdb8/wwwroot/lar/lar-nginx/DOCKER$ docker logs --tail=50  lar_nginx_nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
nginx: [emerg] host not found in upstream "app" in /etc/nginx/conf.d/nginx.conf:12

and again no /var/log/nginx subdirectory :

master@master-laptop:/mnt/_work_sdb8/wwwroot/lar/lar-nginx/DOCKER$ docker-compose exec web bash
root@5f94882294c0:/var/www/LAR-NGINX_docker_root# cat /var/log/nginx/error.log
cat: /var/log/nginx/error.log: No such file or directory
root@5f94882294c0:/var/www/LAR-NGINX_docker_root# cat /var/log/nginx/access.log
cat: /var/log/nginx/access.log: No such file or directory
root@5f94882294c0:/var/www/LAR-NGINX_docker_root# cd /var/log
root@5f94882294c0:/var/log# ls -la
total 152
drwxr-xr-x 1 root root  4096 Feb  9  2021 .
drwxr-xr-x 1 root root  4096 Feb  9  2021 ..
-rw-r--r-- 1 root root  6326 Sep 27 07:40 alternatives.log
drwxr-xr-x 1 root root  4096 Sep 27 07:40 apt
-rw-rw---- 1 root utmp     0 Feb  8  2021 btmp
-rw-r--r-- 1 root root 96133 Sep 27 07:40 dpkg.log
-rw-r--r-- 1 root root  3232 Feb  8  2021 faillog
-rw-rw-r-- 1 root utmp 29492 Feb  8  2021 lastlog
-rw-rw-r-- 1 root utmp     0 Feb  8  2021 wtmp

I have file virtualhost.conf :

<VirtualHost *:80>

  DocumentRoot /var/www/LAR-NGINX_docker_root/public
  <Directory /var/www/LAR-NGINX_docker_root/public>
    Order allow,deny
    Allow from all

    Options -MultiViews

    RewriteEngine On

    # Redirect Trailing Slashes...
    RewriteRule ^(.*)/$ /$1 [L,R=301]


    php_value  memory_limit  4048M
    php_value  file_uploads  On
    php_value  upload_max_filesize  200M
    php_value  post_max_size  200M
    php_value  max_execution_time  1000
    php_value  short_open_tag  On


    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
  </Directory>

  ErrorLog /var/log/nginx/error.log

  # Possible values include: debug, info, notice, warn, error, crit,
  # alert, emerg.
  LogLevel warn

</VirtualHost>

and in the end of my Dockerfile.yml I have :

  RUN  docker-php-ext-install gd pdo pdo_mysql pdo_sqlite zip gmp bcmath pcntl sysvmsg exif

  COPY virtualhost.conf /etc/nginx/conf.d/default.conf

  WORKDIR  /var/www/LAR-NGINX_docker_root

and in docker-compose.yml I added /var/log/nginx path :

version: '3.3'

services:

    web:
        build:
            context: ./           # directory of web/Dockerfile.yml
            dockerfile: Dockerfile.yml


        container_name: lar_nginx_web
        # restart: always
        working_dir: /var/www/LAR-NGINX_docker_root/
        volumes:
            - ../:/var/www/LAR-NGINX_docker_root


    nginx:
        image: nginx:1.19-alpine
        container_name: lar_nginx_nginx
        # restart: always
        ports:
            - '8084:80'

        volumes:
            - ../:/var/www/LAR-NGINX_docker_root
            - ./nginx:/etc/nginx/conf.d
            - ./log/nginx:/var/log/nginx

        depends_on:
            - web



    db:
        container_name: lar_nginx_db
        image: mysql:5.7.28
        # image: mysql:8.0.21
        # restart: always
        environment:
            - MYSQL_DATABASE=DockerLarNginx
            - MYSQL_USER=docker_user
            - MYSQL_PASSWORD=4321
            - MYSQL_ALLOW_EMPTY_PASSWORD=false
            - MYSQL_ROOT_PASSWORD=321
            - TZ=Europe/Kiev

        volumes:
            - /var/lib/mysql


    phpmyadmin:
        container_name: lar_nginx_phpmyadmin
        depends_on:
            - db
        image: phpmyadmin/phpmyadmin
        # restart: always
        ports:
            - 8085:80
        environment:
            PMA_HOST: db
            MYSQL_ROOT_PASSWORD: 1

    composer:
        image: composer:2.1
        container_name: lar_nginx_composer
        volumes:
            - ./:/var/www/LAR-NGINX_docker_root
        working_dir: /var/www/LAR-NGINX_docker_root
        command: composer install  --ignore-platform-reqs

and I have working phpmyadmin by url :

http://127.0.0.1:8085/

But trying to open site

http://127.0.0.1:8084/

I got error :

This site can’t be reached
127.0.0.1 refused to connect.

What is wrong in my configurations ?

UPDATED BLOCK # 3 :
I removed copying of VirtualHost from DOCKER/Dockerfile.yml and added several rows in nginx.conf :

http {
    client_max_body_size 20M;
    memory_limit    4048M;
    file_uploads    On
    upload_max_filesize  200M
    post_max_size   200M
    max_execution_time  1000
    short_open_tag  On
}

server {
    listen 80;
    index index.php;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    error_page 404 /index.php;

    root /var/www/LAR-NGINX_docker_root/public;

    location ~ .php$ {
        try_files $uri $uri/ =404;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location  / { # catch any non php files
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }

}

But checking logs I see :

master@master-laptop:/mnt/_work_sdb8/wwwroot/lar/lar-nginx/DOCKER$ docker logs --tail=50  lar_nginx_nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf is not a file or does not exist

/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/nginx.conf:1

Looks like that is the point that default.conf is not created. Maybe in some docker config file I have
to point to nginx.conf ?

But it is written in my docker-compose.yml :

   nginx:
        image: nginx:1.19-alpine
        container_name: lar_nginx_nginx
        # restart: always
        ports:
            - '8084:80'

        volumes:
            - ../:/var/www/lar-nginx_docker_root
            - ./nginx:/etc/nginx/conf.d

Also entering into bash I got :

root@11ed27f97ac4:/var/log# uname -a
Linux 11ed27f97ac4 5.11.0-36-generic #40~20.04.1-Ubuntu SMP Sat Sep 18 02:14:19 UTC 2021 x86_64 GNU/Linux
root@11ed27f97ac4:/var/log# ls -l /etc/nginx/
ls: cannot access '/etc/nginx/': No such file or directory

and :

master@master-laptop:/mnt/_work_sdb8/wwwroot/lar/lar-nginx/DOCKER$ ls -l /var/log/nginx
ls: cannot access '/var/log/nginx': No such file or directory

I try to run in my hosting Kubuntu :

docker exec -it lar_nginx_nginx sh
Error response from daemon: Container fc8de90773b28e205f1471440f8f997b8fa5e462a390b9e0eb7b1aba0cfb9047 is not running

But in my bash I enter with command :

docker-compose exec web bash

What is wrong ?

UPDATED BLOCK # 4 :

I remade nginx block in docker-compose.yml:

nginx:
image: nginx:1.19-alpine
container_name: lar_nginx_nginx
restart: always
ports:
– ‘8081:80’

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

depends_on:
    - web

and in default.conf :

http {

    index index.php;
    client_max_body_size 20M;
    memory_limit    4048M;
    file_uploads    On;
    upload_max_filesize  200M;
    post_max_size   200M;
    max_execution_time  1000;
    short_open_tag  On;

    server {
        listen 80;
        error_log /var/log/nginx/error.log;
        access_log /var/log/nginx/access.log;
        error_page 404 /index.php;

        root /var/www/LAR-NGINX_docker_root/public;

        location ~ .php$ {
            try_files $uri $uri/ =404;
            fastcgi_pass lar_nginx_web:8081;
            fastcgi_index index.php;
            fastcgi_pass app:8081;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        location  / { # catch any non php files
            try_files $uri $uri/ /index.php?$query_string;
            gzip_static on;
        }

    }

}

But I see errors in log :

master@master-laptop:/_wwwroot/lar/lar-nginx/DOCKER$ docker logs --tail=50  lar_nginx_nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/10/01 13:24:26 [emerg] 1#1: "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/10/01 13:25:27 [emerg] 1#1: "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/10/01 13:26:27 [emerg] 1#1: "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/10/01 13:27:28 [emerg] 1#1: "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: /etc/nginx/conf.d/default.conf differs from the packaged version
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/10/01 13:28:29 [emerg] 1#1: "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1
nginx: [emerg] "http" directive is not allowed here in /etc/nginx/conf.d/default.conf:1

looking at link https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/
and relared examples I do not see any structure errors…

Thanks!

2

Answers


  1. Try to map your named volume onto your host:

     volumes:
          - logs:/var/log/nginx 
    

    And check if the access & error files are available!

    Login or Signup to reply.
  2. Block 1

    You check /var/log/nginx in lar_nginx_web rather than the nginx container which is lar_nginx_nginx.
    You see the logs when you run docker logs --tail=50 lar_nginx_nginx because you use the correct container name.

    If you docker exec -it lar_nginx_nginx sh and ls -l /var/log/nginx you should see the log files.

    Block 2

    I don’t think you need VirtualHost since it is for Apache, not Nginx.

    You should use server block instead for Nginx config.
    Or if you’re looking for php-fpm configuration then you should use php.ini/php-fpm.conf configuration

    Block 3

    Why you don’t have default.conf

    Docker volume mount always takes host volume as higher precedence. So by doing ./nginx:/etc/nginx/conf.d you’re replacing the whole directory /etc/nginx/conf.d with ./nginx, that’s why you can’t find default.conf that comes with the image.

    So if you would like to preserve the files in nginx/conf.d that comes with the image, you should mount your specific files inside like below :

    volumes:
        - ./nginx/my.conf:/etc/nginx/conf.d/my.conf
    
    

    I also spotted some misconfiguration in your nginx.conf that points to your php-fpm

    location ~ .php$ {
            try_files $uri $uri/ =404;
            fastcgi_pass lar_nginx_web:9000; <------- Please make sure you use  hostname that is resolved to your php-fpm here.
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    

    Why sometimes bash and sometimes sh?

    For sh and bash questions, you can use whatever is installed on your docker image. They’re different type of shell. If your image has sh intalled, then you can use docker exec -it container_name sh.
    If your image doesn’t have bash install then you can’t use bash

    Visit the link below for different type of shells:

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search