经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Nginx » 查看文章
nginx经过多层代理后获取真实来源ip过程详解
来源:jb51  时间:2019/9/27 12:47:45  对本文有异议

问题

nginx取 $remote_addr 当做真实ip,而事实上,$http_X_Forwarded_For 才是用户真实ip,$remote_addr只是代理上一层的地址

解决方案:

在 http 模块 加

  1. set_real_ip_from 172.17.10.125; #上一层代理IP地址
  2. real_ip_header X-Forwarded-For;
  3. real_ip_recursive on;

添加之后启动nginx报错:

  1. 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配置文件如下:

  1. user www;
  2. worker_processes 10;
  3. worker_rlimit_nofile 51200;
  4.  
  5. #error_log logs/error.log;
  6. #error_log logs/error.log notice;
  7. #error_log logs/error.log info;
  8. error_log /data/logs/nginx_error.log crit;
  9.  
  10. #pid logs/nginx.pid;
  11.  
  12.  
  13. events {
  14. use epoll;
  15. worker_connections 51200;
  16. }
  17.  
  18. http {
  19. include mime.types;
  20. default_type application/octet-stream;
  21.  
  22. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  23. # '$status $body_bytes_sent "$http_referer" '
  24. # '"$http_user_agent" "$http_x_forwarded_for"';
  25.  
  26. #access_log logs/access.log main;
  27.  
  28. server_names_hash_bucket_size 128;
  29. server_tokens off;
  30. expires 1h;
  31. sendfile off;
  32. tcp_nopush on;
  33. fastcgi_connect_timeout 1200s;
  34. fastcgi_send_timeout 1200s;
  35. fastcgi_read_timeout 1200s;
  36. fastcgi_buffer_size 128k;
  37. fastcgi_buffers 8 128k;#8 128
  38. fastcgi_busy_buffers_size 256k;
  39. fastcgi_temp_file_write_size 256k;
  40. keepalive_timeout 65;
  41. tcp_nodelay on;
  42. error_page 404 /;
  43. gzip on;
  44. gzip_min_length 2048;
  45. gzip_buffers 4 16k;
  46. gzip_http_version 1.1;
  47. gzip_types text/plain css html application/xml application/x-javascript ;
  48.  
  49. set_real_ip_from 上一层代理IP地址;
  50. real_ip_recursive on;
  51. real_ip_header X-Forwarded-For;
  52.  
  53. log_format access '$remote_addr - $remote_user [$time_local] "$request" '
  54. '$status $body_bytes_sent "$http_referer" '
  55. '"$http_user_agent" $http_x_forwarded_for';
  56. #################### include ################################################
  57.  
  58. include conf.d/*.conf;
  59. }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持w3xue。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号