nginx经过多层代理后获取真实来源ip
nginx取 $remote_addr 当做真实ip,而事实上,$http_X_Forwarded_For 才是用户真实ip,$remote_addr只是代理上一层的地址
解决方案:
在 http 模块 加
set_real_ip_from 172.17.10.125; #上一层代理IP地址
real_ip_header X-Forwarded-For;
real_ip_recursive on;
添加之后启动nginx报错:
nginx: [emerg] unknown directive "set_real_ip_from" in /home/lnidmp/nginx/conf/nginx.conf:26
需要添加realip模块,重新编译nginx
1、cd /usr/local/nginx-1.15.12
2、./configure --prefix=/usr/cmcc/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
3、make && make install
温馨提示:
1、set_real_ip_from 是指接受从哪个信任前代理处获得真实用户ip
2、real_ip_header 是指从接收到报文的哪个http首部去获取前代理传送的用户ip
3、real_ip_recursive 是否递归地排除直至得到用户ip(默认为off)
首先,real_ip_header 指定一个http首部名称,默认是X-Real-Ip,假设用默认值的话,nginx在接收到报文后,会查看http首部X-Real-Ip。
(1)如果有1个IP,它会去核对,发送方的ip是否在set_real_ip_from指定的信任ip列表中。如果是被信任的,它会去认为这个X-Real-Ip中的IP值是前代理告诉自己的,用户的真实IP值,于是,它会将该值赋值给自身的$remote_addr变量;如果不被信任,那么将不作处理,那么$remote_addr还是发送方的ip地址。
(2)如果X-Real-Ip有多个IP值,比如前一方代理是这么设置的:proxy_set_header X-Real-Ip $proxy_add_x_forwarded_for;
得到的是一串IP,那么此时real_ip_recursive 的值就至关重要了。nginx将会从ip列表的右到左,去比较set_real_ip_from 的信任列表中的ip。如果real_ip_recursive为off,那么,当最右边一个IP,发现是信任IP,即认为下一个IP(右边第二个)就是用户的真正IP;如果real_ip_recursive为on,那么将从右到左依次比较,知道找到一个不是信任IP为止。然后同样把IP值复制给$remote_addr。
生产nginx配置文件如下:
user www;
worker_processes ;
worker_rlimit_nofile ; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
error_log /data/logs/nginx_error.log crit; #pid logs/nginx.pid; events {
use epoll;
worker_connections ;
} http {
include mime.types;
default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; server_names_hash_bucket_size ;
server_tokens off;
expires 1h;
sendfile off;
tcp_nopush on;
fastcgi_connect_timeout 1200s;
fastcgi_send_timeout 1200s;
fastcgi_read_timeout 1200s;
fastcgi_buffer_size 128k;
fastcgi_buffers 128k;#
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
keepalive_timeout ;
tcp_nodelay on;
error_page /;
gzip on;
gzip_min_length ;
gzip_buffers 16k;
gzip_http_version 1.1;
gzip_types text/plain css html application/xml application/x-javascript ; set_real_ip_from 上一层代理IP地址;
real_ip_recursive on;
real_ip_header X-Forwarded-For; log_format access '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#################### include ################################################ include conf.d/*.conf;
}
nginx经过多层代理后获取真实来源ip的更多相关文章
- nginx设置反向代理,获取真实客户端ip
upstream这个模块提供一个简单方法来实现在轮询和客户端IP之间的后端服务器负荷平衡. upstream abc.com { server 127.0.0.1:8080; server 127.0 ...
- tornado 反向代理后 获取真实客户端IP
首先,nginx必定会设置一个Header传送过来真实的IP nginx.conf server { proxy_set_header X-Real-IP $remote_addr; location ...
- nginx代理后,获取request的ip
应用程序部署上线,一般都会用nginx之类的来进行反向代理,而不是直接访问tomcat之类的容器. 这时候如果用平时的获取ip的代码,就只会获取到nginx所在服务器的ip, 就失去了本身的意义. 今 ...
- nginx做反向代理时获取真实IP
原文:http://blog.csdn.net/aquester/article/details/48657395 1. 编译 对于client -> nginx reverse proxy - ...
- Apache, Nginx获得nginx代理后的真实用户Ip
Nginx 的反向代理设置 proxy_set_header X-Real-IP $remote_addr; apache可以设置日志格式将 %h替换为 %{X-Real-Ip}i 如: LogFo ...
- JAVA获取客户端请求的当前网络ip地址(附:Nginx反向代理后获取客户端请求的真实IP)
1. JAVA获取客户端请求的当前网络ip地址: /** * 获取客户端请求的当前网络ip * @param request * @return */ public static String get ...
- 使用nginx代理后以及配置https后,如何获取真实的ip地址
使用nginx代理后以及配置https后,如何获取真实的ip地址 Date:2018-8-27 14:15:51 使用nginx, apache等反向代理后,如果想获取请求的真实ip,要在nginx中 ...
- NGINX前端代理TOMCAT取真实客户端IP
nginx前端代理tomcat取真实客户端IP 使用Nginx作为反向代理时,Tomcat的日志记录的客户端IP就不在是真实的客户端IP,而是Nginx代理的IP.要解决这个问题可以在Nginx配置一 ...
- nginx设置反向代理后,页面上的js css文件无法加载
问题现象: nginx配置反向代理后,网页可以正常访问,但是页面上的js css文件无法加载,页面样式乱了. (1)nginx配置如下: (2)域名访问:js css文件无法加载: (3)IP访问:j ...
随机推荐
- [LeetCode&Python] Problem 908. Smallest Range I
Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...
- HDU - 6311:Cover(欧拉回路,最少的一笔画覆盖无向图)
The Wall has down and the King in the north has to send his soldiers to sentinel. The North can be r ...
- 原型设计 Axure8.1 软件注册码
用户名:Koshy 注册码: wTADPqxn3KChzJxLmUr5jTTitCgsfRkftQQ1yIG9HmK83MYSm7GPxLREGn+Ii6xY
- Tempter of the Bone dfs+剪枝
The doggie found a bone in an ancient maze, which fascinated him a lot. However, when he picked it u ...
- SQL Server获取指定行的数据
SQL Server获取指定行(如第二行)的数据 --SQL Server获取指定行(如第二行)的数据-- --法一(对象法)-- select * from ( select * , numbe ...
- smarty学习——组合修改器
对于同一个变量,你可以使用多个修改器.它们将从左到右按照设定好的顺序被依次组合使用. 使用时必须要用"|"字符作为它们之间的分隔符. 比如: {#userinfoname#} {# ...
- php过滤html标签截取部分内容
<?php $str = '<span>fdsfsdf</span><a href="#">href</a>'; echo h ...
- Jenkins系列之Jenkins的安装
我们在进行自动化测试的时候通常我们都会进行持续集成,可以帮助我们持续集成的工具有很多,我个人比较喜欢用Jenkins. 主要是因为它有如下优点: 开源免费 跨平台,支持所有的平台 web形式的可视化的 ...
- Javascript 在严格模式下不允许删除变量或对象
如下代码,运行后在浏览器中会报错. <script> "use strict"; var x = 3.14; delete x; </script>
- Oracle 实例恢复
-======================= -- Oracle 实例恢复 --======================= 一.Oracle实例失败 Oracle实例失败多为实例非一致性关闭所 ...