【问题标题】:How to use nginx http/2 server push to send a json payload from an API call to the client如何使用 nginx http/2 服务器推送将 json 有效负载从 API 调用发送到客户端
【发布时间】:2020-01-29 05:13:38
【问题描述】:

我有一个简单的 nginx 配置,如下所示,当客户端请求 /demo.html?version={some_version} 时,我需要返回某个版本的 json 有效负载并将其与请求的 demo.html 一起推送到浏览器。我有一个 REST 端点,可用于获取正确的 json。但是我找不到任何关于如何实现这一点的文档。

server {
    listen 443 ssl;
    server_name localhost;

    ssl_certificate /etc/nginx/ssl/example.crt;
    ssl_certificate_key /etc/nginx/ssl/example.key;

    location / {
        root /usr/share/nginx/html;
        index index.html index.htm;
    }

    location = /demo.html?version=x {
        http2_push payload_x.json; # need to make a call to get a json and push
    }
}

是否甚至可以在 nginx 中动态推送 json 有效负载?
如果是这样,如何做到这一点?

【问题讨论】:

    标签: nginx http2 server-push


    【解决方案1】:

    是的,可以推送 JSON 文件。是的,你会像你说的那样做,但要确保你给它完整的路径(所以至少必须以 / 开头):

    http2_push /payload_x.json;
    

    要根据版本查询参数设置 x 变量,您应该可以使用$arg_version

    http2_push /payload_$arg_version.json;
    

    您仍然需要从浏览器请求 JSON 文件,但随后它将获取推送资源,而不是从服务器一路请求它。

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 2020-02-28
      • 1970-01-01
      • 2016-10-23
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      相关资源
      最近更新 更多