{"id":2680,"date":"2012-03-06T15:26:12","date_gmt":"2012-03-06T15:26:12","guid":{"rendered":"https:\/\/supportex.net\/?p=2680"},"modified":"2019-05-10T17:06:10","modified_gmt":"2019-05-10T15:06:10","slug":"apache-remote-ip-behind-nginx","status":"publish","type":"post","link":"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/","title":{"rendered":"Apache: getting remote IP when working behind nginx"},"content":{"rendered":"<p>Sometimes you may want to use Apache behind nginx. In this case nginx works as reverse proxy and handles user connections and static files. And Apache generates dynamic content (for instance, with use of PHP). In this case remote IP in your scripts would be 127.0.0.1 since Apache gets requests from the same server. It could lead to some problems with your application. For example, if you add to ban list IP addresses from your application there could get address 127.0.0.1. To solve this issue you can use header <em>X-Forwarded-For<\/em> which nginx will add to it requests. But sometimes you don&#8217;t want or can&#8217;t modify your code. In this case you can use Apache module <a href=\"http:\/\/stderr.net\/apache\/rpaf\/\">rpaf<\/a> which solves this problem. Here is how to setup it on Centos 6.2.<\/p>\n<p>Firstly you need to set up headers in your nginx location:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">proxy_pass http:\/\/127.0.0.1:8888;\nproxy_set_header Host $host;\nproxy_set_header X-Real-IP $remote_addr;\nproxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;<\/pre>\n<p>To install module we need to rebuild it from srpm package. You can get it from <a href=\"http:\/\/rpms.southbridge.ru\/\">this repository<\/a>.<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">wget http:\/\/rpms.southbridge.ru\/stable\/SRPMS\/mod_rpaf-0.6-1.southbridge.src.rpm<\/code><\/p>\n<p>Rebuild:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">rpmbuild -bb ~\/rpmbuild\/SPECS\/mod_rpaf.spec<\/code><\/p>\n<p>And install:<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">rpm -ivh ~\/rpmbuild\/RPMS\/x86_64\/mod_rpaf-0.6-1.southbridge.x86_64.rpm<\/code><\/p>\n<p>Add to your Apache config file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">LoadModule rpaf_module modules\/mod_rpaf-2.0.so\n\nRPAFenable on\nRPAFsethostname Off\nRPAFproxy_ips 127.0.0.1 &lt;your external IP&gt;\n\nRPAFheader X-Real-IP<\/pre>\n<p>After Apache restart it should start getting correct remote IP of user. To make sure check its access logs.<\/p>\n<div id=\"_mcePaste\" style=\"position: absolute; left: -10000px; top: 110px; width: 1px; height: 1px; overflow-x: hidden; overflow-y: hidden;\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you may want to use Apache behind nginx. In this case nginx works as reverse proxy and handles user connections and static files. And Apache generates dynamic content (for instance, with use of PHP). In this case remote IP in your scripts would be 127.0.0.1 since Apache gets requests from the same server. It&hellip; <\/p>\n<div class=\"readmore-wrapper\"><a href=\"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/\" class=\"more-link\">Read <\/a><\/div>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[152],"tags":[249,275,246],"class_list":["post-2680","post","type-post","status-publish","format-standard","hentry","category-linux","tag-apache-en","tag-linux","tag-nginx-en"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Apache: getting remote IP when working behind nginx &#8211; Supportex.NET blog<\/title>\n<meta name=\"description\" content=\"How to get Apache remote IP when working behind nginx. Remote IP Apache nginx proxy.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Apache: getting remote IP when working behind nginx &#8211; Supportex.NET blog\" \/>\n<meta property=\"og:description\" content=\"How to get Apache remote IP when working behind nginx. Remote IP Apache nginx proxy.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/\" \/>\n<meta property=\"og:site_name\" content=\"Supportex.NET blog\" \/>\n<meta property=\"article:published_time\" content=\"2012-03-06T15:26:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-05-10T15:06:10+00:00\" \/>\n<meta name=\"author\" content=\"Oleksii Tykhonov\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Oleksii Tykhonov\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/\"},\"author\":{\"name\":\"Oleksii Tykhonov\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\"},\"headline\":\"Apache: getting remote IP when working behind nginx\",\"datePublished\":\"2012-03-06T15:26:12+00:00\",\"dateModified\":\"2019-05-10T15:06:10+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/\"},\"wordCount\":193,\"keywords\":[\"Apache\",\"linux\",\"nginx\"],\"articleSection\":[\"linux\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/\",\"url\":\"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/\",\"name\":\"Apache: getting remote IP when working behind nginx &#8211; Supportex.NET blog\",\"isPartOf\":{\"@id\":\"https:\/\/supportex.net\/blog\/en\/#website\"},\"datePublished\":\"2012-03-06T15:26:12+00:00\",\"dateModified\":\"2019-05-10T15:06:10+00:00\",\"author\":{\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\"},\"description\":\"How to get Apache remote IP when working behind nginx. Remote IP Apache nginx proxy.\",\"breadcrumb\":{\"@id\":\"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/supportex.net\/blog\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Apache: getting remote IP when working behind nginx\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#website\",\"url\":\"https:\/\/supportex.net\/blog\/en\/\",\"name\":\"Supportex.NET blog\",\"description\":\"Server and network management company\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/supportex.net\/blog\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251\",\"name\":\"Oleksii Tykhonov\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g\",\"caption\":\"Oleksii Tykhonov\"},\"url\":\"https:\/\/supportex.net\/blog\/author\/oleksiitykhonov\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Apache: getting remote IP when working behind nginx &#8211; Supportex.NET blog","description":"How to get Apache remote IP when working behind nginx. Remote IP Apache nginx proxy.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/","og_locale":"en_US","og_type":"article","og_title":"Apache: getting remote IP when working behind nginx &#8211; Supportex.NET blog","og_description":"How to get Apache remote IP when working behind nginx. Remote IP Apache nginx proxy.","og_url":"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/","og_site_name":"Supportex.NET blog","article_published_time":"2012-03-06T15:26:12+00:00","article_modified_time":"2019-05-10T15:06:10+00:00","author":"Oleksii Tykhonov","twitter_misc":{"Written by":"Oleksii Tykhonov","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/#article","isPartOf":{"@id":"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/"},"author":{"name":"Oleksii Tykhonov","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251"},"headline":"Apache: getting remote IP when working behind nginx","datePublished":"2012-03-06T15:26:12+00:00","dateModified":"2019-05-10T15:06:10+00:00","mainEntityOfPage":{"@id":"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/"},"wordCount":193,"keywords":["Apache","linux","nginx"],"articleSection":["linux"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/","url":"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/","name":"Apache: getting remote IP when working behind nginx &#8211; Supportex.NET blog","isPartOf":{"@id":"https:\/\/supportex.net\/blog\/en\/#website"},"datePublished":"2012-03-06T15:26:12+00:00","dateModified":"2019-05-10T15:06:10+00:00","author":{"@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251"},"description":"How to get Apache remote IP when working behind nginx. Remote IP Apache nginx proxy.","breadcrumb":{"@id":"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/supportex.net\/blog\/2012\/03\/apache-remote-ip-behind-nginx\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/supportex.net\/blog\/en\/"},{"@type":"ListItem","position":2,"name":"Apache: getting remote IP when working behind nginx"}]},{"@type":"WebSite","@id":"https:\/\/supportex.net\/blog\/en\/#website","url":"https:\/\/supportex.net\/blog\/en\/","name":"Supportex.NET blog","description":"Server and network management company","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/supportex.net\/blog\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/0690c26a0266603129fc15eae6243251","name":"Oleksii Tykhonov","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/supportex.net\/blog\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4fd5f58002717075c88963469b9babef?s=96&d=mm&r=g","caption":"Oleksii Tykhonov"},"url":"https:\/\/supportex.net\/blog\/author\/oleksiitykhonov\/"}]}},"_links":{"self":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/2680","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/comments?post=2680"}],"version-history":[{"count":4,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/2680\/revisions"}],"predecessor-version":[{"id":3151,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/posts\/2680\/revisions\/3151"}],"wp:attachment":[{"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/media?parent=2680"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/categories?post=2680"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/supportex.net\/blog\/wp-json\/wp\/v2\/tags?post=2680"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}