0

我的工作流程如下。 [ 客户端 => 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.1access.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

4

2 回答 2

0

您正在设置set_real_ip_from为,正如文档在这里127.0.0.1所说 ,它使客户地址127.0.0.1

省略这一行以获得真实的客户地址

于 2021-09-12T16:09:37.067 回答
0

您在代理查询时设置了 ‍<code>X-Real-IP。在 tomcat 中只需使用 getHeader("x_real_ip") 即可获取客户端的真实 IP。

于 2021-09-28T20:11:33.007 回答