Link Search Menu Expand Document
Table of contents

Enable log on location

server {
    listen 80;
    server_name 198.255.25.1;
    root /sites/demo; 
    
    location /apple.png {
        access_log /var/log/nginx/secure.access.log;
        access_log /var/log/nginx/access.log;
        # request will be logged in 2 locations
    }

    location /orange.png {
        log_not_found off; 
        access_log off; 
        # no log for this location 
    }
}

Useful reverse proxy upstream logs

  • Step 1: Run vim /etc/nginx/nginx.conf, add below log_format inside http { … }
http {
    log_format upstream_time '$remote_addr - $remote_user [$time_local]'
    '"$request" $status $body_bytes_sent'
    '"$http_referer" "$http_user_agent"'
    'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';
}
  • Step 2: Modify vim /etc/nginx/sites-available
access_log /var/log/nginx/geekyflare-access.log upstream_time;
    # add `upstream_time` at the end
error_log /var/log/nginx/geekyflare-error.log;

Check and grepping status code

cat example.com-access.log | cut -d '"' -f3 | cut -d ' ' -f2 | sort | uniq -c | sort -rn
    # outputs: 
    # 124 200
    #  30 301
    #   5 404
    #   1 502