Nginx module development-get user ip Address
Nginx module development-get user ip Address
Core code:
if(r->headers_in.x_real_ip != NULL) { ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%V", &r->headers_in.x_real_ip->value); } else { sockaddr_in *sin = (sockaddr_in*)(r->connection->sockaddr); ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "%s", inet_ntoa(sin->sin_addr)); }
Code Testing
Code Description: If the Header contains x-real-ip, it is used as an ip address. Otherwise, the ip address is obtained from the socket. Here, the ip address is simply printed to the error log.
Here, r is the pointer of ngx_http_request_t. Anyone who knows how to develop the basic data structure of the nginx module knows this. If you do not know this, please learn more.
Tucao: I spent a morning looking for how to obtain the ip address of the nginx module. There seems to be a small amount of information on the Internet. I only google to a foreigner and said that I went to the socket in the connection, so nginx cannot encapsulate a ready-made function?
If anyone knows a more simple and crude method, leave a message.
For more Nginx tutorials, see the following:
Deployment of Nginx + MySQL + PHP in CentOS 6.2
Build a WEB server using Nginx
Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5
Performance Tuning for Nginx in CentOS 6.3
Configure Nginx to load the ngx_pagespeed module in CentOS 6.3
Install and configure Nginx + Pcre + php-fpm in CentOS 6.4
Nginx installation and configuration instructions
Nginx log filtering using ngx_log_if does not record specific logs
Nginx details: click here
Nginx: click here
This article permanently updates the link address: