看不到 nginx 日志

unable to see nginx logs

我试图确认我的日志是 json 格式,但我什至看不到一个日志。我正在使用 docker-compose

version: '3'
services:
  nginx:
    image: test_site
    volumes:
      - /Users/mikeJ/Desktop/test-logs/access:/tmp/logs/access
      - /Users/mikeJ/Desktop/test-logs/error:/tmp/logs/error
    build:
      context: .
    restart: unless-stopped      
    ports:
      - "8040:8040"

ngnix.conf

worker_processes 1;

events { worker_connections 1024; }

http {
  include    mime.types;
  sendfile on;
  access_log on;

 log_format json_combined escape=json
   '{'
     '"time_local":"$time_local",'
     '"remote_addr":"$remote_addr",'
     '"remote_user":"$remote_user",'
     '"request":"$request",'
     '"status": "$status",'
     '"body_bytes_sent":"$body_bytes_sent",'
     '"request_time":"$request_time",'
     '"http_referrer":"$http_referer",'
     '"http_user_agent":"$http_user_agent"'
   '}';

  server {
    listen 8040;

    error_log /tmp/logs/error/error.log warn;
    access_log /tmp/logs/access/access.log;

    server_name localhost;
    location /{
      root /usr/share/nginx/html/;
      index index.html;
    }

  location ~ ^/test/footer {
    root /usr/share/nginx/html/;
    expires 5m;
    access_log on;
}

}

码头文件

FROM nginx:1.15.0-alpine

RUN rm -v /etc/nginx/nginx.conf

# Copying nginx configuration file
ADD nginx.conf /etc/nginx/

# setup nginx caching
RUN mkdir -p /tmp/nginx/cache

#create directory for logs
RUN mkdir -p /tmp/logs/error
RUN mkdir -p /tmp/logs/access

#adding footer file
ADD footer /usr/share/nginx/html/footer

# Expose ports
EXPOSE 8040

我什至ssh进了容器,里面什么也没有。

从容器内部

 # ps aux | grep nginx
    1 root       0:00 nginx: master process nginx -g daemon off;
    7 nginx      0:00 nginx: worker process

能否确认 nginx.conf 是否正确? 好像是nginx进程没有权限写入创建的目录。

ps -eo "%U %G %a" | grep nginx 

运行上面的命令学习用户。在你的情况下是 nginx 。 更改日志目录的所有者和组并重新加载 nginx 服务。

#create directory for logs
RUN mkdir -p /tmp/logs/error
RUN mkdir -p /tmp/logs/access && \
    chown -R nginx:nginx /tmp/logs/

#adding footer file
ADD footer /usr/share/nginx/html/footer

检查日志文件夹 post 访问您的 URL 之一。