Introduction#
When using a CDN, the logs in Nginx are filled with CDN node IPs, which is inconvenient for debugging and other purposes.
To solve this problem, we can customize the Log Format in the Nginx configuration file to display the visitor's real IP in the logs.
Get Started#
First, you need to determine which item in the response header of your CDN contains the user's real IP. Please refer to the previous article for details. Then, open the main Nginx configuration file and find the http{} section. Insert the following content wherever you like.
log_format main '$http_cf_connecting_ip / $remote_addr - [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent"';
In this code snippet, main represents the name of the custom format. Replace http_cf_connecting_ip with the header of your CDN that contains the user's real IP, with http_ prefix. The logs will be output in the following format:
User IP / Node IP - [Access Time] "Request Method and Directory" Response Code Size of Data Packets Sent Referring URL User Agent
Then, find the http{} section of the site configuration. Find the access_log
parameter you had before and add main
before the semicolon.
Afterwards, the logs of this site will be output in your custom format.
The End#
This solves the problem. If you find this article helpful, please give it a thumbs up, consider supporting, and leave your comments in the comment section meow~
This article is synchronized with xLog by Mix Space. The original link is https://blog.nekorua.com/posts/maintain/16.html