获取openresty所在的当前服务器的IP和域名

2,373 views
Skip to first unread message

王程

unread,
Dec 2, 2015, 4:23:35 AM12/2/15
to openresty
大家好:
   请问在openresty 或者 nginx中是否有获取本机IP和域名的方法?求告知

YuanSheng Wang

unread,
Dec 2, 2015, 4:39:19 AM12/2/15
to open...@googlegroups.com
可以通过 LuaJIT 的 FFI 方式调用本地 C 函数完成系统 API 调用。

本机 IP 是可能的,本机域名,做不到吧?除非你的域名信息明确记录到某个文件,这样的话直接读取文件就好。


On Wed, Dec 2, 2015 at 5:23 PM, 王程 <wang28...@gmail.com> wrote:
大家好:
   请问在openresty 或者 nginx中是否有获取本机IP和域名的方法?求告知

--
--
邮件来自列表“openresty”,专用于技术讨论!
订阅: 请发空白邮件到 openresty...@googlegroups.com
发言: 请发邮件到 open...@googlegroups.com
退订: 请发邮件至 openresty+...@googlegroups.com
归档: http://groups.google.com/group/openresty
官网: http://openresty.org/
仓库: https://github.com/agentzh/ngx_openresty
教程: http://openresty.org/download/agentzh-nginx-tutorials-zhcn.html



--
Message has been deleted

王程

unread,
Dec 2, 2015, 5:20:06 AM12/2/15
to openresty, yuan...@iresty.org
你好:
     我通过ngx.var.hostname 获取到本机的主机名,但是没有找到方法来获取本机的IP,请明示怎么获取本机IP,我指的是网卡的IP

在 2015年12月2日星期三 UTC+8下午5:39:19,YuanSheng Wang写道:

泡泡鱼

unread,
Dec 2, 2015, 7:13:45 AM12/2/15
to openresty, yuansheng
local socket = require("socket") print(os.time()) function GetAdd(hostname) local ip, resolved = socket.dns.toip(hostname) local ListTab = {} for k, v in ipairs(resolved.ip) do table.insert(ListTab, v) end return ListTab end print(unpack(GetAdd('localhost'))) print(unpack(GetAdd(socket.dns.gethostname())))


------------------ 原始邮件 ------------------
发件人: "王程";<wang28...@gmail.com>;
发送时间: 2015年12月2日(星期三) 晚上6:20
收件人: "openresty"<open...@googlegroups.com>;
抄送: "yuansheng"<yuan...@iresty.org>;
主题: Re: [openresty] 获取openresty所在的当前服务器的IP和域名

王程

unread,
Dec 2, 2015, 8:23:50 AM12/2/15
to openresty, yuan...@iresty.org, chenwe...@qq.com
非常感谢,你帮了我大忙!

在 2015年12月2日星期三 UTC+8下午8:13:45,泡泡鱼写道:

王程

unread,
Dec 4, 2015, 2:53:17 AM12/4/15
to openresty, yuan...@iresty.org, chenwe...@qq.com

你好,我认真看了下您的代码,我现在这个socket 包依赖 luascoket ,为了减少依赖,我想通过LUA FFI方式实现!
在实现过程中 调用inet_ntoa 方法时,通过 gethostbyname 方法得到的结果无法转换为 struct in_addr 参数。

C代码如下:
typedef char * in_addr_t;

typedef struct 
    {
in_addr_t s_addr;
}in_addr;

typedef struct 
    {
        char    *h_name;               
        char    **h_aliases;
        int     h_addrtype;
        int     h_length;
        char    **h_addr_list;
        
    }hostent;
int gethostname(char *name, size_t len);     
char *inet_ntoa (struct in_addr); 

   hent = gethostbyname(hname);
     for(i = 0; hent->h_addr_list[i]; i++) {
          printf("%s\n", inet_ntoa(*(struct in_addr*)(hent->h_addr_list[i])));
    }

Lua代码:
     local hostent =  ffi.C.gethostbyname(ngx.var.hostname)
    通过执行 ffi.cast('struct in_addr *',hostent.h_addr_list[0]) ,已经通过ffi.cast('struct in_addr *',hostent.h_addr_list[0]) 得到 struct in_addr * ,但是无法得到 struct in_addr  ,请问还需要怎样转换么?

在 2015年12月2日星期三 UTC+8下午9:23:50,王程写道:

hopestar

unread,
May 10, 2016, 5:35:49 AM5/10/16
to openresty


在 2015年12月2日星期三 UTC+8下午5:23:35,王程写道:
大家好:
   请问在openresty 或者 nginx中是否有获取本机IP和域名的方法?求告知


ngx.var.host这个拿不到本机IP吗? 按照上面的方法,拿到的貌似都是localhost~  

Kwanhur Huang

unread,
May 10, 2016, 11:35:30 AM5/10/16
to open...@googlegroups.com, Kwanhur Huang
hello,

ngx.var.host 获取主机名
ngx.var.server_addr 获取主机IP,若listen没有没有指明服务器地址,使用这变量会发起一次系统调用来获取到主机IP地址(慎用!)

Kwanhur Huang

unread,
May 10, 2016, 11:45:13 AM5/10/16
to open...@googlegroups.com, Kwanhur Huang
ngx.var.host 是获取请求信息中的host(若listen没有指明IP地址则返回localhost,若指明则返回IP地址)
ngx.var.hostname 是获取所在服务器的主机名

Guanglin Lv

unread,
May 11, 2016, 6:50:13 AM5/11/16
to openresty, huang_...@163.com

我记得ngx.var.host是按如下顺序获取

请求行上的host --> 请求头域的host --> nginx server name


在 2016年5月10日星期二 UTC+8下午11:45:13,Kwanhur Huang写道:

Ahlfors Lee

unread,
May 12, 2016, 11:50:20 PM5/12/16
to openresty, yuan...@iresty.org
Hello,

求教下各位,如果将ngx.var.hostname作为机器识别,是否可行?本来想用ngx.var.server_addr, 但测试了下发现获取到的是127.0.0.1,看来不太合适用。

在 2015年12月2日星期三 UTC+8下午5:39:19,YuanSheng Wang写道:
可以通过 LuaJIT 的 FFI 方式调用本地 C 函数完成系统 API 调用。

Kwanhur Huang

unread,
May 13, 2016, 12:13:42 AM5/13/16
to open...@googlegroups.com, Kwanhur Huang, yuan...@iresty.org
若确保服务器hostname是唯一的,可考虑采用
若不是唯一的,则可考虑采用在init阶段通过os.execute获取本机IP保存至共享变量
Reply all
Reply to author
Forward
0 new messages