Nginx sni proxy, get real IP and build
一、编译Nginx
- 获取Nginx源码
- 解压并安装
- 安装gcc等编译工具
- 编译安装并添加下面的模块
- stream
- stream_ssl_preread_module
- stream_ssl_module
- http_ssl_module
- http_realip_module
- stream_realip_module
1 | ./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/sbin/nginx --conf-path=/usr/local/nginx/nginx.conf --with-stream --with-stream_ssl_preread_module --with-stream_ssl_module --with-http_ssl_module --with-http_realip_module --with-stream_realip_module |
二、修改配置文件
1 | #user nobody; |
三、后台服务器获取IP地址
添加依赖到 pom.xml 中
1
2
3
4
5<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.7</version>
</dependency>获取IP地址
1
2
3
4
5
6
7
8
9
10
public ResponseEntity<IPCheckResponse<String>> forwardRequest(HttpServletRequest request) {
// 获取客户端IP地址
String ip = request.getHeader("X-Real-IP");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
// 获取代理IP地址
ip = request.getHeader("x-forwarded-for");
}
return ResponseEntity.ok(new IPCheckResponse<>("200", "Check Success", ip));
}测试
1
curl https://xxx.com/ipCheck
返回数据
1
2
3
4
5{
"code": "200",
"message": "Check Success",
"data": "{\n \"ip\": \"100.100.100.100\",\n \"hostname\": \"host-by.xxx.com\",\n \"city\": \"Los Angeles\",\n \"region\": \"California\",\n \"country\": \"US\",\n \"loc\": \"38.0559,-128.2666\",\n \"org\": \"AS888 XXX Cloud Services\",\n \"postal\": \"00000\",\n \"timezone\": \"America/Los_Angeles\"\n}"
}