鍍金池/ 問(wèn)答/Linux/ Docker下的lnmp環(huán)境如何獲取clientIp,和主機(jī)Ip。

Docker下的lnmp環(huán)境如何獲取clientIp,和主機(jī)Ip。

首先我在Mac下使用docker,docker中使用一個(gè)容器安裝lnmp環(huán)境。用戶訪問(wèn)時(shí),在項(xiàng)目獲取訪問(wèn)者Ip時(shí),永遠(yuǎn)都是172.0.0.1之類的docker橋接的ip,拿不到客戶端ip。谷歌了很久,無(wú)法解決。求解
同時(shí)也想過(guò)去宿主機(jī)的ip。謝謝

下面是NGINX配置


listen 80;
server_name www.nihao.test;

location / {
proxy_pass http://www.nihao.test;
                    proxy_set_header    Host             $host:$server_port;#轉(zhuǎn)發(fā)后端口別忘了
                    proxy_set_header    X-Real-IP        $remote_addr;
                    proxy_set_header    X-Forwarded-For  $proxy_add_x_forwarded_for;
                    proxy_set_header    HTTP_X_FORWARDED_FOR $remote_addr;#關(guān)鍵句
                    proxy_set_header    X-Forwarded-Proto $scheme;
                    proxy_redirect      default;

    try_files $uri $uri/ $uri=404;
}
回答
編輯回答
萌面人

我也遇到這個(gè)問(wèn)題. 直接配置Nginx好像不行的. 2015到2018 這個(gè)問(wèn)題好像一直存在.

通過(guò)搜索查到的相關(guān)問(wèn)題

Document how to get real remote client ip for service running in container

nginx 獲取客戶端 IP 實(shí)現(xiàn) 根據(jù)配置了但無(wú)法獲取到真實(shí) IP

network_mode-docker 網(wǎng)絡(luò)連接方式

nginx cannot get to client IP

2017年6月17日 06:59