Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
Nginx compress mime types with parameters

I have Nginx reverse proxying my service that works by OData protocol. I'm trying to enable compression for theese requests by putting

#...
gzip on;
gzip_types application/json;
#...
server {
   #...
   location /odata/ {
      proxy_pass http://localhost:7700/odata/;
   }
   #...
}

in nginx.conf.

Sometimes my service returns

Content-Type: application/json; charset=utf-8; odata.metadata=minimal

and Nginx compresses it.

But sometimes my service returns

Content-Type: application/json; odata.metadata=minimal; odata.streaming=true; charset=utf-8

and Nginx doesn't compress such responses.

What should I do to enable Nginx compressing such responses?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Solved this problem by writing middleware in my application, that changes Content-Type header and transforms it to application/json; charset=utf-8; odata.metadata=minimal; odata.streaming=true;

After that Nginx could recognize it as json content type and compresses it.

over 4 years ago · Santiago Trujillo Report

0

Let me add my solution for ASP.NET Core.

Note though that these responses are chunked so Nginx parameter gzip_min_length will not work and small responses will become gzipped as well increasing the size and reducing performance.

/// <summary>
/// Middleware to reorder Content-Type parts for Nginx compression.
/// </summary>
public class FixODataMiddlewareForNginx
{
    readonly RequestDelegate _next;

    public FixODataMiddlewareForNginx(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        context.Response.OnStarting(() =>
        {
            var contentType = context.Response.GetTypedHeaders().ContentType;
            if (contentType != null && 
                contentType.Parameters.Count > 0 && 
                !contentType.Charset.HasValue)
            {
                contentType.Parameters.Insert(0, new NameValueHeaderValue("charset", "utf-8"));
                context.Response.Headers["Content-Type"] = contentType.ToString();
            }
            return Task.CompletedTask;
        });
        await _next.Invoke(context);
    }
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!