Sub topic 10
Using Server Logs and Search Console
This chapter assumes you can read an nginx or Apache access log, use basic shell tools (grep, awk, sort), and have verified ownership of your site in Google Search Console. Crawlers and curl show what your site looks like when you test it. Logs and Search Console show what visitors and search engines actually received.
Why a second data source
A crawler follows links it can reach from a starting page. It misses URLs that only exist in old bookmarks, external sites, emails, or Google's index. Logs and Search Console cover those gaps:
- Server logs record every request your server answered, including the status code, the requested path, the referrer, and the user agent. They are complete for your server, but they only show the past.
- Search Console shows what Google found when it crawled and indexed your pages. It covers only Google, and its reports lag by days.
Used together, they answer three questions: which broken URLs are being requested, who requests them, and which ones matter.
Reading the access log
Nginx and Apache both commonly write the combined log format. A line looks like this:
203.0.113.9 - - [27/Nov/2026:19:41:02 +0000] "GET /old-page HTTP/1.1" 404 153 "https://blog.example.org/post" "Mozilla/5.0 (compatible; Googlebot/2.1)"When you split on whitespace, the fields you need are:
| awk field | Content |
|---|---|
| `$1` | Client IP address |
| `$7` | Request path, including any query string |
| `$9` | Response status code |
The referrer and user agent are in quotes and contain spaces, so use a quote as the field separator when you need them.
Status code overview
Start with the distribution of status codes. A sudden rise in 404s, 301s, or 500s after a deploy is usually visible here first.
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -rnMost requested 404 URLs
This is the core query. The same command appears in the topic's code examples:
awk '$9 == 404 {print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -20A path requested thousands of times is a better target than one requested twice. Many 404s for paths like /wp-login.php or /.env are vulnerability scanners. You do not fix those, so skip them.
Finding who links to the broken URL
The referrer tells you which page sent the visitor. Splitting on " puts the request in field 2, the status and size in field 3, and the referrer in field 4:
awk -F'"' '{split($3, s, " "); if (s[1] == 404) print $2 " <- " $4}' /var/log/nginx/access.log \
| sort | uniq -c | sort -rn | head -20The output shows each broken request next to its referrer. A referrer on your own domain means you can fix the link at its source. A referrer on another domain means you probably need a redirect, or a request to the other site's owner. A referrer of - means the visitor typed the URL, used a bookmark, or the browser withheld it.
Separating crawlers from people
Filter by user agent to see what search engines hit:
grep -i googlebot /var/log/nginx/access.log | awk '$9 == 404 {print $7}' | sort | uniq -c | sort -rn | head -20Redirects in the log
Frequent 301 or 302 hits on the same path mean something still links to the old URL, or the redirect is being followed on every visit. List them like this:
awk '$9 == 301 || $9 == 302 {print $7}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head -20Logs cannot show you a chain directly. Each hop is a separate request, often seconds apart, from the same client. Take any heavily redirected path and trace it with curl to see the full chain.
Log limitations
- Paths with unescaped spaces can shift field numbers. Real requests normally encode spaces as
%20, but malformed requests may not. - Logs rotate. Search
access.log.1and the compressed.gzfiles withzgreporzcatwhen you need history. - A CDN or cache may answer requests before they reach your server. Use the CDN's logs too.
- Custom log formats change the field positions. Check
log_formatin your nginx config orLogFormatin Apache before using the awk examples.
Search Console reports
Search Console groups problems by what Google saw. Open Indexing > Pages to see why pages are not indexed. The entries relevant to this tutorial are:
| Reason | Meaning | Typical action |
|---|---|---|
| Not found (404) | Google requested the URL and got a 404 | Fix the link, redirect it, or leave it if the page is truly gone |
| Soft 404 | The page returned 200 but looks like an error or empty page | Return a real 404 or 410, or restore real content |
| Page with redirect | The URL redirects, so the target is indexed instead | Usually fine, but check for chains |
| Redirect error | A loop, a chain that is too long, or a bad target | Fix the redirect rule |
| Server error (5xx) | The server failed when Google asked | Check server logs for the same time |
| Blocked or "Excluded by noindex" | Not a link problem | Check it is intentional |
Click a reason to see example URLs, then use URL Inspection on one of them. It shows the crawl time, the status Google received, and the canonical URL Google chose. For some URLs it also lists referring pages, but not always, so combine this with your logs.
The Settings > Crawl stats report breaks Googlebot requests down by response. If a large share of crawl requests return 404 or 301, Googlebot is spending effort on dead or moved URLs instead of live ones.
Validating a fix
After you fix a group of URLs, click Validate fix on that reason. Google rechecks the sample and reports passed or failed. This can take days. Do not read a failed validation as proof the fix is wrong. Test the URL yourself with curl first, then compare against the example URLs Google lists.
Combining the two sources
Use the log to find volume and referrers, and Search Console to find what Google cares about. For each candidate URL, collect:
- Hit count in the logs over a fixed window, such as 30 days.
- Whether the requests come from bots, people, or both.
- The referrer, split into internal, external, and none.
- Whether Search Console lists it, and under which reason.
A URL with many human hits and an external referrer is a strong candidate for a 301 redirect. A URL with only bot hits and no referrer is probably low priority. An internal referrer means you should edit the link itself. The next chapters cover how to weigh these and fix them.
Exercise
Your access log shows 4,000 requests for /guides/setup in the last month, all with status 404 and the referrer https://example.com/docs/intro. Search Console lists the URL as "Not found (404)". Which command shows this pattern, and what is the best fix?
Show answer
Run awk -F'"' '{split($3, s, " "); if (s[1] == 404) print $2 " <- " $4}' access.log | sort | uniq -c | sort -rn to see the path and referrer together. The referrer is on your own site, so edit the link on /docs/intro to point at the correct current URL. If the page moved, also add a one-hop 301 from /guides/setup to the new URL, because external links and bookmarks may still use the old one.
Exercise
Why can't you detect a redirect chain by looking at a single log line, and what do you do instead?
Show answer
Each hop in a chain is logged as a separate request with its own 301 or 302 status, so no single line shows the whole path. Find a frequently redirected path from the 301/302 counts, then run curl -sIL on it and read each status line and Location header to see every hop.