我在Docker的容器中只有一个Nginx。 请帮我。我在Google上搜索了这个问题
我只有一个容器可以在其中部署通常用作开发环境的nginx PHP。 PHP无法获取真实的客户端IP地址。 无论我如何设置NGINX,它都不会起作用
这是nginx配置
listen 80;
server_name lh.hello.test;
location / {
proxy_pass http://lh.hello.test;
proxy_set_header Host $host:$server_port;
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;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_redirect default;
}
这是PHP $_SERVER
'USER' => string 'hello' (length=6)
'HOME' => string '/home/hello' (length=12)
'HTTP_CONNECTION' => string 'keep-alive' (length=10)
'HTTP_ACCEPT_LANGUAGE' => string 'zh-CN,zh;q=0.9,en;q=0.8' (length=23)
'HTTP_ACCEPT_ENCODING' => string 'gzip, deflate' (length=13)
'HTTP_ACCEPT' => string 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' (length=85)
'HTTP_DNT' => string '1' (length=1)
'HTTP_USER_AGENT' => string 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.81 Safari/537.36' (length=120)
'HTTP_UPGRADE_INSECURE_REQUESTS' => string '1' (length=1)
'HTTP_CACHE_CONTROL' => string 'max-age=0' (length=9)
'HTTP_HOST' => string 'lh.hello.test:800' (length=18)
'SCRIPT_FILENAME' => string '/workspace/myProject/hello_blog/public/index.php' (length=49)
'REDIRECT_STATUS' => string '200' (length=3)
'SERVER_NAME' => string 'lh.hello.test' (length=14)
'SERVER_PORT' => string '80' (length=2)
'SERVER_ADDR' => string '172.17.0.4' (length=10)
'REMOTE_PORT' => string '54908' (length=5)
'REMOTE_ADDR' => string '172.17.0.1' (length=10)
'SERVER_SOFTWARE' => string 'nginx/1.14.0' (length=12)
'GATEWAY_INTERFACE' => string 'CGI/1.1' (length=7)
'REQUEST_SCHEME' => string 'http' (length=4)
'SERVER_PROTOCOL' => string 'HTTP/1.1' (length=8)
'DOCUMENT_ROOT' => string '/workspace/myProject/hello_blog/public' (length=39)
'DOCUMENT_URI' => string '/index.php' (length=10)
'REQUEST_URI' => string '/test/test' (length=10)
'SCRIPT_NAME' => string '/index.php' (length=10)
'CONTENT_LENGTH' => string '' (length=0)
'CONTENT_TYPE' => string '' (length=0)
'REQUEST_METHOD' => string 'GET' (length=3)
'QUERY_STRING' => string '/test/test' (length=10)
'FCGI_ROLE' => string 'RESPONDER' (length=9)
'PHP_SELF' => string '/index.php' (length=10)
'REQUEST_TIME_FLOAT' => float 1536761653.4628
'REQUEST_TIME' => int 1536761653
答案 0 :(得分:0)
如果您使用的是nginx代理,
首先添加代理配置
location / {
# proxy host and ip information
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy
proxy_pass http://xx.xx.xx.xx:xxxx;
}
然后您可以通过请求标头中的【X-Real-IP】值来生成真实IP。