29

Given the following configuration of nginx:

server {
    listen  80;
    server_name apilocal;
    sub_filter  "apiupstream/api" "apilocal";
    sub_filter_once off;
    location /people/ {
            proxy_pass  http://apiupstream/api/people/;
            proxy_set_header Accept-Encoding "";
    }
}

Sub_filter does not properly response parts of the response. Once I remove proxy_pass from the configuration, it works properly. A lot of folks with this problem end up having gzip compression from the upstream server. I've verified that my upstream server does not have gzip encoding turned on for its responses. But just in case, I've also used the proxy_set_header above to not accept gzip.

Is there potentially something else I'm missing?

the-wabbit
  • 40,737
  • 13
  • 111
  • 174
kylehayes
  • 367
  • 1
  • 3
  • 8

5 Answers5

19

Your response probably has other content-type than defined in sub_filter_types by default.

Reference: http://nginx.org/r/sub_filter_types

VBart
  • 8,309
  • 3
  • 25
  • 26
16

James T Snell answered it in a comment:

I didn't have proxy_set_header Accept-Encoding ""; you need that to tell the backend that compression isn't allowed in a response.

Jan DB
  • 291
  • 2
  • 3
1

make sure precompression is not activated:, otherwise it will not work because sub_filter is executed on the zipped data

server {
    gzip_static off;
}
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 05 '22 at 06:19
1

does it need to be inside a location block? also, maybe no quotes on match arg?

http://wiki.nginx.org/HttpSubModule

location / {   sub_filter      
      </head>   
      '</head><script
      language="javascript" src="$script"></script>';   
      sub_filter_once on;
}
nandoP
  • 2,021
  • 14
  • 15
0

Using ingress controller on top of kubernetes, this is how i fixed it:

    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_set_header Accept-Encoding "";
      sub_filter_once off;
      sub_filter_types *;
      sub_filter 'http:' 'https:';

But i restricted that to PATHs that requires that substituation:

spec:
  rules:
  - host: xyz.company.local
    http:
      paths:
      - pathType: Exact
        path: /my-exact-path
        backend:
          # ...