nginx在使用varnish作緩存情況下獲取用戶真實ip

前言:在網站架構當中,使用varnish緩存服務器爲網站做緩存,後端再使用lnmp架構已經是很平常的事情了,但是,在很多種情況下,後端的nginx服務器,無法獲取到用戶的真實ip.日誌中總是記錄着前端的varnish服務器ip。這也讓一些站長很煩惱。其實nginx有這種功能的。下面我就來講解一下,如何讓nginx在使用varnish做緩存的情況下獲取到用戶的真實ip。
我們先弄兩臺測試機器
  1. A:192.168.1.151 服務: varnishd 
  2. B:192.168.1.150 服務:  nginx 
一,我們先安裝好這兩個服務。
1..在A服務器上安裝varnishd
  1. # yum install pcre* 
  2. # wget  http://repo.varnish-cache.org/source/varnish-3.0.0.tar.gz 
  3. # tar -xvf varnishd-3.0.0.tar.gz 
  4. # cd varnish-3.0.0 
  5. # ./configure --prefix=/usr/local/varnishd 
  6. # make&&make install 
  7.   
2,在B服務器上安裝nginx
  1. # yum install pcre* 
  2. # wget  http://soft.vpser.net/web/nginx/nginx-1.0.10.tar.gz 
  3. # cd nginx-1.0.10/ 
  4. # ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --with-http_realip_module 
  5. # make && make install 
這裏在nginx安裝時,添加了一個 --with-http_realip_module 的模塊來獲取真實ip.
這樣我們就在兩臺服務器上分別安裝好了這兩個服務。
二.關於一些基本配置
這裏我直接在varnishd配置文件裏,加上一個緩存的網站 www.yaozhibingceshi.com  ,配置文件如下:
  1. #Cache for linuxtone sites  
  2. #backend vhost  
  3. backend  wwwyaozhibingceshicom {  
  4. .host = "www.yaozhibingceshi.com";  
  5. .port = "80";  
  6. }  
  7. #acl  
  8. acl purge {  
  9.   "localhost";  
  10.   "127.0.0.1";  
  11.   "192.168.0.0"/24;  
  12. }  
  13. sub vcl_recv {  
  14.         if (req.http.Accept-Encoding) {  
  15.             if (req.url ~ "\.(jpg|png|gif|jpeg|flv)$" ) {  
  16.                 remove req.http.Accept-Encoding;  
  17.                 remove req.http.Cookie;  
  18.             } else if (req.http.Accept-Encoding ~ "gzip") {  
  19.                 set req.http.Accept-Encoding = "gzip";  
  20.             } else if (req.http.Accept-Encoding ~ "deflate") {  
  21.                 set req.http.Accept-Encoding = "deflate";  
  22.             } else {  
  23.                 remove req.http.Accept-Encoding;  
  24.             }  
  25.         }  
  26.            if (req.http.host ~  "(.*)yaozhibingceshi.com") {  
  27.                        set req.backend = wwwyaozhibingceshicom;  
  28.                  }  
  29.             else {  
  30.                         error 404 "This website is maintaining or not exist!";  
  31.                 }  
  32.   if (req.request == "PURGE") {  
  33.      if (!client.ip ~purge) {  
  34.        error 405 "Not Allowed";  
  35.    }  
  36. #.dd.....  
  37.    return(lookup);  
  38.   }  
  39. #...GET...url...jpg,png,gif. ..cookie  
  40.   if (req.request == "GET"&& req.url ~ "\.(png|gif|jpeg|jpg|ico|swf|css|js|html|htm|gz|tgz|bz2|tbz|mp3|ogg|mp4|flv|f4v|pdf)$") {  
  41.         unset req.http.cookie;  
  42.   }  
  43. #..GET...url.php....cache....  
  44.   if (req.request =="GET"&&req.url ~ "\.php($|\?)"){  
  45.         return (pass);  
  46.   }  
  47. #   }  
  48. #........pipe..  
  49.     if (req.request != "GET" &&  
  50.       req.request != "HEAD" &&  
  51.       req.request != "PUT" &&  
  52.       req.request != "POST" &&  
  53.       req.request != "TRACE" &&  
  54.       req.request != "OPTIONS" &&  
  55.       req.request != "DELETE") {  
  56.         return (pipe);  
  57.     }  
  58. #..GET .HEAD.....  
  59.     if (req.request != "GET" && req.request != "HEAD") {  
  60.         return (pass);  
  61.     }  
  62.     if (req.http.Authorization) {  
  63.         return (pass);  
  64.     }  
  65.     return (lookup);  
  66. }  
  67. #..url+host hash......  
  68. sub vcl_hash {  
  69.     hash_data(req.url);  
  70.     if (req.http.host) {  
  71.         hash_data(req.http.host);  
  72.     } else {  
  73.         hash_data(server.ip);  
  74.     }  
  75.     return (hash);  
  76. }  
  77. # .....purge .....  
  78. sub vcl_hit {  
  79.    if (req.request == "PURGE") {  
  80.        set obj.ttl = 0s;  
  81.        error 200 "Purged";  
  82.     }  
  83.     return (deliver);  
  84. }  
  85. sub vcl_fetch {  
  86.           if (req.url ~ "\.(jpeg|jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|ico|swf|flv|dmg|js|css|html|htm)$") {  
  87.                    set beresp.ttl = 2d;  
  88.                    set berespberesp.http.expires = beresp.ttl;  
  89.                    set beresp.http.Cache-Control = "max-age=172800";  
  90.                    unset beresp.http.set-cookie;  
  91.           }  
  92.           if (req.url ~ "\.(dmg|js|css|html|htm)$") {  
  93.                    set beresp.do_gzip = true;  
  94.           }  
  95.           if (beresp.status == 503) {  
  96.                          set beresp.saintmode = 15s;  
  97.           }  
  98. }  
  99. sub vcl_deliver {  
  100.         set resp.http.x-hits = obj.hits ;  
  101.         if (obj.hits > 0) {  
  102.                 set resp.http.X-Cache = "HIT You!";  
  103.         } else {  
  104.                 set resp.http.X-Cache = "MISS Me!";  
  105.         }  
  106. }  
並在A服務器的host文件裏綁定 www.yaozhibing.com 爲 192.168.1.150
  1. # vim /etc/hosts 
  2. 192.168.1.150 www.yaozhibingceshi.com
啓動varnish
 
  1. # /usr/local/varnishd/etc/varnish/vcl.conf -s malloc,10M -T 127.0.0.1:2000 -a 0.0.0.0:80 
B服務器上的Nginx 可參考網上一些配置,都是大同小異的,
我在這裏添加一個虛擬主機:
  1. # vim /usr/local/nginx/conf/nginx.conf
  2. server 
  3.         {       listen    80; 
  4.                 server_name www.yaozhibingceshi.com; 
  5.                 index  index.php index.html index.htm; 
  6.                 root  /home/yaozhibing; 
  7.  
  8.   log_format  wwwlogs  '$remote_addr - $http_x_real_ip - $http_X_Forwarded_For - $remote_user [$time_local] "$request" '; 
  9.     access_log  /home/yaozhibingceshi.log  wwwlogs; 
我們在日誌文件裏定義了 $http_real_ip,  $http_X_forwarded_for的值,其實這兩個值是一樣的。 http_real_ip是指用戶的真實ip。$http_X_forwarded_for是指通過上一級代理之前的ip。如果有多級代理,這個值裏面就有很多的ip.我們這裏只有一級代理,所以。這裏的$http_X_forwarded_for 指的也是用戶的真實ip.
好,我們來監控一下日誌。看能不能獲取到這些值。
很明顯這些值,是獲取不到的,只能獲取到前端varnishd服務器 192.168.1.151的ip.
三,配置varnishd,讓nginx獲取到真實用戶的ip.
那麼現在我們來整一下varnish的配置。讓varnish把$http_real_ip和$http_X_forwarded_for值傳給nginx.
 我們在A服務器的varnish配置文件裏的sub vcl_recv 裏面加入以下這段:
  1. remove req.http.X-real-ip; 
  2. set req.http.X-real-ip = client.ip; 
  3. set req.http.X-Forwarded-For = client.ip; 
意思就是獲取用戶的真實ip 即 client.ip 並賦值給  http.X-real-ip 和 http.X-Forwarded-For。
現在的varnishd配置文件爲: 
 
  1. #Cache for linuxtone sites   
  2. #backend vhost   
  3. backend  wwwyaozhibingceshicom {  
  4. .host = "www.yaozhibingceshi.com";  
  5. .port = "80";  
  6. }  
  7. #acl   
  8. acl purge {  
  9.   "localhost";  
  10.   "127.0.0.1";  
  11.   "192.168.0.0"/24;  
  12. }  
  13. sub vcl_recv {  
  14.   
  15.  
  16. #此處爲添加內容 
  17. remove req.http.X-real-ip;  
  18. set req.http.X-real-ip = client.ip;  
  19. set req.http.X-Forwarded-For = client.ip;  
  20.   
  21.  
  22.         if (req.http.Accept-Encoding) {  
  23.             if (req.url ~ "\.(jpg|png|gif|jpeg|flv)$" ) {  
  24.                 remove req.http.Accept-Encoding;  
  25.                 remove req.http.Cookie;  
  26.             } else if (req.http.Accept-Encoding ~ "gzip") {  
  27.                 set req.http.Accept-Encoding = "gzip";  
  28.             } else if (req.http.Accept-Encoding ~ "deflate") {  

  29. #以下略。。  
我們再來看一下nginx的訪問日誌。看有沒有獲取到用戶真實ip.
這下我們獲取到了我本機的真實ip。192.168.1.5,但是我們從定義的日誌格式來看,這個值應該是 http.X_Forwarded_For 的值。當有多級代理的時候,這個值不能代表用戶的真實ip.但是我在varnishd確實把http_real_ip傳過來了啊,爲什麼不能顯示呢。這下,我們用要用到nginx的 http_realip_modul這個模塊了。接下來,我們在nginx裏,做一下設置,來獲取 real_ip.
四:修改nginx配置文件,來獲取用戶真實ip.
  Nginx 的http_realip_modul很好用。在nginx裏定義一下從哪獲取值。獲取哪個值。就OK 了,
我們把虛擬主機的配置文件修改成下面這樣的:
  1. server 
  2.         { 
  3.                 listen    80; 
  4.                 server_name www.yaozhibingceshi.com; 
  5.                 index  index.php index.html index.htm; 
  6.                 root  /home/yaozhibing; 
  7. location / { 
  8.   set_real_ip_from   192.168.1.151; 
  9.   real_ip_header     X-Real-ip; 
  10.    log_format  wwwlogs  '$remote_addr - $http_x_real_ip - $http_X_Forwarded_For - $remote_user [$time_local] "$request" '; 
  11.     access_log  /home/yaozhibingceshi.log  wwwlogs; 
也就是在虛擬機主機的配置文件裏,添加了:
  1. location / { 
  2.   set_real_ip_from   192.168.1.151; 
  3.   real_ip_header     X-Real-ip; 
set_real_ip_from 是定義獲取的源,就是從哪裏獲取值
real_ip_header  是定義獲取哪個值。
我們來重啓一下nginx,並監控一下日誌。
我們看到,http.X_real_ip 和 http.X_Forwarded_For的值。都能正常顯示了。
如果不想看到上級代理的值。在nginx配置文件裏把 log_format 的 $remote_addr去掉就可以了。
日誌還能添加更多內容,這裏我們只看ip,所以把日誌簡化寫的。

如果不清楚請加QQ:410018348 共同探討。

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