我的工作流程如下。 [ 客户端 => Kubernetes 服务 => Nginx => Tomcat ]
这是我的 nginx.conf
worker_processes 1;
http {
log_format main '$remote_addr - $http_nid_id [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# HTTP server
server {
listen 80;
listen [::]:80;
server_name localhost;
set_real_ip_from 127.0.0.1;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
location / {
proxy_pass http://localhost:8080/;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
proxy_set_header X-Real-IP $remote_addr;
}
...
...
调用 API 时,$remote_addr
显示127.0.0.1
为access.log
。这是我的 access.log
127.0.0.1 - - [12/Sep/2021:21:32:25 +0900] "GET /test HTTP/1.1" 200 98 "-" "Go-http-client/1.1" "-"
127.0.0.1 - - [12/Sep/2021:21:32:25 +0900] "GET /test HTTP/1.1" 200 98 "-" "Go-http-client/1.1" "-"
127.0.0.1 - - [12/Sep/2021:21:32:25 +0900] "GET /test HTTP/1.1" 200 98 "-" "Go-http-client/1.1" "-"
127.0.0.1 - - [12/Sep/2021:21:32:27 +0900] "GET /test HTTP/1.1" 200 98 "-" "Go-http-client/1.1" "-"
...
为什么是$remote_addr
127.0.0.1
?(参考https://nginx.org/en/docs/http/ngx_http_core_module.html#var_remote_addr)