【问题标题】:How to disable NGINX logging with file如何使用文件禁用 NGINX 日志记录
【发布时间】:2021-06-24 12:39:37
【问题描述】:

我对 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' 成功运行上述内容

【问题讨论】:

    标签: reactjs docker nginx


    【解决方案1】:

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

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

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

    【讨论】:

    • 嗨@richardsefton 谢谢!我在哪里添加“驱动程序:无”?那是在docker文件中吗?还是别的地方?
    • driver: none 将用于 docker-compose.yaml 文件。但为此,您应该将容器分成 nginx 和节点容器。为容器设置日志驱动程序,因为它在您的 dockerfile 中可能也会停止来自您的节点应用程序的日志。
    • 如果您使用 nginx 进行反向代理,docker-compose 将两个容器联网在一起,以便您可以反向代理到 http://:
    • 纳米。我只是重读了一遍。您正在构建和复制使用节点构建的应用程序并将即时猜测作为静态文件提供服务?只需将--log-driver none 添加到您的 docker run 命令中
    • 我试过 'docker run --log-driver none -p 80:80 myapp' 但我仍然得到以下形式的日志:“GET / HTTP/1.1”304 0“-”“Mozilla/ 5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.106 Safari/537.36" "-"
    【解决方案2】:

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

    1. 获取 nginx.conf 文件: 根据What is the 'default' nginx.conf that comes with docker Nginx image? 执行以下操作:
    # 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;"]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-24
      • 2020-01-17
      • 2016-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多