Nginx 镜像没有转发到镜像 link
Nginx mirror not forwarding to mirror link
我目前有这个 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;
}
这工作得很好,但在这种情况下我根本没有收到任何日志。
我哪里错了?
您必须将 $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 -
NGINX 提交一个简单的 curl 后的日志
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
我目前有这个 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;
}
这工作得很好,但在这种情况下我根本没有收到任何日志。
我哪里错了?
您必须将 $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 -
NGINX 提交一个简单的 curl 后的日志
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