Nginx: directly return $remote_addr in text/plain Nginx: directly return $remote_addr in text/plain nginx nginx

Nginx: directly return $remote_addr in text/plain


The simplest way is:

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

No need to use any 3rd party module (echo, lua etc.)


Use ngx_echo:

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

Use ngx_lua:

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


Here's an approach you can use to consider proxied connections:

# Map IP address to variablemap ":$http_x_forwarded_for" $IP_ADDR {    ":" $remote_addr; # Forwarded for not set    default $http_x_forwarded_for; # Forwarded for is set}server {    ...    location / {        default_type text/plain;        return 200 "$IP_ADDR";    }}