Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enforce Brotli over Gzip for reverse proxy #162

Open
itsdax opened this issue Jan 27, 2024 · 1 comment
Open

Enforce Brotli over Gzip for reverse proxy #162

itsdax opened this issue Jan 27, 2024 · 1 comment

Comments

@itsdax
Copy link

itsdax commented Jan 27, 2024

Hello, I am having trouble figuring out how to configure Nginx to always prefer Brotli over Gzip.

For example, if the request(Accept-Encoding: gzip, deflate, br) is sent to Nginx, and the reverse proxies location handles compression in gzip, how do I tell Nginx to gunzip content and compress with brotli?

In the case above, all responses will result in gzip compression. However, if the backend does not perform gzip compression, then the response to the original caller would be in br.

Appreciate any help on this matter. Thanks!

@itsdax
Copy link
Author

itsdax commented Jan 27, 2024

Oh I figured out a way to do this, via proxying through 3 layers. Starting from the third (last layer), it makes the request to the origin with compressing with gzip, then flows back upstream (layer 2) to gunzip, and finally back to layer one to compress with Brotli.

This seems like a hack... so I'll leave this issue open for anyone to tell me how the following code violates the geneva conventions. Please let me know if there's better alternatives.

    http {
      server {

        brotli on;
        brotli_comp_level 11;
        brotli_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    
        listen 80;
        location / {
          proxy_pass http://127.0.0.1:81/;
          proxy_http_version 1.1;
        }
    
      }
    
      server {
        listen 81;
    
        location / {
          proxy_pass http://127.0.0.1:82/;
          proxy_http_version 1.1;
          proxy_set_header Accept-Encoding "";
        }
    
      }
    
      server {
        listen 82;
    
        gunzip on;
  
        location / {
          resolver kube-dns.kube-system.svc.cluster.local;
          proxy_pass http://MYSERVICE.default.svc.cluster.local:8080/;
          proxy_http_version 1.1;
    
          proxy_set_header Accept-Encoding "gzip";
        }
    
      }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant