Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

276
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda