Chinaunix首页 | 论坛 | 博客
  • 博客访问: 142078
  • 博文数量: 37
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 352
  • 用 户 组: 普通用户
  • 注册时间: 2012-12-08 10:56
文章存档

2015年(18)

2014年(6)

2013年(13)

我的朋友

分类: LINUX

2015-11-21 19:48:04

首先,需要在nginx的配置文件nginx.conf下添加如下配置:
server {

        listen       80;
       server_name  localhost;

        location /{

                         root   html;
                         index  index.html index.htm;

                         proxy_set_header            X-real-ip $remote_addr;                                           # 可直接获取客户端IP

                         proxy_set_header            X-Forwarded-For $proxy_add_x_forwarded_for;    # 通过代理服务器获取客户端IP

        }
-----------------------------------------------------------------------------
创建Client_ip.lua 程序,通过读取ngx.var.remote_addr 值,即可获得IP值。
local ClientIP = ngx.req.get_headers()["X-real-ip"]
ngx.say("IP:",ClientIP)
ngx.say(type(ClientIP))
if ClientIP ==nil then
   ClientIP = ngx.req.get_headers()["X-Forwarded-For"]
   ngx.say("IP:",ClientIP)
   ngx.say(type(ClientIP))
end
local ip_addr = tostring(ngx.var.remote_addr)
--if ClientIP ==nil then
--   ClientIP = ngx.var.remote.addr
--end
ngx.say("IP:",ClientIP)
ngx.say("IP:",ip_addr)

阅读(5790) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~