當我們使用 Nginx 代理轉發服務後,會發現我們無法取得客戶端的真實IP位址,而無法取得客戶端的地理位置等資訊。
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } # 代理转发 location /api/{ proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header REMOTE-HOST $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Public-Network-URL http://$http_host$request_uri; proxy_pass http://localhost:8080/; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
這樣,我們就將客戶端的頭部資訊一起轉送過去,就能取得使用者的真實IP 位址了。
以上是Nginx反向代理後無法取得客戶端真實IP位址怎麼解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!