nginx在使用varnish作缓存情况下获取用户真实ip
http://yaozb.blog.51cto.com/2762349/840297
- A:192.168.1.151 服务: varnishd
- B:192.168.1.150 服务: nginx
- # yum install pcre*
- # wget http://repo.varnish-cache.org/source/varnish-3.0.0.tar.gz
- # tar -xvf varnishd-3.0.0.tar.gz
- # cd varnish-3.0.0
- # ./configure --prefix=/usr/local/varnishd
- # make&&make install
- # yum install pcre*
- # wget http://soft.vpser.net/web/nginx/nginx-1.0.10.tar.gz
- # cd nginx-1.0.10/
- # ./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
- # make && make install
- #Cache for linuxtone sites
- #backend vhost
- backend wwwyaozhibingceshicom {
- .host = "www.yaozhibingceshi.com";
- .port = "80";
- }
- #acl
- acl purge {
- "localhost";
- "127.0.0.1";
- "192.168.0.0"/24;
- }
- sub vcl_recv {
- if (req.http.Accept-Encoding) {
- if (req.url ~ "\.(jpg|png|gif|jpeg|flv)$" ) {
- remove req.http.Accept-Encoding;
- remove req.http.Cookie;
- } else if (req.http.Accept-Encoding ~ "gzip") {
- set req.http.Accept-Encoding = "gzip";
- } else if (req.http.Accept-Encoding ~ "deflate") {
- set req.http.Accept-Encoding = "deflate";
- } else {
- remove req.http.Accept-Encoding;
- }
- }
- if (req.http.host ~ "(.*)yaozhibingceshi.com") {
- set req.backend = wwwyaozhibingceshicom;
- }
- else {
- error 404 "This website is maintaining or not exist!";
- }
- if (req.request == "PURGE") {
- if (!client.ip ~purge) {
- error 405 "Not Allowed";
- }
- #.dd.....
- return(lookup);
- }
- #...GET...url...jpg,png,gif. ..cookie
- 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)$") {
- unset req.http.cookie;
- }
- #..GET...url.php....cache....
- if (req.request =="GET"&&req.url ~ "\.php($|\?)"){
- return (pass);
- }
- # }
- #........pipe..
- if (req.request != "GET" &&
- req.request != "HEAD" &&
- req.request != "PUT" &&
- req.request != "POST" &&
- req.request != "TRACE" &&
- req.request != "OPTIONS" &&
- req.request != "DELETE") {
- return (pipe);
- }
- #..GET .HEAD.....
- if (req.request != "GET" && req.request != "HEAD") {
- return (pass);
- }
- if (req.http.Authorization) {
- return (pass);
- }
- return (lookup);
- }
- #..url+host hash......
- sub vcl_hash {
- hash_data(req.url);
- if (req.http.host) {
- hash_data(req.http.host);
- } else {
- hash_data(server.ip);
- }
- return (hash);
- }
- # .....purge .....
- sub vcl_hit {
- if (req.request == "PURGE") {
- set obj.ttl = 0s;
- error 200 "Purged";
- }
- return (deliver);
- }
- sub vcl_fetch {
- if (req.url ~ "\.(jpeg|jpg|png|gif|gz|tgz|bz2|tbz|mp3|ogg|ico|swf|flv|dmg|js|css|html|htm)$") {
- set beresp.ttl = 2d;
- set berespberesp.http.expires = beresp.ttl;
- set beresp.http.Cache-Control = "max-age=172800";
- unset beresp.http.set-cookie;
- }
- if (req.url ~ "\.(dmg|js|css|html|htm)$") {
- set beresp.do_gzip = true;
- }
- if (beresp.status == 503) {
- set beresp.saintmode = 15s;
- }
- }
- sub vcl_deliver {
- set resp.http.x-hits = obj.hits ;
- if (obj.hits > 0) {
- set resp.http.X-Cache = "HIT You!";
- } else {
- set resp.http.X-Cache = "MISS Me!";
- }
- }
- # vim /etc/hosts
- 192.168.1.150 www.yaozhibingceshi.com
- # /usr/local/varnishd/etc/varnish/vcl.conf -s malloc,10M -T 127.0.0.1:2000 -a 0.0.0.0:80
- # vim /usr/local/nginx/conf/nginx.conf
- server
- { listen 80;
- server_name www.yaozhibingceshi.com;
- index index.php index.html index.htm;
- root /home/yaozhibing;
- log_format wwwlogs '$remote_addr - $http_x_real_ip - $http_X_Forwarded_For - $remote_user [$time_local] "$request" ';
- access_log /home/yaozhibingceshi.log wwwlogs;
- }
- remove req.http.X-real-ip;
- set req.http.X-real-ip = client.ip;
- set req.http.X-Forwarded-For = client.ip;
- #Cache for linuxtone sites
- #backend vhost
- backend wwwyaozhibingceshicom {
- .host = "www.yaozhibingceshi.com";
- .port = "80";
- }
- #acl
- acl purge {
- "localhost";
- "127.0.0.1";
- "192.168.0.0"/24;
- }
- sub vcl_recv {
- #此处为添加内容
- remove req.http.X-real-ip;
- set req.http.X-real-ip = client.ip;
- set req.http.X-Forwarded-For = client.ip;
- if (req.http.Accept-Encoding) {
- if (req.url ~ "\.(jpg|png|gif|jpeg|flv)$" ) {
- remove req.http.Accept-Encoding;
- remove req.http.Cookie;
- } else if (req.http.Accept-Encoding ~ "gzip") {
- set req.http.Accept-Encoding = "gzip";
- } else if (req.http.Accept-Encoding ~ "deflate") {
- #以下略。。
- server
- {
- listen 80;
- server_name www.yaozhibingceshi.com;
- index index.php index.html index.htm;
- root /home/yaozhibing;
- location / {
- set_real_ip_from 192.168.1.151;
- real_ip_header X-Real-ip;
- }
- log_format wwwlogs '$remote_addr - $http_x_real_ip - $http_X_Forwarded_For - $remote_user [$time_local] "$request" ';
- access_log /home/yaozhibingceshi.log wwwlogs;
- }
- location / {
- set_real_ip_from 192.168.1.151;
- real_ip_header X-Real-ip;
- }