如何使用文件禁用 NGINX 日志记录

IT技术 reactjs docker nginx
2021-05-24 06:07:41

我对 Nginx 非常陌生,并注意到每当我在本地访问服务器时,它都会记录。我想知道,我需要创建哪些配置文件(以及我将它们放在哪里)以及我必须将哪些内容放入其中才能禁用该行为(我试图防止溢出)。我在 aws 上运行我的应用程序,并收到了很多格式为 '172.31.22.19 - - [23/Jun/2021:23:38:33 +0000] "GET / HTTP/1.1" 200 3022 "- " "ELB-HealthChecker/2.0" "-"' 有没有办法禁用它?还是我需要禁用所有内容?

我的 docker 文件是:

# pull official base image
FROM node:16 AS builder

# set working directory
WORKDIR /app


# install app dependencies
#copies package.json and package-lock.json to Docker environment
COPY package.json ./

# Installs all node packages
RUN npm install


# Copies everything over to Docker environment
COPY . ./
RUN npm run build

#Stage 2
#######################################
#pull the official nginx:1.19.0 base image
FROM nginx:1.19.0
#copies React to the container directory
# Set working directory to nginx resources directory
WORKDIR /usr/share/nginx/html
# Remove default nginx static resources
RUN rm -rf ./*
# Copies static resources from builder stage
COPY --from=builder /app/build .
EXPOSE 80
# Containers run nginx with global directives and daemon off
ENTRYPOINT ["nginx", "-g", "daemon off;"]

我可以使用 'docker run -p 80:80 my-app' 成功运行上述内容

2个回答

如果您使用 docker run 命令来运行容器,则将标志添​​加--log-driver none到 run 命令

查看您的 dockerfile 您正在单个容器中运行节点和 nginx。我建议不要这样做,并使用 docker-compose 将它们分成单独的容器

如果您这样做,则将该行添加driver: none到运行 nginx 容器的服务中

我通过添加 nginx.conf 文件(见下文)并将 access_log 的值更改为“关闭”来解决该问题。我描述了我采取的步骤

  1. 获取 nginx.conf 文件:Per docker Nginx 映像附带的“默认” nginx.conf 是什么?请执行下列操作:
# Create a temporary container
docker run -d --rm --name mynginx nginx

# Copy the nginx configuration in the container
docker cp mynginx:/etc/nginx .
  1. 在项目的根目录中创建 nginx.conf 文件。我的是:
user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


    access_log  off;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
  1. 将 Dockefile 修改为(注意“COPY nginx.conf /etc/nginx/nginx.conf”):
# pull official base image
FROM node:16 AS builder

# set working directory
WORKDIR /app


# install app dependencies
#copies package.json and package-lock.json to Docker environment
COPY package.json ./

# Installs all node packages
RUN npm install


# Copies everything over to Docker environment
COPY . ./
RUN npm run build

#Stage 2
#######################################
#pull the official nginx:1.19.0 base image
FROM nginx:1.19.0
COPY nginx.conf /etc/nginx/nginx.conf
#copies React to the container directory
# Set working directory to nginx resources directory
WORKDIR /usr/share/nginx/html
# Remove default nginx static resources
RUN rm -rf ./*
# Copies static resources from builder stage
COPY --from=builder /app/build .
EXPOSE 80
# Containers run nginx with global directives and daemon off
ENTRYPOINT ["nginx", "-g", "daemon off;"]