Bonanza
Email
Enterprise Service
menu
Email
Enterprise Service
Submit
Basic information
Waiting for a reply
Your form has been submitted. We'll contact you in 24 hours.
Close
Home/ Blog/ How to Log Client IP in Old Boy Nginx Proxy

How to Log Client IP in Old Boy Nginx Proxy

Author:PYPROXY
2024-10-28 15:56:03

How to Log Client IP in Old Boy Nginx Proxy


In modern Internet architecture, Nginx is widely used as a high-performance reverse proxy server. However, when Nginx is used as a proxy server, by default, it logs the IP address of the proxy server instead of the real IP of the client. This may cause problems in some cases, especially when access logs need to be analyzed. This article will detail how to properly log client IP in Old Boy Nginx Proxy, and provide practical configuration steps and tips.


1. What is Nginx?

Nginx is a high-performance HTTP and reverse proxy server, and can also be used as an IMAP/POP3 proxy server. Due to its efficient processing power and low resource consumption, Nginx is widely used in scenarios such as website load balancing, static content serving, and dynamic content processing.


2. Why do you need to log client IP?

There are several important reasons for logging client IP:

- Security: By logging the real client IP, it can help administrators identify potential malicious access behaviors.

- Traffic Analysis: Analyzing the client IP in the access log can provide valuable information about user behavior and traffic sources.

- Troubleshooting: When a problem occurs, being able to identify the real IP of the visitor helps to quickly locate the problem.


3. How does Nginx log IP by default?

In the default configuration of Nginx, the access log usually records the IP address of the Nginx server instead of the real IP of the client. This is because when Nginx is used as a reverse proxy, the client request will pass through Nginx, and Nginx will forward the request to the backend server.

3.1 Nginx access log format

The default access log format of Nginx is as follows:

```nginx

log_format main '$remote_addr - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

```

In this format, `$remote_addr` represents the client IP connected to Nginx, but in the case of a proxy, this IP may not be the client's real IP.


4. How to configure Nginx to record client IP

To ensure that Nginx can correctly record the client's real IP, it is usually necessary to use the `X-Forwarded-For` or `X-Real-IP` header. The following are the detailed configuration steps:

4.1 Configure the backend server

First, make sure the backend server can recognize and process the `X-Forwarded-For` header. Most modern web servers (such as Apache, Tomcat, etc.) can automatically process this header.

4.2 Modify the Nginx configuration file

1. Open the Nginx configuration file:

The Nginx configuration file is usually located in `/etc/nginx/nginx.conf` or `/etc/nginx/conf.d/default.conf`.

2. Set `proxy_set_header`:

In the `location` block of Nginx, add the following configuration:

```nginx

location / {

proxy_pass http://backend_server;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

```

- `proxy_set_header X-Real-IP $remote_addr;`: Set the client's real IP to the `X-Real-IP` header.

- `proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;`: Add the client's IP to the `X-Forwarded-For` header.

3. Configure access log format:

Modify the access log format to record the real client IP. The access log format can be modified to:

```nginx

log_format main '$http_x_real_ip - $remote_user [$time_local] "$request" '

'$status $body_bytes_sent "$http_referer" '

'"$http_user_agent" "$http_x_forwarded_for"';

```

In this way, the value of the `X-Real-IP` header will be recorded in the access log.

4.3 Restart Nginx

After completing the configuration, restart Nginx to make the changes take effect:

```bash

sudo systemctl restart nginx

```


5. Verify the configuration

To verify whether Nginx successfully records the client IP, you can perform the following test:

1. Access log file:

Nginx's access log is usually located in `/var/log/nginx/access.log`. You can use the following command to view the log:

```bash

tail -f /var/log/nginx/access.log

```

2. Send a test request:

Use the curl command to send a request and check whether the real client IP is recorded in the log:

```bash

curl -H "X-Forwarded-For: 192.168.1.100" http://your-nginx-server

```

Check the access log to confirm whether `192.168.1.100` is recorded as the client IP.


6. Common problems and solutions

6.1 Access log does not record the client IP

If the client IP is still not recorded in the access log, check the following:

- Nginx configuration file: Make sure the `proxy_set_header` directive is configured correctly.

- Backend server settings: Make sure the backend server can handle the `X-Real-IP` and `X-Forwarded-For` headers.

 6.2 IP Recording in Proxy Chain

In a complex proxy chain, there may be multiple `X-Forwarded-For` headers. You can use the following configuration to ensure that all IPs are recorded:

```nginx

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

```

In this way, all passing IPs will be recorded.


Properly recording client IP in the old boy Nginx proxy is an important step to ensure security and traffic analysis. With the detailed configuration guide provided in this article, you can easily achieve this goal. If you encounter problems during the configuration process, please refer to relevant materials or seek help. I hope this article can provide you with valuable information to help you better use the Nginx proxy server.