Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

268
Views
Can an nginx conf file replace an http header when writing to access.log?

I use Cloudflare for my domains.

I've noticed a situation in my access.log file that when someone attempts to connect directly (via the IP address) the $http_x_forwarded_for value is "-", which is correct and by design as $http_x_forwarded_for isn't being set by Cloudflare.

I want to do the following (see first if block), but nginx won't allow it in the main nginx.conf file. Is there another way to do this?

  if ($http_x_forwarded_for = '-') {
    $http_x_forwarded_for = $remote_addr;
  }

  # format: e.g., $http_cf_ipcountry derives from the Cloudflare header HTTP_CF_IPCOUNTRY;
  # the others too follow this format
  log_format complete '$remote_addr - $remote_user [$time_local] '
                      '"$request" $status $body_bytes_sent '
                      '"$http_referer" "$http_user_agent" "$host" '
                      '"$http_x_forwarded_for" "$http_cf_ipcountry" "$http_accept_language"';
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Using maps instead of if / else :)

Add this to your http context block:

log_format complete '$remote_addr - $remote_user [$time_local] '
                      '"$request" $status $body_bytes_sent '
                      '"$http_referer" "$http_user_agent" "$host" '
                      '"$logforwarded" "$http_cf_ipcountry" "$http_accept_language"';


map $http_x_forwarded_for $logforwarded {
     default $http_x_forwarded_for;
     ''  $remote_addr;
 }

The access-log entry will look like this: ::1 - - [02/Mar/2020:05:51:46 -0500] "GET / HTTP/1.1" 200 4 "-" "curl/7.29.0" "localhost" "::1" "-" "-"

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!