【问题标题】:Nginx mirror not forwarding to mirror linkNginx 镜像不转发到镜像链接
【发布时间】:2022-02-11 06:29:53
【问题描述】:

我目前有这个 nginx 设置:

events {
    worker_connections  4096;
}

http {    
    log_format upstreamlog '[$time_local] Forwarding $request from $remote_addr to: ($upstream_addr $request_uri $request) - Statuscode: $upstream_status Response time: $upstream_response_time';
    log_format upstreamlogmirror '[$time_local] Forwarding mirror $request from $remote_addr to: ($upstream_addr $request_uri $request): Statuscode: $upstream_status Response time: $upstream_response_time';

    server {    
        listen        0.0.0.0:80;
        listen        0.0.0.0:443;

        location / {
            access_log  /dev/stdout upstreamlog;
            mirror /mirror;
            proxy_pass https://first.service.com/api/;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location /mirror {
            internal;
            proxy_pass http://127.0.0.1:55555/;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

    server {    
        listen 127.0.0.1:55555;
        location / {
            access_log  /dev/stdout upstreamlogmirror;
            proxy_pass https://second.service.com/;
        }
    }
}

但是,我在这方面遇到了一些困难。

似乎镜像服务器确实收到了一个请求(我在日志中看到它),但是在请求时,例如https://localhost/myroute,它会正确转发到https://first.service.com/api/myroute,但是第二个服务只发送到https://second.service.com/,没有附加路由。

我也尝试用这个设置完成整个事情:

        location / {
            access_log  /dev/stdout upstreamlog;
            mirror /mirror;
            proxy_pass https://first.service.com/api/;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

        location /mirror {
            internal;
            access_log  /dev/stdout upstreamlogmirror;
            proxy_pass http://second.service.com/;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }

这很好用,但在这种情况下,我根本没有得到任何日志。

我哪里错了?

【问题讨论】:

    标签: nginx nginx-config


    【解决方案1】:

    您必须将 $request_uri 添加到 proxy_pass 才能将完整的 URI 发送到第二个镜像后端。刚刚在我的实验室测试过:

    server {
      listen 9001;
      log_subrequest on;
      access_log /var/log/nginx/mirror.log upstream_time;
      location / {
        mirror /mirror;
        proxy_pass http://127.0.0.1:9002/;
      }
    
      location = /mirror {
        proxy_pass http://127.0.0.1:8000$request_uri;
     }
    }
    
    server {listen 9002; return 200 "Server 1\n";}
    

    确保将internal 指令添加到镜像位置。

    由于来自镜像的响应将被忽略,我正在使用 Python http.server 来伪造我的后端并查看传入请求的跟踪。

    python3 -m http.server 8000

    Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
    127.0.0.1 - - [05/Feb/2022 22:00:45] code 404, message File not found
    127.0.0.1 - - [05/Feb/2022 22:00:45] "GET /test/something HTTP/1.0" 404 -
    

    提交简单 curl 后的 NGINX 日志 curl 127.0.0.1:9001/test/something

    127.0.0.1 - - [05/Feb/2022:22:08:46 -0500] "GET /test/something HTTP/1.1" 404 156 "-" "curl/7.29.0" upa="127.0.0.1:8000/test/something" rt=0.003 uct="0.000" uht="0.003" urt="0.003"
    127.0.0.1 - - [05/Feb/2022:22:08:46 -0500] "GET /test/something HTTP/1.1" 200 9 "-" "curl/7.29.0" upa="127.0.0.1:9002/test/something" rt=0.003 uct="0.000" uht="0.001" urt="0.001"
    

    要在日志文件中包含正确的 URI,请确保将 $request_uri 添加到日志格式中。 $uri 变量将更改为 /mirror

    阅读更多https://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_uri

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-21
      • 2019-07-20
      • 2021-08-03
      • 1970-01-01
      • 2012-01-14
      • 2017-06-24
      • 1970-01-01
      相关资源
      最近更新 更多