skip to Main Content

I am very new to Nginx and noticed that whenever I hit my server locally it logs. I was wondering, what config files do I need to create (and where do I put them) and what do I have to put into them to disable that behavior (I am trying to prevent spew). I am running my application on aws and am getting a lot of log lines of the form ‘172.31.22.19 – – [23/Jun/2021:23:38:33 +0000] "GET / HTTP/1.1" 200 3022 "-" "ELB-HealthChecker/2.0" "-"’ Is there a way to disable that? Or do I need to disable everything?

My docker file is:

# 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;"]

I can successfully run the above with ‘docker run -p 80:80 my-app’

2

Answers


  1. Chosen as BEST ANSWER

    I fixed the issue by adding a nginx.conf file (see below) and changing the value of access_log to 'off'. I describe the steps I took

    1. get the nginx.conf file: Per What is the 'default' nginx.conf that comes with docker Nginx image? do the following:
    # Create a temporary container
    docker run -d --rm --name mynginx nginx
    
    # Copy the nginx configuration in the container
    docker cp mynginx:/etc/nginx .
    
    1. create nginx.conf file in root of project. Mine was:
    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. Modify Dockefile to (note the '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;"]
    

  2. if you are using a docker run command to run the container then add the flag --log-driver none to the run command

    Looking at your dockerfile youre running node and nginx in a single container. I would advise against this and seperate them into seperate containers using docker-compose

    if you do this then add the line driver: none to the service running the nginx container

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search