首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用js_body_filter njs指令时,Nginx不返回响应体。

使用js_body_filter njs指令时,Nginx不返回响应体。
EN

Stack Overflow用户
提问于 2021-09-22 16:04:10
回答 1查看 513关注 0票数 1

问题是,当使用js_body_filter指令时,nginx不会返回响应。实际上,这是示例代码,可以在文档和示例中找到。我遗漏了什么?

下面的所有配置以及可以在其上测试的Dockerfile:

代码语言:javascript
复制
docker build -t test .
docker run -p 18080:8080 test
curl localhost:18080/index.html -v

卷曲回复:

代码语言:javascript
复制
*   Trying 127.0.0.1:18080...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 18080 (#0)
> GET /index.html HTTP/1.1
> Host: localhost:18080
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< Server: nginx/1.21.1
< Date: Wed, 22 Sep 2021 16:01:05 GMT
< Content-Type: text/html
< Content-Length: 5
< Last-Modified: Wed, 22 Sep 2021 15:55:24 GMT
< Connection: keep-alive
< ETag: "614b51ec-5"
< Expires: Wed, 22 Sep 2021 16:01:04 GMT
< Cache-Control: no-cache
< Accept-Ranges: bytes
<
* transfer closed with 5 bytes remaining to read
* Closing connection 0
curl: (18) transfer closed with 5 bytes remaining to read

default.conf

代码语言:javascript
复制
js_import envsubst from conf.d/envsubst.js;

server {
  listen 8080;
  location / {
    proxy_buffering off;
    js_body_filter envsubst.process;
    root /etc/nginx/www;
    index  index.html index.htm;
    try_files $uri $uri/ /index.html;
    expires -1;
  }

  error_page   500 502 503 504  /50x.html;
  location = /50x.html {
    root /etc/nginx/www;
  }
}

server {
  listen 8085;
  location /health {
    access_log off;
    return 200 "";
  }
}

envsubst.js

代码语言:javascript
复制
var res = "";

function process(r, data, flags) {
    res += data;
    if (flags.last) {
        r.sendBuffer(res.toLowerCase(), flags);
    }
}

export default { process };

nginx.conf

代码语言:javascript
复制
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /tmp/nginx.pid;

load_module modules/ngx_http_js_module.so;

events {
    worker_connections  1024;
}


http {
    proxy_temp_path /tmp/proxy_temp;
    client_body_temp_path /tmp/client_temp;
    fastcgi_temp_path /tmp/fastcgi_temp;
    uwsgi_temp_path /tmp/uwsgi_temp;
    scgi_temp_path /tmp/scgi_temp;

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

index.html

代码语言:javascript
复制
test

Dockerfile

代码语言:javascript
复制
FROM nginxinc/nginx-unprivileged:1.21.1-alpine

COPY nginx.conf /etc/nginx/nginx.conf
COPY default.conf /etc/nginx/conf.d/default.conf
COPY envsubst.js /etc/nginx/conf.d/envsubst.js

RUN  mkdir /etc/nginx/www 
COPY index.html /etc/nginx/www/index.html
EN

回答 1

Stack Overflow用户

发布于 2022-11-29 09:31:48

我也有同样的问题,解决了这样的问题:

代码语言:javascript
复制
var res = "";

function process(r, data, flags) {
    res += data;
    if (flags.last) {
        if (res.length == 0 && r.variables.request_filename && r.headersOut["Content-Length"] != 0) {
            // running this filter on files causes empty responses
            // if we have a filename we read it and send it to the client
            var fs = require('fs');
            res = fs.readFileSync(r.variables.request_filename);
        }
        r.sendBuffer(res.toLowerCase(), flags);
    }
}

export default { process };

如上所述,使用没有任何js_body_filter..._pass,文件中的数据永远不会被读取到内存中。因此,在这个补丁中,如果过滤器没有得到任何数据,并且有一个request_filename集,并且标头指示我们要发送一个非零长度的正文,那么我们自己读取文件。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69287692

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档