NGINX 从 proxy

编程入门 行业动态 更新时间:2024-10-27 00:35:13
本文介绍了NGINX 从 proxy_pass 响应中读取正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两台服务器:

NGINX(它将文件 ID 交换为文件路径)Golang(它接受文件 id 并返回它的路径)

示例: 当浏览器客户端向 https://example/file?id=123 发出请求时,NGINX 应将此请求代理到 Golang 服务器 https://go.example/getpath?file_id=123,它将响应返回给 NGINX:

Ex: When browser client makes request to https://example/file?id=123, NGINX should proxy this request to Golang server https://go.example/getpath?file_id=123, which will return the response to NGINX:

{
  data: {
    filePath: "/static/..."
  },
  status: "ok"
}

然后 NGINX 应该从 filePath 获取值并从该位置返回文件.

Then NGINX should get value from filePath and return file from the location.

那么问题是如何在 NGINX 中读取响应(获取文件路径)?

推荐答案

我假设您是软件开发人员并且您可以完全控制您的应用程序,因此无需在此处强行将方钉插入圆孔中.

I assume you are software developer and your have full control over your application so there is no need to force square peg in a round hole here.

不同种类的反向代理支持ESI(Edge Side Includes)技术,允许开发者用静态文件的内容或上游服务器的响应体替换响应体的不同部分.

Different kinds of reverse proxies support ESI(Edge Side Includes) technology which allow developer to replace different parts of responce body with content of static files or with response bodies from upstream servers.

Nginx 也有这样的技术.它称为SSI(服务器端包含).

Nginx has such technology as well. It is called SSI (Server Side Includes).

location /file {
    ssi on;
    proxy_pass http://go.example;
}

您的上游服务器可以生成包含内容的正文 <!--# include file="/path-to-static-files/some-static-file.ext" -->nginx 会用文件的内容替换这个 in-body 指令.

Your upstream server can produce body with content <!--# include file="/path-to-static-files/some-static-file.ext" --> and nginx will replace this in-body directive with content of the file.

但你提到了流媒体...

这意味着文件将具有任意大小,并且使用 SSI 构建响应肯定会消耗宝贵的 RAM 资源,因此我们需要一个计划 #B.

It means that files will be of arbitrary sizes and building response with SSI would certainly eat precious RAM resources so we need a Plan #B.

有一种足够好"的方法可以将大文件提供给客户端,而无需向客户端显示文件的静态位置.您可以使用 nginx 的错误处理程序根据上游服务器提供的信息来服务器静态文件.例如,上游服务器可以发送回重定向 302 和包含文件的真实文件路径的位置头字段.此响应未到达客户端并被输入到错误处理程序中.

There is "good enough" method to feed big files to the clients without showing static location of the file to the client. You can use nginx's error handler to server static files based on information supplied by upstream server. Upstream server for example can send back redirect 302 with Location header field containing real file path to the file. This response does not reach the client and is feed into error handler.

这是一个配置示例:

location /file {
    error_page 302 = @service_static_file;
    proxy_intercept_errors on;
    proxy_set_header Host            $host;
    proxy_pass http://go.example;
}

location @service_static_file {
    root /hidden-files;
    try_files $upstream_http_location 404.html;
}

使用这种方法,您将能够在不使系统过载的情况下提供文件,同时可以控制向谁提供文件.

为此,您的上游服务器应以状态 302 和典型的位置:"字段响应,nginx 将使用位置内容在静态文件的新"根目录中查找文件.

For this to work your upstream server should respond with status 302 and with typical "Location:" field and nginx will use location content to find the file in the "new" root for static files.

这个方法是足够好"类型(而不是完美)的原因,因为它不支持部分请求(即范围:字节...)

The reason for this method to be of "good enough" type (instead of perfect) because it does not support partial requests (i.e. Range: bytes ...)

这篇关于NGINX 从 proxy_pass 响应中读取正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-03-29 03:36:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/747830.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NGINX   proxy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!