2015-08-20 52 views
3

這聽起來像一個代碼高爾夫問題,但究竟是在text/plain返回$remote_addr最簡單/最輕的方式嗎?

所以,它應該在純文本返回的IP地址的幾個字節。

216.58.221.164 

使用案例:用於瞭解客戶端自己的外部(NAT)全局IP地址的API。

是否有可能與Nginx的單獨,沒有任何後端辦呢?如果是這樣,怎麼樣?

回答

6

最簡單的方法是:

location /remote_addr { 
    default_type text/plain; 
    return 200 "$remote_addr\n"; 
} 

無需使用任何第三方模塊(回聲,LUA等。 )

+0

完美,那正是我想要的。 :) – kenn

1

使用ngx_echo

location /ip { 
    default_type text/plain; 
    echo $remote_addr; 
} 

使用ngx_lua

location /b { 
    default_type text/plain; 
    content_by_lua ' 
     ngx.say(ngx.var.remote_addr) 
    '; 
} 
相關問題