我有一个配置将我所有的本地 API 调用(来自客户端,带有 apikey 标头)重定向example.com
到远程 API 服务器distant-api-server.com:8000
)。
upstream api-server {
server distant-api-server.com:8000;
}
server {
listen 80;
index index.html;
server_name example.com;
location ~ /api/(?<path>.*) {
if ($request_method = OPTIONS ) {
add_header 'Access-Control-Allow-Origin' "$http_origin";
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'apikey';
return 200;
}
if ($api_route = "error"){return 501;}
error_page 501 /501_apikey.html;
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT';
add_header 'Access-Control-Allow-Headers' 'apikey';
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://$api_route/$path$is_args$args;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Host $remote_addr;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_redirect off;
}
}
Run Code Online (Sandbox Code Playgroud)
我想记录: - 从本地服务器发送到 /api 的请求 - 然后由 Nginx 发送的请求http://$api_route/$path$is_args$args
- 从远程 API 收到并由 Nginx 传输到我的客户端页面的响应
目前,当我调用 /api 时,远程 API 出现错误,然后我的请求超时。通过了解 Nginx 发送/接收的内容,可以帮助我进行调试。
我怎样才能实现这种日志记录?
2ps*_*2ps 14
OP,欢迎来到服务器故障!作为仅供参考,请听听@MichaelHampton 的建议,并仔细检查您的直觉。在这种情况下,访问日志确实为您提供了您可能需要的信息。你只需要告诉它你想记录什么。
为了扩展 M Hampton 的建议,您应该做的是为上游/代理日志记录定义自己的日志格式,然后使用自定义日志格式将access_log
指令添加到您正在执行的位置proxy_pass
。
location ~ /api/(?<path>.*) {
log_format upstream_logging . . .;
. . .
access_log /var/log/nginx/api_logging.log upstream_logging;
}
Run Code Online (Sandbox Code Playgroud)
您可以在日志格式中包含来自 nginx 的任何变量。您必须指定从上游个别想要的报头使用此变量,例如$upstream_http_server
。这是一种建议的日志记录格式,用于记录上游信息,您可以将其用作起点。
log_format upstream_logging '[$time_local] $remote_addr - $remote_user - $server_name to: $upstream_addr: $request upstream_response_time $upstream_response_time msec $msec request_time $request_time';
Run Code Online (Sandbox Code Playgroud)
一点点谷歌搜索和阅读文档(并听 M Hampton!)会让你走很长的路。祝你好运!
归档时间: |
|
查看次数: |
36552 次 |
最近记录: |