I have nginx terminating websites and proxying them to Apache with PHP-FPM. I'm trying to record the Clients IP in the apache log, but cannot seem to get this to work.

nginx configuration

server {
  listen         80;
  server_name    www.website.com;

  location ^~ /.well-known/acme-challenge/ {
      alias /var/www/dehydrated/;

  location / {
      return         301 https://$server_name$request_uri;

server {
  listen 443 ssl http2;
  server_name www.website.com;

  location / {
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Forwarded-Proto $scheme;
      proxy_read_timeout 600;
      proxy_buffer_size   128k;
      proxy_buffers   4 256k;
      proxy_busy_buffers_size   256k;
      client_max_body_size 100M;

  location ~ /\.ht {
      deny all;

  ssl_certificate_key /etc/nginx/ssl/www.website.com/privkey.pem;
  ssl_certificate /etc/nginx/ssl/www.website.com/fullchain.pem;
  ssl_prefer_server_ciphers on;
  ssl_dhparam /etc/nginx/ssl/dhparams.pem;

apache configuration

  <VirtualHost *:80>

    ServerName www.website.com
    ServerAlias origin.www.website.com

    ServerAdmin jd@automatem.co.nz

    DocumentRoot /var/www/www.website.com/trunk

    <IfModule mpm_itk_module>
          AssignUserId www_arenz www_arenz
    CustomLog /var/www/www.website.com/apachelogs/www.website.com-access.log combined
    ErrorLog /var/www/www.website.com/apachelogs/www.website.com-error.log

      <FilesMatch "\.php$">
          CGIPassAuth on
          SetHandler "proxy:fcgi://

      RemoteIPHeader X-Real-IP


Apache logs always show as the IP address of the client, which is the public IP address of nginx

  • Maybe the order of the directives matters?! E.g., try to put the RemoteIPHeader before the CustomLog.
    – cnst
    Feb 4 '19 at 3:11
  • @cnst have tried, but doesn't help
    – jdog
    Feb 4 '19 at 6:41
  • TBH, I'm not sure why you're even using Apache if all it's doing is static+fcgi. Why not move the whole thing to nginx?
    – cnst
    Feb 4 '19 at 6:48
  • @cnst end user needs to use .htaccess, too hard to teach otherwise, secondly, there is Varnish between nginx and apache, but not always
    – jdog
    Feb 4 '19 at 7:16
  • wait, so, how do you know it's not varnish that's affecting the headers? you probably have to do more troubleshooting yourself to find out what's wrong.
    – cnst
    Feb 4 '19 at 22:38

You could try this solution:

Apache doesn't log remoteIP when RemoteIPHeader X-Forwarded-For is present

# Log format config
LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" common
SetEnvIf X-Forwarded-For "^.*\..*\..*\..*" forwarded
CustomLog "logs/access_log" common env=forwarded

# Header config
RemoteIPHeader X-Real-IP
RemoteIPHeader X-Client-IP

Furthermore, log combined use %h. Setting %a, it tells to Apache to log the client IP as recorded by mod_remoteip (%a) rather than hostname (%h)

For your reference:



Hope this can help you

  • "Furthermore, log combined use %h. Setting %a, it tells to Apache to log the client IP as recorded by mod_remoteip (%a)" This was really the only missing piece of information
    – jdog
    Feb 7 '19 at 20:38

You can also use rpaf apache module instead of RemoteIP directive, set it in httpd.conf

  <IfModule rpaf_module>                                                                                                                                                                                                                                                 
      RPAF_Enable On                                                                                                                                                                                                                                                     
      RPAF_SetHostName On                                                                                                                                                                                                                                                
      RPAF_Header X-Forwarded-For                                                                                                                                                                                                                                        

Then just add a new log format like this

LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" forwarded

CustomLog "/var/log/httpd-access.log" forwarded

Works a treat here with nginx as proxy + apache + php-fpm

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.