caddy2配置
www.example.com {
encode gzip
reverse_proxy https://api.example.com {
header_up Host {upstream_hostport}
header_up X-Real-IP {http.request.header.X-Forwarded-For}
# 这里取header里面的值 发送 X-Real-IP
header_up X-Forwarded-For {http.request.remote}
header_up X-Forwarded-Port {http.request.port}
header_up X-Forwarded-Proto {http.request.scheme}
}
log {
output file /var/log/caddy/www.example.com {
roll_size 50
roll_compressed
roll_keep 20
}
}
}
nginx 接收
http {
# 前面省略
# 接收自定义标头
underscores_in_headers on;
server {
#前面省略
location / {
proxy_set_header HOST $host;
set $remote_ipaddr $remote_addr;
if ($http_x_real_ip ~ "^([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})(.*)" ) {
set $remote_ipaddr $http_x_real_ip;
}
proxy_set_header X-Real-IP $remote_ipaddr; # 变量主要是这个
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://172.16.251.255;
proxy_redirect default;
proxy_intercept_errors off;
}
}
}
Q.E.D.