這是套了CF的用法($http_cf_connecting_ip)
location /ip {
add_header Content-Type text/plain;
return 200 $http_cf_connecting_ip;}
這是套了CDN(包括CF)/有反代($http_x_forwarded_for)
location /ip {
add_header Content-Type text/plain;
return 200 $http_x_forwarded_for;}
甚麼也沒套直接用$remote_addr
location /ip {
add_header Content-Type text/plain;
return 200 $remote_addr;}
也可以這樣(三個按順序匹配,你們就用這個吧)
location /ip {
add_header Content-Type text/plain;
if ($http_cf_connecting_ip != ""){return 200 $http_cf_connecting_ip;}
if ($http_x_forwarded_for != ""){return 200 $http_x_forwarded_for;}
if ($remote_addr != ""){return 200 $remote_addr;}}
加在你的網站配置里就行了,/ip可以自己改成想要的
支援ipv4/ipv6(但示例沒解析ipv6)
文章標題:nginx的獲取ip的網頁
本文鏈接:https://angelal.cc/1236.html
文章版權:除非特別註明,否則均為AngelaL的原創文章,轉載必須以鏈接形式標明本文鏈接
本文最後更新發佈於:2025年02月26日 21:39, 某些文章具有時效性,若有錯誤或已失效,請在下方留言。