How to log the correct Ip having Varnish and Nginx
来源:互联网 发布:wbs软件下载 编辑:程序博客网 时间:2024/05/25 23:25
How to log the correct Ip having Varnish and Nginx
1) Make nginx aware that you are behind a proxy
Nginx is smart enough to have a dedicated module for this work: Real Ip
This module allows to change the client’s IP address to value from request header (e. g. X-Real-IP or X-Forwarded-For), it is useful if nginx works behind some proxy of L7 load balancer, and the request comes from a local IP, but proxy add request header with client’s IP.
The configuration is really simple, you just have to add these 2 lines in your /etc/nginx/nginx.conf
file (section: http) or directly on your Virtual host file (section : server)
and restart nginx.
You have also to modify your vcl file (usually /etc/varnish/default.vcl
), in the vcl_recv
part add this rule:
And restart Varnish, this will set the header X-Forwarded-For correctly.
Drawback:
This module is usually not enabled by default, you can enable it rebuilding nginx with the configure option:
--with-http_realip_module
If you use Nginx from a binary package verify the description of the package, or simply run from the teminal nginx -V
that will give a verbose output like this one:
in this example the module is NOT built in the Nginx webserver, so this solution would not work, let’s move to solution 2:
2) Change the format of your Nginx log files
This solution uses the header X-Forwarded-For too, so you have to set it on varnish
as done in the former solution to set it. The idea behind this solution is that Nginx has all the information about the remote IP, just in a different header, so it’s just a matter of making nginx use that variable in its access logs instead of the default variable defining the referring IP.
Edit your nginx.conf file and in the http section add this line:
log_format varnish_log '$http_x_forwarded_for - $remote_user [$time_local] ' '"$request" $status $body_bytes_sent "$http_referer" ' '"$http_user_agent"' ;
You also have to change the access_log directive of you virtual host, to use the varnish_log format:
Note: log_format controls how nginx logs the requests, in this example we have added a new log format named “varnish_log” where the first field is the header $http_x_forwarded_for and not the standard $remote_addr, in this way you’ll correctly logs the remote IP of your visitors.
Drawback
if you have many virtual hosts, you have to change for everyone of them the access_log directive to use the new log_format.
- How to log the correct Ip having Varnish and Nginx
- symbian add log and get the correct part
- Varnish and nginx setup
- How to clear the archive log
- Correct Smartphone Video Orientation and How To Rotate iOS and Android Videos with ffmpeg
- Nginx, Varnish, Cherokee, thttpd, mini-httpd, WEBrick, Orion, AOLserver,Yaws and Boa log escape sequence injection
- How to link with the correct C Run-Time (CRT) library
- How to determine the correct setting for JOB_QUEUE_PROCESSES (Doc ID 578831.1)
- How to link with the correct C Run-Time (CRT) library
- How to Create and Use the DLL
- How to use the CONVERSION_EXIT_PARVW_INPUT and CONVERSION_EXIT_PARVW_OUTPUT
- How to Paging and sorting the gridview
- How to find which w3wp.exe belongs to which application pool and Find correct w3wp ProcessId for Web
- How to read Android crash log and stack trace
- How to read Android crash log and stack trace
- How to read Android crash log and stack trace
- How to read Android crash log and stack trace
- How to read Android crash log and stack trace
- 多线程(四):任务一->线程汇合->任务二
- textview动态调整背景颜色background
- 常用备查Linux、Android、数据库命令列表(不断更新)
- linux安装apache支持https(ssl)
- C语言实现strlen(char *str)
- How to log the correct Ip having Varnish and Nginx
- Firefox OS系统开发者手机抢购一空
- 关于nginx开启特定路径的ssl证书验证 双向ssl
- MooTools学习笔记(三)窗体事件domready
- opecv-亚像素水平检测角点位置
- python判断操作系统类型
- LP的决策
- hdoj_2012 素数判定
- 分布式内存管理