侧边栏壁纸
    • 累计撰写 208 篇文章
    • 累计收到 576 条评论
    Nginx、WAF 接入 CDN 后获取访客真实 ip 方法
    2022-06-14  103 阅读 0 评论 0 点赞

    Nginx、WAF 接入 CDN 后获取访客真实 ip 方法

    奉天
    2022-06-14 / 0 评论 / 103 阅读 / 检测收录中......
    温馨提示:
    本文最后更新于2022年06月14日,已超过681天没有更新,若内容或图片失效,请留言反馈。
    广告
    nginx

    方法一:打开 Nginx.conf 文件,用 log_format 在 http{}自定义一条新的日志格式:

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

    此时日志格式为:CDN IP-----真实 IP。

    方法二:

    #获取用户真实 IP,并赋值给变量$clientRealIP
    map $http_x_forwarded_for  $clientRealIp {
            ""      $remote_addr;
            ~^(?P<firstAddr>[0-9\.]+),?.*$  $firstAddr;
            }
    log_format  main  '$clientRealIP - $remote_user [$time_local] "$request" '
                                       '$status $body_bytes_sent "$http_referer" '
                                       '"$http_user_agent"';

    此时日志格式为:真实 IP-----。

    最后再网站配置项,在 access_log /www/wwwlogs/网站名.log; 后面 加上 main;

    例如:access_log /www/wwwlogs/www.xxx.com.log main;

    WAF

    打开 /www/server/nginx/waf/init.lua 文件 找到 17 行 替换代码:

    function getClientIp()
                  IP  = ngx.var.http_x_forwarded_for
                  if IP == nil then
                      IP  = ngx.var.remote_addr
                  end
                  return IP
    end

    其他具体参考百度。

    0

    海报

    正在生成.....

    评论 (0)

    取消