Home » Default » Nginx显示来访者IP地址

server {
  server_name _;
  location / {
    default_type text/plain;

    set $response_body                   "X-Real-IP:          $remote_addr\n";

    if ($http_x_forwarded_for) {
      set $response_body "${response_body}X-Forwarded-For:    $http_x_forwarded_for\n";     
    }

    if ($http_x_forwarded_proto) {
      set $response_body "${response_body}X-Forwarded-Proto:  $http_x_forwarded_proto\n";
    }

    if ($http_x_forwarded_host) {
      set $response_body "${response_body}X-Forwarded-Host:   $http_x_forwarded_host\n";
    }

    if ($http_x_forwarded_server) {
      set $response_body "${response_body}X-Forwarded-Server: $http_x_forwarded_server\n";
    }

    return 200 $response_body;
  }
}
Sample virtual host configuration that will proxy traffic to the <a
href="http://172.16.0.101" rel="nofollow">http://172.16.0.101
address.
    server {
      server_name _;
      location / {
        proxy_set_header X-Forwarded-Proto  $scheme;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    
        proxy_set_header X-Forwarded-Host $host:$server_port;
        proxy_set_header X-Forwarded-Server $host;

    proxy_pass http://172.16.0.101;
  }
}

https://sleeplessbeastie.eu/2022/04/11/how-to-display-remote-address-using-nginx/

标签: Nginx X-Real-IP

添加新评论

V