技術(shù)員聯(lián)盟提供win764位系統(tǒng)下載,win10,win7,xp,裝機(jī)純凈版,64位旗艦版,綠色軟件,免費(fèi)軟件下載基地!

當(dāng)前位置:主頁 > 教程 > 服務(wù)器類 >

使用slb后nginx、apache獲取真實(shí)ip

來源:技術(shù)員聯(lián)盟┆發(fā)布時間:2019-02-25 06:15┆點(diǎn)擊:

  前提是在slb設(shè)置中開啟了“獲取真實(shí)ip”的功能。

  如果只是需要在程序中獲取真實(shí)ip 那么只要在程序中獲取 HTTP_X_FORWARDED_FOR 的值基本就可以了,不需要下面的操作。

  下面的操作可以實(shí)現(xiàn)nginx或者apache日志中記錄用戶真實(shí)ip或者在apache、nginx中禁止某些ip 用戶訪問等。

  nginx

  nginx作為slb獲取真實(shí)ip是使用 http_realip_module,默認(rèn)一鍵安裝包安裝的nginx沒有安裝這個模塊需要重新重新編譯nginx并加裝。

  以一鍵安裝包為例

  重新編譯nginx增加 --with-http_realip_module

  wget

  tar zxvf nginx-1.0.12.tar.gz

  cd nginx-1.0.12

  ./configure --user=www --group=www --prefix=http://www.3lian.com/alidata/server/nginx --with-http_stub_status_module --without-http-cache --with-http_ssl_module --with-http_realip_module

  make

  make install

  kill -USR2 `cat /alidata/server/nginx/logs/nginx.pid`

  kill -QUIT `cat /alidata/server/nginx/logs/ nginx.pid.oldbin`

  修改nginx.conf

  vi /alidata/server/nginx/conf/nginx.conf

  在

  fastcgi_connect_timeout 300;

  fastcgi_send_timeout 300;

  fastcgi_read_timeout 300;

  fastcgi_buffer_size 64k;

  fastcgi_buffers 4 64k;

  fastcgi_busy_buffers_size 128k;

  fastcgi_temp_file_write_size 128k;

  下面增加

  set_real_ip_from ip地址;(這個ip地址首先不是slb提供的公網(wǎng)ip,具體ip多少可以看一下之前nginx日志,如果有多個都要寫上。)

  set_real_ip_from ip地址;(這個ip地址首先不是slb提供的公網(wǎng)ip,具體ip多少可以看一下之前nginx日志,如果有多個都要寫上。)

  real_ip_header X-Forwarded-For;

  重啟nginx

  /alidata/server/nginx/sbin/nginx -s reload

  apache

  安裝apache的一個第三方模塊"mod_rpaf", 官方網(wǎng)站:

  wget

  tar zxvf mod_rpaf-0.6.tar.gz

  cd mod_rpaf-0.6

  /alidata/server/httpd/bin/apxs -i -c -n mod_rpaf-2.0.so mod_rpaf-2.0.c

  修改apache配置 /alidata/server/httpd/conf/httpd.conf

  在最末尾添加

  LoadModule rpaf_module modules/mod_rpaf-2.0.so

  RPAFenable On

  RPAFsethostname On

  RPAFproxy_ips ip地址

  RPAFheader X-Forwarded-For

  //RPAFproxy_ips ip地址(這個ip地址首先不是slb提供的公網(wǎng)ip,具體ip多少可以看一下apache日志,通常會有2個 都要寫上)

  添加完成后重啟apache

  /alidata/server/httpd/bin/apachectl restart

  具體案例

  LoadModule rpaf_module modules/mod_rpaf-2.0.so

  RPAFenable On

  RPAFsethostname On

  RPAFproxy_ips 10.242.230.65 10.242.230.131

  RPAFheader X-Forwarded-For