nuxt服務器端渲染請求接口獲取用戶真實ip對應國家 nuxt服務器端渲染請求接口獲取用戶真實ip對應國家

nuxt服務器端渲染請求接口獲取用戶真實ip對應國家

一、目的

通過用戶接口獲取其真實的ip地址,最終獲取到用戶所在國家。

針對情況包括: 1、ssr node端請求後端接口 獲取ip

2、客戶端請求後端接口 獲取ip

二、問題

目前通過接口獲取不到外網ip而是內網ip

原因:

  • 接口是nuxt的ssr服務器端渲染接口,而非普通客戶端請求接口(原理爲從node服務器端,發起的請求,其ip必定爲咱們服務器的內網ip地址)

三、方案

主要是依賴於nginx,獲取其真實ip

1、nginx 轉發到node服務的location中, header裏面設置 x-real-ip 爲 $remote_addrX-Forwarded-For 設爲 $proxy_add_x_forwarded_for;

location / {                                                                                               
                 proxy_pass   http://10.10.50.191:88;                                                      
                proxy_set_header Host  $host;
                # need set x-real-ip and x-forwarded-for to get user ip 
                proxy_set_header X-Real-Ip $remote_addr;
                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

  # nginx代理接口
  location ^~ /baseUrl/ {                                                                                                                                                 
      proxy_pass   http://域名:19000/;                                                                                                                          
      proxy_cookie_path /baseUrl/ /;                                                                                                                                     
      proxy_pass_header Set-Cookie;             
      proxy_set_header Host  $host;
      proxy_set_header X-Real-Ip $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }

2、前端 從 服務器端渲染代碼處, async nuxtServerInit (或者async asyncData) 裏面獲取 req

3、req裏面會有 nginx設置進去的header,x-real-ip x-forwarded-for,值爲ip地址

4、把它重新設置到axios的header中

5、後端從接口header裏面就可以拿到一開始設置的remote_addr了

以上方案針對於 服務器渲染請求的接口,後端從接口中獲取其用戶的真實ip的情況。主要是依賴前端(req中獲取header,重新設入)、nginx、後端配合實現。

對於普通的客戶端請求接口,後端從接口中獲取其用戶的真實ip的情況,也是同樣適用。主要是依賴nginx的代理轉發,設置header,後端直接獲取就可以實現。

獲取ip對應的國家(後端邏輯)

後端有ip數據表,裏面有很多國家的ip地址,通過查表來獲取對應國家簡碼,如US、CN

ssr服務端接口請求獲取國家(通過ip)整個邏輯

客戶端請求獲取國家整個邏輯

參考代碼

nginx配置

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
 worker_connections  1024;
}


http {
 include       /etc/nginx/mime.types;
 default_type  application/octet-stream;

 log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 '$status $body_bytes_sent "$http_referer" '
 '"$http_user_agent" "$http_x_forwarded_for" - $http_x_real_ip - $proxy_add_x_forwarded_for';

 access_log  /var/log/nginx/access.log  main;

 client_max_body_size   200m;

 sendfile        on;
 #tcp_nopush     on;

 keepalive_timeout  65;

 #gzip  on;

 server {
 listen      80;
 server_name  localhost;

 #charset koi8-r;

 #access_log  logs/host.access.log  main;

 # nginx轉發到node
 location / {
 proxy_pass   http://10.10.50.97:88;
 proxy_set_header Host  $host;
 # need set x-real-ip and x-forwarded-for to get user ip 
 proxy_set_header X-Real-Ip $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

 }

 #error_page  404              /404.html;

 # redirect server error pages to the static page /50x.html
 #
 error_page   500 502 503 504  /50x.html;
 location = /50x.html {
 root   html;
 }

 # nginx代理接口
 location ^~ /baseUrl/ { 
 proxy_pass   http://域名:19000/;
 proxy_cookie_path /baseUrl/ /; 
 proxy_pass_header Set-Cookie; 
 proxy_set_header Host  $host;
 proxy_set_header X-Real-Ip $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
 }

 include /etc/nginx/conf.d/*.conf;
}

java

public final class NetUtils {
 private NetUtils() {
 }

 public static String getRealIp(HttpServletRequest request) {
 String ip = request.getHeader("x-forwarded-for");
 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
 ip = request.getHeader("X-Real-IP");
 }

 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
 ip = request.getHeader("Proxy-Client-IP");
 }

 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
 ip = request.getHeader("WL-Proxy-Client-IP");
 }

 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
 ip = request.getHeader("HTTP_CLIENT_IP");
 }

 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
 ip = request.getHeader("HTTP_X_FORWARDED_FOR");
 }

 if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
 ip = request.getRemoteAddr();
 if ("127.0.1".equals(ip) || "0:0:0:0:0:0:0:1".equals(ip)) {
 InetAddress address = (InetAddress)Safe.call(InetAddress::getLocalHost);
 ip = address.getHostAddress();
 }
 }

 if (ip != null && ip.length() > 15) {
 int i = ip.indexOf(",");
 if (i > 0) {
 ip = ip.substring(0, i);
 }
 }

 return ip;
 }
}

參考資料

https://segmentfault.com/a/1190000022240278

https://www.jianshu.com/p/a37fd499f0c1

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章