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 get real IP by using Nginx reverse proxy

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

Share

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

This article is about how to use Nginx reverse proxy to get real IP, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading 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.

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.

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

?

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

twenty-two

twenty-three

twenty-four

Server {

Listen 9461; # listener port number

Server_name localhost 192.168.88.22; # access address

Location / {

Root project path; # for example: E:/Publish/xxx/

Index index.html

# this is used to deal with the problem of rewriting when Vue, Angular and React use 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 delivery

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

?

one

two

three

four

five

six

seven

eight

nine

ten

eleven

twelve

thirteen

fourteen

fifteen

sixteen

seventeen

eighteen

nineteen

twenty

twenty-one

twenty-two

twenty-three

twenty-four

twenty-five

twenty-six

twenty-seven

twenty-eight

twenty-nine

thirty

thirty-one

thirty-two

thirty-three

thirty-four

thirty-five

thirty-six

thirty-seven

thirty-eight

thirty-nine

forty

forty-one

forty-two

forty-three

forty-four

forty-five

forty-six

forty-seven

forty-eight

forty-nine

fifty

fifty-one

fifty-two

fifty-three

fifty-four

fifty-five

fifty-six

fifty-seven

fifty-eight

fifty-nine

sixty

sixty-one

sixty-two

sixty-three

sixty-four

sixty-five

sixty-six

sixty-seven

sixty-eight

sixty-nine

seventy

seventy-one

seventy-two

seventy-three

seventy-four

seventy-five

seventy-six

seventy-seven

seventy-eight

seventy-nine

eighty

eighty-one

eighty-two

eighty-three

eighty-four

eighty-five

eighty-six

eighty-seven

eighty-eight

eighty-nine

ninety

ninety-one

ninety-two

ninety-three

ninety-four

ninety-five

ninety-six

ninety-seven

ninety-eight

ninety-nine

one hundred

one hundred and one

one hundred and two

one hundred and three

one hundred and four

one hundred and five

one hundred and six

one hundred and seven

one hundred and eight

one hundred and nine

one hundred and ten

one hundred and eleven

one hundred and twelve

one hundred and thirteen

one hundred and fourteen

one hundred and fifteen

one hundred and sixteen

one hundred and seventeen

one hundred and eighteen

one hundred and nineteen

one hundred and twenty

one hundred and twenty one

one hundred and twenty two

one hundred and twenty three

one hundred and twenty four

one hundred and twenty five

one hundred and twenty six

one hundred and twenty seven

one hundred and twenty eight

one hundred and twenty nine

one hundred and thirty

one hundred and thirty one

one hundred and thirty two

one hundred and thirty three

one hundred and thirty four

one hundred and thirty five

one hundred and thirty six

one hundred and thirty seven

one hundred and thirty eight

one hundred and thirty nine

one hundred and forty

one hundred and forty one

one hundred and forty two

one hundred and forty three

one hundred and forty four

one hundred and forty five

one hundred and forty six

one hundred and forty seven

one hundred and forty eight

one hundred and forty nine

one hundred and fifty

one hundred and fifty one

one hundred and fifty two

one hundred and fifty three

one hundred and fifty four

one hundred and fifty five

one hundred and fifty six

one hundred and fifty seven

one hundred and fifty eight

one hundred and fifty nine

one hundred and sixty

one hundred and sixty one

one hundred and sixty two

one hundred and sixty three

one hundred and sixty four

one hundred and sixty five

one hundred and sixty six

one hundred and sixty seven

one hundred and sixty eight

one hundred and sixty nine

one hundred and seventy

one hundred and seventy one

one hundred and seventy two

one hundred and seventy three

# 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 of the Web client

/ / /

/ / /

Private static string GetWebClientIp ()

{

Var ip = GetWebProxyRealIp ()? 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 IP of Web proxy

/ / /

/ / /

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

}

/ / the following formats may exist: X-Forwarded-For: client, proxy1, proxy2

If (ip.Contains (",")

{

/ / if there are multiple reverse proxies, the resulting IP is a comma-separated collection of IP, taking the first

/ / X-Forwarded-For: the first client

String [] ips = ip.Split (new string [1] {","}, StringSplitOptions.RemoveEmptyEntries)

Var I = 0

For (I = 0; I)

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