這篇文章將為大家詳細(xì)講解有關(guān)nginx經(jīng)過多層代理后怎么獲取真實(shí)來源ip,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)公司是一家專注于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作與策劃設(shè)計(jì),海陽網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)公司做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:海陽等地區(qū)。海陽做網(wǎng)站價(jià)格咨詢:18982081108
問題
nginx取 $remote_addr 當(dāng)做真實(shí)ip,而事實(shí)上,$http_X_Forwarded_For 才是用戶真實(shí)ip,$remote_addr只是代理上一層的地址
解決方案:
在 http 模塊 加
set_real_ip_from 172.17.10.125; #上一層代理IP地址 real_ip_header X-Forwarded-For; real_ip_recursive on;
添加之后啟動(dòng)nginx報(bào)錯(cuò):
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 是指接受從哪個(gè)信任前代理處獲得真實(shí)用戶ip
2、real_ip_header 是指從接收到報(bào)文的哪個(gè)http首部去獲取前代理傳送的用戶ip
3、real_ip_recursive 是否遞歸地排除直至得到用戶ip(默認(rèn)為off)
首先,real_ip_header 指定一個(gè)http首部名稱,默認(rèn)是X-Real-Ip,假設(shè)用默認(rèn)值的話,nginx在接收到報(bào)文后,會(huì)查看http首部X-Real-Ip。
(1)如果有1個(gè)IP,它會(huì)去核對(duì),發(fā)送方的ip是否在set_real_ip_from指定的信任ip列表中。如果是被信任的,它會(huì)去認(rèn)為這個(gè)X-Real-Ip中的IP值是前代理告訴自己的,用戶的真實(shí)IP值,于是,它會(huì)將該值賦值給自身的$remote_addr變量;如果不被信任,那么將不作處理,那么$remote_addr還是發(fā)送方的ip地址。
(2)如果X-Real-Ip有多個(gè)IP值,比如前一方代理是這么設(shè)置的:proxy_set_header X-Real-Ip $proxy_add_x_forwarded_for;
得到的是一串IP,那么此時(shí)real_ip_recursive 的值就至關(guān)重要了。nginx將會(huì)從ip列表的右到左,去比較set_real_ip_from 的信任列表中的ip。
如果real_ip_recursive為off,那么,當(dāng)最右邊一個(gè)IP,發(fā)現(xiàn)是信任IP,即認(rèn)為下一個(gè)IP(右邊第二個(gè))就是用戶的真正IP;
如果real_ip_recursive為on,那么將從右到左依次比較,知道找到一個(gè)不是信任IP為止。
然后同樣把IP值復(fù)制給$remote_addr。
生產(chǎn)nginx配置文件如下:
user www; worker_processes 10; worker_rlimit_nofile 51200; #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 51200; } 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 128; 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 8 128k;#8 128 fastcgi_busy_buffers_size 256k; fastcgi_temp_file_write_size 256k; keepalive_timeout 65; tcp_nodelay on; error_page 404 /; gzip on; gzip_min_length 2048; gzip_buffers 4 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; }
關(guān)于“nginx經(jīng)過多層代理后怎么獲取真實(shí)來源ip”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
當(dāng)前題目:nginx經(jīng)過多層代理后怎么獲取真實(shí)來源ip
URL鏈接:http://aaarwkj.com/article34/ihhhse.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營(yíng)銷、電子商務(wù)、靜態(tài)網(wǎng)站、用戶體驗(yàn)、、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)