Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save codedeep79/4b5bf55e8350e742064f1ccecf712596 to your computer and use it in GitHub Desktop.

Select an option

Save codedeep79/4b5bf55e8350e742064f1ccecf712596 to your computer and use it in GitHub Desktop.
Phân tích nhật ký truy cập (log) của Nginx

If you want to optimize your web server, it is essential to understand Nginx access logs. Nginx access logs comprise detailed information about the access requests of the users. Your Nginx access logs are saved in the "/var/log" directory, executing the below-given command will parse Nginx access logs to retrieve a list of the IP addresses of the users that have been accessed in the log file:

sudo cat /var/log/nginx/access.log | awk '{ print $1}' | sort | uniq -c | sort

Parse Nginx access logs for getting accessed file list:

sudo cat /var/log/nginx/access.log | awk '{ print $7}' | sort | uniq -c | sort

Parse Nginx access logs for counting requests per second:

sudo cat /var/log/nginx/access.log | awk '{print $4}' | uniq -c | sort -rn | head

Parse Nginx access logs for getting response codes:

sudo cat /var/log/nginx/access.log | cut -d '"' -f3 | cut -d ' ' -f2 | sort | uniq -c | sort -rn

Parse Nginx access logs using online analyzer tools: To use Goaccess for parsing Nginx access logs, you have to install it first on your system: sudo apt install goaccess Goaccess comprises a real-time monitoring feature as well as an interactive terminal viewer. Goaccess generates metrics after every 200 milliseconds. Instead of manually checking the Nginx access log for finding the issue, you can utilize the most critical parsing information of Nginx access logs by executing this command: goaccess /var/log/nginx/access.log

Now, choose a log format and enter:

  • Log Format: %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
  • Date Format: %d/%b/%Y
  • Time Format: %H:%M:%S
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment