Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use reverse proxy to get real IP in Nginx

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

Today, I will talk to you about how to use reverse proxy to get the real IP in Nginx. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.

I. Preface

In solving the WebApi cross-domain secondary request and the Vue single page problem, Nginx solves the secondary request caused by the cross-domain problem, but it also gives rise to a new problem, that is, if you need to obtain a user's IP, the IP address is always the local address.

Back to the top.

Second, the reason

Because after the Nginx reverse proxy, the IP obtained in the application is the IP of the reverse proxy server, and the domain name obtained is also the domain name of the Url configured by the reverse proxy.

Back to the top.

III. Solutions

To solve this problem, you need to add some configuration information to the Nginx reverse proxy configuration in order to pass the real IP and domain name of the client to the application. At the same time, you need to modify the method of obtaining the IP address.

It is important to note, however, that after a reverse proxy through Nginx, if you go through several layers of proxies to access IP, the IP address you may get is in this format: clientIP,proxy1,proxy2.

If you need to insert an IP address into the database, you need to do something to prevent injection. Therefore, it is necessary to intercept the format of the above IP address.

3.1 Nginx is configured as follows

Server {listen 9461; # listener port number server_name localhost 192.168.88.22; # access address location / {root project path; # for example: eGroupsPublishUnixxmax; index index.html # this is used to deal with problems rewritten by Vue, Angular and React when using H5's History if (!-e $request_filename) {rewrite ^ (. *) / index.html last; break }} # proxy server interface location / api {proxy_pass http://localhost:9460/api;# proxy interface address # Host configuration and domain name passing proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr Proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}}

3.2 C # code to get the real IP method

# region Ip (client IP address) / client IP address / public static string Ip {get {var result = string.Empty; if (HttpContext.Current! = null) {result = GetWebClientIp ();} if (string.IsNullOrWhiteSpace (result)) {result = GetLanIp () } return result;}} / get the IP / private static string GetWebClientIp () {var ip = GetWebProxyRealIp ()? of the Web client. GetWebRemoteIp (); foreach (var hostAddress in Dns.GetHostAddresses (ip)) {if (hostAddress.AddressFamily = = AddressFamily.InterNetwork) {return hostAddress.ToString ();}} return string.Empty } / get Web remote IP / private static string GetWebRemoteIp () {try {return HttpContext.Current.Request.ServerVariables ["HTTP_X_FORWARDED_FOR"]? HttpContext.Current.Request.ServerVariables ["REMOTE_ADDR"]?? ";} catch (Exception e) {return string.Empty;} / get the real Web agent IP / private static string GetWebProxyRealIp () {var request = HttpContext.Current.Request; string ip = request.Headers.Get (" x-forwarded-for ") If (string.IsNullOrEmpty (ip) | | string.Equals ("unknown", ip, StringComparison.OrdinalIgnoreCase)) {ip = request.Headers.Get ("Proxy-Client-IP");} if (string.IsNullOrEmpty (ip) | | string.Equals ("unknown", ip, StringComparison.OrdinalIgnoreCase)) {ip = request.Headers.Get ("WL-Proxy-Client-IP") } if (string.IsNullOrEmpty (ip) | | string.Equals ("unknown", ip, StringComparison.OrdinalIgnoreCase)) {ip = request.UserHostAddress;} if (string.IsNullOrEmpty (ip)) {return string.Empty } / / there may be the following format: X-Forwarded-For: client, proxy1, proxy2 if (ip.Contains (",")) {/ / if there are multiple reverse proxies, the resulting IP is a comma-separated IP collection Take * / X-Forwarded-For: client * string [] ips = ip.Split (new string [1] {","}, StringSplitOptions.RemoveEmptyEntries) Var I = 0; for (I = 0; I < ips.Length; iTunes +) {if (ips [I]! = "") {/ / determine whether it is an intranet IP if (false = = IsInnerIp (IPs [I])) {IPAddress realIp If (IPAddress.TryParse (ips [I], out realIp) & & IPs [I] .split ('.'). Length = = 4) {/ / legal IP return ips [I];} return "" } ip = ips [0]; / / obtain * IP addresses} return ip;} / determine whether the IP address is a private network IP address / IP address / private static bool IsInnerIp (string ip) {bool isInnerIp = false; ulong ipNum = Ip2Ulong (ip) / * Private IP * Class A: 10.0.0.0-10.255.255.255 * Class B: 172.16.0.0-172.31.255.255 * Class C: 192.168.0.0-192.168.255.255 * of course, there are 127 loopback addresses * / ulong aBegin = Ip2Ulong ("10.0.0.0") Ulong aEnd = Ip2Ulong ("10.255.255.255"); ulong bBegin = Ip2Ulong ("172.16.0.0"); ulong bEnd = Ip2Ulong ("10.31.255.255"); ulong cBegin = Ip2Ulong ("192.168.0.0"); ulong cEnd = Ip2Ulong ("192.168.255.255") IsInnerIp = IsInner (ipNum, aBegin, aEnd) | | IsInner (ipNum, bBegin, bEnd) | | IsInner (ipNum, cBegin, cEnd) | | ip.Equals ("127.0.0.1"); return isInnerIp;} / convert IP addresses to long digits / IP addresses / private static ulong Ip2Ulong (string ip) {byte [] bytes = IPAddress.Parse (ip). GetAddressBytes () Ulong ret = 0; foreach (var b in bytes) {ret

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report