預設的狀況下,varnish 下面的backend , 諸如 apache 或是 nginx ... 等
接到 http 的 請求時,client ip 都是varnish 的ip
如此一下,程式無法針對來源做出判斷,geoip 也無法知道來源是屬於哪個國家
解決這個問題步驟如下

編輯 varnish/default.vcl
sub vcl_recv {
###
    if (req.restarts == 0) {
            if (req.http.x-forwarded-for) {
                    set req.http.X-Forwarded-For = req.http.X-Forwarded-For + ", " + client.ip;
            } else {
                    set req.http.X-Forwarded-For = client.ip;
            }
    }
###
}

修改backend log format 設定,以apache為例

#LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

即可在access_log 中看到實際使用者來源ip
 

arrow
arrow
    文章標籤
    varnish proxy X-Forwarded-For
    全站熱搜

    helloworld 發表在 痞客邦 留言(0) 人氣()