Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

nginx

Configuration

# Test/verify configuration.
sudo nginx -t

# View base configuration.
sudo cat /etc/nginx/nginx.conf

# View default site configuration.
sudo cat /etc/nginx/sites-enabled/default

# List enabled sites.
ls -l /etc/nginx/sites-enabled/

# List available sites.
ls /etc/nginx/sites-available/

# Enable site via a symbolic link.
sudo ln -s /etc/nginx/sites-available/SITE_CONFIG_FILE_NAME /etc/nginx/sites-enabled/

# Remove symbolic link/enabled site.
sudo rm SITE_CONFIG_FILE_NAME
sudo unlink SITE_CONFIG_FILE_NAME
# Remove with confirmation.
sudo rm -i SITE_CONFIG_FILE_NAME

Logging

# View access logs.
sudo cat /var/log/nginx/access.log

# View last 20 lines of the error log.
sudo tail -20 /var/log/nginx/error.log

# Get top 20 most used user agents.
sudo cat /var/log/nginx/access.log | awk -F\" '{print $6}' | sort | uniq -c | sort -nr | head -20

# Get top 20 most used client IPs.
sudo cat /var/log/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -nr | head -20

# Get requests by status code.
sudo awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -rn

# Get 50 most 200 requested URLs.
sudo awk '($9 ~ /200/)' /var/log/nginx/access.log | awk '{print $7}' | sort | uniq -c | sort -rn | head -50

# Get 50 most 200 user agents.
sudo awk '($9 ~ /200/)' /var/log/nginx/access.log | awk -F\" '{print $6}' | sort | uniq -c | sort -rn | head -50

# Get 50 most 200 IPs.
sudo awk '($9 ~ /200/)' /var/log/nginx/access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -50

Management

# Restart nginx.
sudo systemctl restart nginx