Fetching Correct IP Address And Host
#
Description- This doc demonstrates how to get correct IP address and host when
Apache ShenYu
serves behindnginx
reverse proxy. - After fetched real
IP
andhost
, you can match them with plugins and selectors.
#
Default ImplementationThe embedded implementation in
Apache ShenYu
is :org.apache.shenyu.web.forward.ForwardedRemoteAddressResolver
.You need to config
X-Forwarded-For
innginx
first to get correctIP address
andhost
.
#
Implement through a Plugin- Declare a new class named
CustomRemoteAddressResolver
and implementsorg.apache.shenyu.plugin.api.RemoteAddressResolver
.
public interface RemoteAddressResolver {
/** * Resolve inet socket address. * * @param exchange the exchange * @return the inet socket address */ default InetSocketAddress resolve(ServerWebExchange exchange) { return exchange.getRequest().getRemoteAddress(); }
}
- Register defined class as a
Spring Bean
.
@Bean public SignService customRemoteAddressResolver() { return new CustomRemoteAddressResolver(); }