Home > Software design >  In nginx under docker can not find files error.log and access.log
In nginx under docker can not find files error.log and access.log

Time:10-01

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 ?

Thanks!

CodePudding user response:

Try to map your named volume onto your host:

 volumes:
      - logs:/var/log/nginx 

And check if the access & error files are available!

CodePudding user response:

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

  • Related