首页 > 解决方案 > 在docker下的nginx中找不到文件error.log和access.log

问题描述

从 php:8.0.2-fpm 在 docker 中安装 nginx 我找不到我在 nginx.conf 中定义的 error.log 和 access.log 文件:

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;
    }

}

但是进入 bash 我无法打开这些文件并检查我没有看到 /var/log/nginx/ 子目录:

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

在 DOCKER/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

我的配置有问题吗?如何解决?

更新块#2:

我修复了一些无效路径,但正在运行

docker-compose up -d --build

成功后,我在 nginx 日志中发现了一些消息:

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

再次没有 /var/log/nginx 子目录:

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

我有文件 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>

在我的 Dockerfile.yml 结尾,我有:

  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

在 docker-compose.yml 我添加了 /var/log/nginx 路径:

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

我通过 url 工作 phpmyadmin :

http://127.0.0.1:8085/

但试图打开网站

http://127.0.0.1:8084/

我得到错误:

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

我的配置有什么问题?

更新块#3: 我从 DOCKER/Dockerfile.yml 中删除了 VirtualHost 的复制,并在 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;
    }

}

但是检查我看到的日志:

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

看起来那是未创建 default.conf 的关键。也许在一些 docker 配置文件中我必须指向 nginx.conf ?

但它写在我的 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

也进入 bash 我得到了:

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

和 :

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

我尝试在我的托管 Kubuntu 中运行:

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

但是在我的 bash 中,我使用命令输入:

docker-compose exec web bash

怎么了 ?

更新块#4:

我在 docker-compose.yml 中重新制作了 nginx 块:

nginx:图像:nginx:1.19-alpine 容器名称:lar_nginx_nginx 重启:总是端口:-'8081:80'

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

depends_on:
    - web

在 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;
        }

    }

}

但我在日志中看到错误:

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

查看链接https://www.nginx.com/resources/wiki/start/topics/examples/server_blocks/ 和相关示例我没有看到任何结构错误...

谢谢!

标签: dockernginx

解决方案


尝试将您的命名卷映射到您的主机:

 volumes:
      - logs:/var/log/nginx 

并检查访问和错误文件是否可用!


推荐阅读