大网数据广告活动
服务器运维问答

Possible to log X-FORWARDED-FOR to nginx error_log?

· 52CE 我爱测

Nginx allows custom log formats for access logs -- right now I'm logging http_x_forwarded_for and it's working fine. The problem is my error log only shows the LB IP. After Googling and reading through their documentation, it see…

问题原文

原帖链接 · skrewler · CC BY-SA 3.0

Nginx allows custom log formats for access logs -- right now I'm logging http_x_forwarded_for and it's working fine.

The problem is my error log only shows the LB IP. After Googling and reading through their documentation, it seems error_log doesn't support a custom format, only setting the log level (info, alert, notice, warning, etc).

Is there any kind of workaround or module I could use to get around this?

For completeness, my setup is:

Amazon ELB -> pool of instances running nginx as a reverse proxy to php-fpm 7.0

已采纳解答

原帖链接 · Michael Hampton · CC BY-SA 3.0

You should be using the real IP module, so that the client's actual IP address is considered the remote IP address, rather than your load balancer's IP address. This way, you won't have to check X-Forwarded-For in your logs, nor in your application either.

It is simple to enable it, just supply your load balancer IP address(es):

set_real_ip_from  10.0.0.0/8;
set_real_ip_from  172.16.0.0/12;
set_real_ip_from  192.168.0.0/16;
real_ip_header    X-Forwarded-For;

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-02-07)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。