banner
libxcnya.so

libxcnya.so

Nothing...
telegram
twitter
github
email

Nginx Custom Log Format

Preface#

When using a CDN, the logs in Nginx are filled with CDN node IPs, which makes it inconvenient for us to debug.

1

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 Start#

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 locate the http{} section. Insert the following content wherever you like:

2

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 from your CDN that contains the user's real IP, make sure to include http_, and the logs will be output in the following format:

User IP / Node IP - [Access Time] "Request Method and Directory" Response Code Bytes Sent Referrer URL User Agent

Next, locate the site-specific configuration in the http{} section. Find the access_log parameter you had before and add main before the semicolon.

3

After this, the logs for that site will be output in your custom format.

4

The End#

This way, the problem is solved. If you found this article helpful, feel free to give it a thumbs up, consider a small donation, and share your thoughts in the comments section.

This article is synchronized updated by Mix Space to xLog. The original link is https://blog.nekorua.com/posts/maintain/16.html

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.