我有 nginx 终止网站并使用 PHP-FPM 将它们代理到 Apache。我正在尝试在 apache 日志中记录客户端 IP,但似乎无法使其正常工作。
nginx配置
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_pass http://1.2.3.4:80;
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_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/nginx/ssl/dhparams.pem;
}
阿帕奇配置
<VirtualHost *:80>
ServerName www.website.com
ServerAlias origin.www.website.com
ServerAdmin [email protected]
DocumentRoot /var/www/www.website.com/trunk
<IfModule mpm_itk_module>
AssignUserId www_arenz www_arenz
</IfModule>
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://127.0.0.1:9115/
</FilesMatch>
RemoteIPHeader X-Real-IP
RemoteIPTrustedProxy 2.3.4.5/32
</VirtualHost>
Apache日志总是显示2.3.4.5作为客户端的IP地址,也就是nginx的公网IP地址
你可以试试这个解决方案:
当 RemoteIPHeader X-Forwarded-For 存在时,Apache 不记录 remoteIP
此外,日志并用
%h
。设置%a
,它告诉 Apache 记录由 mod_remoteip(%a
) 而不是 hostname(%h
)记录的客户端 IP供你参考:
https://httpd.apache.org/docs/2.4/logs.html#accesslog
https://www.techstacks.com/howto/log-client-ip-and-xforwardedfor-ip-in-apache.html
希望这可以帮到你
您也可以使用 rpaf apache 模块而不是 RemoteIP 指令,在 httpd.conf 中设置它
然后像这样添加一个新的日志格式
在这里使用 nginx 作为代理 + apache + php-fpm