VPS侦探论坛

标题: 套CloudFlare后,如何利用NGINX 获取用户真实IP? [打印本页]

作者: ew52000    时间: 2022-7-1 17:25
标题: 套CloudFlare后,如何利用NGINX 获取用户真实IP?
在网上查到,在nginx.conf里面增加下面代码:

  1. set_real_ip_from 0.0.0.0/0;
  2. real_ip_header X-Forwarded-For;
复制代码

即可实现~原帖子地址:https://www.lizi.tw/program/6929.html
我之前用宝塔的时候有实验成功,转战军哥的lnmp后,添加上面代码报错。麻烦大佬们帮忙看一下~nginx重启报错信息:
  1. Stoping nginx... nginx: [emerg] unknown directive "set_real_ip_from" in /usr/local/nginx/conf/nginx.conf:24
  2. failed. Use force-quit
  3. Terminating nginx...  done
  4. Starting nginx... nginx: [emerg] unknown directive "set_real_ip_from" in /usr/local/nginx/conf/nginx.conf:24
  5. failed
复制代码
我的nginx配置添加位置
  1. user  www www;

  2. worker_processes auto;
  3. worker_cpu_affinity auto;

  4. error_log  /home/wwwlogs/nginx_error.log  crit;

  5. pid        /usr/local/nginx/logs/nginx.pid;

  6. #Specifies the value for maximum file descriptors that can be opened by this process.
  7. worker_rlimit_nofile 51200;

  8. events
  9.     {
  10.         use epoll;
  11.         worker_connections 51200;
  12.         multi_accept off;
  13.         accept_mutex off;
  14.     }

  15. http
  16.     {
  17.         include       mime.types;

  18. set_real_ip_from 0.0.0.0/0;
  19. real_ip_header X-Forwarded-For;
  20.                
  21.         default_type  application/octet-stream;

  22.         server_names_hash_bucket_size 128;
  23.         client_header_buffer_size 32k;
  24.         large_client_header_buffers 4 32k;
  25.         client_max_body_size 50m;

  26.         sendfile on;
  27.         sendfile_max_chunk 512k;
  28.         tcp_nopush on;

  29.         keepalive_timeout 60;

  30.         tcp_nodelay on;

  31.         fastcgi_connect_timeout 300;
  32.         fastcgi_send_timeout 300;
  33.         fastcgi_read_timeout 300;
  34.         fastcgi_buffer_size 64k;
  35.         fastcgi_buffers 4 64k;
  36.         fastcgi_busy_buffers_size 128k;
  37.         fastcgi_temp_file_write_size 256k;

  38.         gzip on;
  39.         gzip_min_length  5k;
  40.         gzip_buffers     4 16k;
  41.         gzip_http_version 1.1;
  42.         gzip_comp_level 6;
  43.         gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
  44.         gzip_vary on;
  45.         gzip_proxied   expired no-cache no-store private auth;
  46.         gzip_disable   "MSIE [1-6]\.";

  47.         #limit_conn_zone $binary_remote_addr zone=perip:10m;
  48.         ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

  49.         server_tokens off;
  50.         access_log off;
复制代码



作者: ew52000    时间: 2022-7-1 20:41
找到了替代的方法,很实用~
https://bbs.vpser.net/thread-14350-1-1.html
作者: licess    时间: 2022-7-1 21:34
set_real_ip_from 是 --with-http_realip_module 添加这个参数到 lnmp.conf 中的 Nginx_Modules_Options 后面的引号里面,再升级一下nginx就会增加上这个模块




欢迎光临 VPS侦探论坛 (https://bbs.lnmp.com/) Powered by Discuz! X3.4