{"id":5935,"date":"2015-02-16T13:40:51","date_gmt":"2015-02-16T05:40:51","guid":{"rendered":"https:\/\/webhostinggeeks.com\/howto\/?p=5935"},"modified":"2023-07-20T08:55:30","modified_gmt":"2023-07-20T08:55:30","slug":"how-to-pass-clientsvisitors-ip-through-varnish-to-nginx","status":"publish","type":"post","link":"https:\/\/webhostinggeeks.com\/howto\/how-to-pass-clientsvisitors-ip-through-varnish-to-nginx\/","title":{"rendered":"How to Pass Clients\/Visitors IP Through Varnish to Nginx"},"content":{"rendered":"<p><a href=\"https:\/\/webhostinggeeks.com\/blog\/varnish-mechanism-features-capabilities-advantages\/\">Varnish<\/a> is a powerful open-source software known as an HTTP <a href=\"https:\/\/webhostinggeeks.com\/blog\/what-is-reverse-proxy\/\">reverse proxy<\/a> typically run in front of <a href=\"https:\/\/webhostinggeeks.com\/best\/web-server\/\">web servers<\/a> such as <a href=\"https:\/\/webhostinggeeks.com\/blog\/apache-http-server-explained\/\">Apache<\/a> or <a href=\"https:\/\/webhostinggeeks.com\/blog\/nginx-server-explained\/\">Nginx<\/a>.<\/p>\n<p>Its primary function is to store the Varnish cache and remember the web server&#8217;s response to the user at the time of the first content access. It then returns the cached copy for subsequent requests from end users without asking the Nginx web server again.<\/p>\n<p>This process can lead to a situation where the Nginx access logs display the local IP proxy (usually 127.0.0.1 if installed on the same server) instead of the user&#8217;s IP.<\/p>\n<p>For example:<\/p>\n<pre>\r\n127.0.0.1 - - [16\/Feb\/2015:01:03:09 +0800] \"GET \/red-hat-details-next-linux-and-storage-platforms-for-cloud-big-data-era\/?share=google-plus-1 HTTP\/1.1\" 302 5 \"-\" \"Mozilla\/5.0 (compatible; Yahoo! Slurp; http:\/\/help.yahoo.com\/help\/us\/ysearch\/slurp)\"\r\n127.0.0.1 - - [16\/Feb\/2015:01:03:15 +0800] \"GET \/how-to-install-and-configure-epel-repository-on-centos-5-8\/ HTTP\/1.1\" 200 15212 \"-\" \"Mozilla\/5.0 (Windows NT 6.1) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/40.0.2214.111 Safari\/537.36\"\r\n127.0.0.1 - - [16\/Feb\/2015:01:03:22 +0800] \"POST \/ngx_pagespeed_beacon?url=http%3A%2F%2Fwww.ehowstuff.com%2Fhow-to-install-telnet-client-on-centos-6-3%2F HTTP\/1.1\" 404 564 \"https:\/\/webhostinggeeks.com\/howto\/how-to-install-telnet-client-on-centos-6-3\/\" \"Mozilla\/5.0 (X11; Linux x86_64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/40.0.2214.111 Safari\/537.36\"\r\n127.0.0.1 - - [16\/Feb\/2015:01:03:23 +0800] \"GET \/how-to-setup-squid-proxy-server-on-linux-centos-6-3\/ HTTP\/1.1\" 200 16246 \"-\" \"Mozilla\/5.0 (compatible; Baiduspider\/2.0; +http:\/\/www.baidu.com\/search\/spider.html)\"\r\n<\/pre>\n<p>This can be problematic for software like awstats or log analysis software due to incomplete information about the visitors.<\/p>\n<p>In this tutorial, we will guide you on how to relay your blog visitor IP address through Varnish, to Nginx logs. This process has been tested on CentOS 6.6 and CentOS 7. Before we start, please ensure that the http_realip_module has been enabled. This module allows changing the client&#8217;s IP address to value from request header (e.g., X-Real-IP or X-Forwarded-For). This module isn&#8217;t built by default, enable it with the configure option &#8211;with-http_realip_module.<\/p>\n<h2>Step 1: Include &#8220;X-Forwareded-For&#8221; in the sub vcl_recv default.vcl<\/h2>\n<pre>\r\n[root@centos66 ~]# vi \/etc\/varnish\/default.vcl\r\nsub vcl_recv {\r\n # IP forwarding\r\n if (req.restarts == 0) {\r\n if (req.http.x-forwarded-for) {\r\n set req.http.X-Forwarded-For =\r\n req.http.X-Forwarded-For + \", \" + client.ip;\r\n } else {\r\n set req.http.X-Forwarded-For = client.ip;\r\n }\r\n }\r\n..\r\n..\r\n<\/pre>\n<h2>Step 2: Add the following in nginx.conf<\/h2>\n<pre>\r\n[root@centos66 ~]# vi \/etc\/nginx\/nginx.conf\r\nhttp {\r\n..\r\n..\r\n set_real_ip_from 127.0.0.1;\r\n real_ip_header X-Forwarded-For;\r\n..\r\n..\r\n}\r\n<\/pre>\n<h2>Step 3: Restart Nginx web server and Varnish<\/h2>\n<pre>\r\n[root@centos66 ~]# service nginx restart\r\n[root@centos66 ~]# service varnish restart\r\n<\/pre>\n<h2>Step 4: Check and monitor the nginx access log again<\/h2>\n<p>After performing the above steps, monitor the Nginx access log again. It should now return the actual visitor IP.<\/p>\n<p>For example:<\/p>\n<pre>\r\n157.55.39.102 - - [16\/Feb\/2015:01:06:04 +0800] \"GET \/how-to-download-centos-6-2-iso\/ HTTP\/1.1\" 200 14622 \"-\" \"Mozilla\/5.0 (compatible; bingbot\/2.0; +http:\/\/www.bing.com\/bingbot.htm)\"\r\n68.180.228.247 - - [16\/Feb\/2015:01:06:16 +0800] \"GET \/tag\/centos-6-2\/page\/4\/ HTTP\/1.1\" 200 14474 \"-\" \"Mozilla\/5.0 (compatible; Yahoo! Slurp; http:\/\/help.yahoo.com\/help\/us\/ysearch\/slurp)\"\r\n220.181.108.178 - - [16\/Feb\/2015:01:06:25 +0800] \"GET \/howto-guides\/howto-centos\/ HTTP\/1.1\" 200 13863 \"-\" \"Mozilla\/5.0 (compatible; Baiduspider\/2.0; +http:\/\/www.baidu.com\/search\/spider.html)\"\r\n66.249.79.116 - - [16\/Feb\/2015:01:06:33 +0800] \"GET \/how-to-enable-root-login-on-ubuntu-14-04\/ HTTP\/1.1\" 200 15547 \"-\" \"Mozilla\/5.0 (compatible; Googlebot\/2.1; +http:\/\/www.google.com\/bot.html)\"\r\n<\/pre>\n<h2>Commands Mentioned<\/h2>\n<ul>\n<li><span class=\"fw-bold\">vi \/etc\/varnish\/default.vcl<\/span> \u2013 Opens the Varnish configuration file in a text editor.<\/li>\n<li><span class=\"fw-bold\">vi \/etc\/nginx\/nginx.conf<\/span> \u2013 Opens the Nginx configuration file in a text editor.<\/li>\n<li><span class=\"fw-bold\">service nginx restart<\/span> \u2013 Restarts the Nginx service.<\/li>\n<li><span class=\"fw-bold\">service varnish restart<\/span> \u2013 Restarts the Varnish service.<\/li>\n<\/ul>\n<h2>FAQ<\/h2>\n<ol itemscope itemtype=\"https:\/\/schema.org\/FAQPage\">\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is Varnish in web hosting?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">Varnish is an open-source HTTP reverse proxy software that is typically run in front of web servers like Apache or Nginx. It is designed to store the Varnish cache and remember the web server&#8217;s response to the user at the time of the first content access. It then returns the cached copy for subsequent requests from end users without asking the web server again, thereby improving the performance of the website.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What is the http_realip_module in Nginx?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">The http_realip_module is a module in Nginx that allows changing the client&#8217;s IP address to value from request header (e.g., X-Real-IP or X-Forwarded-For). This is particularly useful when you want to pass the original client&#8217;s IP address through proxies or load balancers.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">Why is it important to pass the client&#8217;s IP address through Varnish to Nginx?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">Passing the client&#8217;s IP address through Varnish to Nginx is important for accurate logging and analysis. Without this, the Nginx access logs would display the local IP proxy instead of the user&#8217;s IP, leading to incomplete information about the visitors. This can be problematic for software like awstats or log analysis software.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What are the steps to relay the visitor&#8217;s IP address through Varnish to Nginx logs?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">The steps involve modifying the Varnish and Nginx configuration files to include the &#8220;X-Forwarded-For&#8221; header, setting the real IP from 127.0.0.1, and then restarting both the Nginx and Varnish services. After these steps, the Nginx access log should return the actual visitor IP.<\/span>\n            <\/p>\n<\/li>\n<li itemscope itemprop=\"mainEntity\" itemtype=\"https:\/\/schema.org\/Question\">\n<p class=\"fw-bold\" itemprop=\"name\">What does the command &#8216;service nginx restart&#8217; do?<\/p>\n<p itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n                <span itemprop=\"text\">The command &#8216;service nginx restart&#8217; is used to restart the Nginx service. This is often necessary after making changes to the Nginx configuration file to ensure that the new settings take effect.<\/span>\n            <\/p>\n<\/li>\n<\/ol>\n<h2>Conclusion<\/h2>\n<p>Relaying your blog visitor&#8217;s IP address through Varnish to Nginx logs is a crucial step in ensuring accurate logging and analysis of your website&#8217;s traffic. By following the steps outlined in this tutorial, you can easily configure your server to pass the client&#8217;s IP address through Varnish to Nginx. This process has been tested on CentOS 6.6 and CentOS 7, and it involves modifying the Varnish and Nginx configuration files, setting the real IP from 127.0.0.1, and restarting both the Nginx and Varnish services.<\/p>\n<p>Remember, before you start, ensure that the http_realip_module has been enabled. This module allows changing the client&#8217;s IP address to value from request header (e.g., X-Real-IP or X-Forwarded-For). This module isn&#8217;t built by default, enable it with the configure option &#8211;with-http_realip_module.<\/p>\n<p>I hope this tutorial has been helpful in guiding you through the process.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Varnish is a powerful open-source software known as an HTTP reverse proxy typically run in front of web servers such as Apache or Nginx. Its primary function is to store&#8230;<\/p>\n","protected":false},"author":6,"featured_media":5538,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"wds_primary_category":0,"footnotes":""},"categories":[1014,1078],"tags":[1605,1876],"class_list":["post-5935","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-nginx","category-varnish-cache","tag-nginx","tag-varnish"],"_links":{"self":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/5935","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/comments?post=5935"}],"version-history":[{"count":0,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/posts\/5935\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media\/5538"}],"wp:attachment":[{"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/media?parent=5935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/categories?post=5935"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webhostinggeeks.com\/howto\/wp-json\/wp\/v2\/tags?post=5935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}