Remove unnecessary HTTP headers in my rails

I'm currently developing an API where size matters: I want the response to contain as few bytes as possible. I optimized my JSON response, but the rails are still responding to a lot of weird headers.

HTTP/1.1 200 OK
Server: nginx/0.7.67                            # Not from Rails, so ok.
Date: Wed, 25 Apr 2012 20:17:21 GMT             # Date does not matter. We use ETag Can I remove this?
ETag: "678ff0c6074b9456832a710a3cab8e22"        # Needed.
Content-Type: application/json; charset=utf-8   # Also needed.
Transfer-Encoding: chunked                      # The alternative would be Content-Length, so ok.
Connection: keep-alive                          # Good, less TCP overhead.
Status: 200 OK                                  # Redundant! How can I remove this?
X-UA-Compatible: IE=Edge,chrome=1               # Completely unneded.
Cache-Control: no-cache                         # Not needed.
X-Request-Id: c468ce87bb6969541c74f6ea761bce27  # Not a real header at all.
X-Runtime: 0.001376                             # Same goes for this
X-Rack-Cache: invalidate, pass                  # And this.

Thus, there are many unnecessary HTTP headers. I could filter them on my server (nginx), but is there any way to stop this right on the rails?

+5
source share
3 answers

You can do this with the Rack middleware. See https://gist.github.com/02c1cc8ce504033d61bf for an example to do this in one.

When adding it to the application configuration, use something like config.middleware.insert_before(ActionDispatch::Static, ::HeaderDelete)

, rake middleware, ActionDispatch::Static.

http://guides.rubyonrails.org/rails_on_rack.html , Rack Rails.

+10

, Nginx, HttpHeadersMoreModule. , .

more_clear_headers, :

more_clear_headers Server Date Status X-UA-Compatible Cache-Control X-Request-Id X-Runtime X-Rack-Cache;

Server, , , .

Nginx , . Nginx , .

+9

, , x1a4 Stephen McCarth, .

HttpHeadersMoreModule, , - Ubuntu NginX , , ( ), .

- proxy_hide_header

server {

  location @unicorn {

    # ...
    proxy_hide_header X-Powered-By;
    proxy_hide_header X-Runtime;
    # ...
  }
}

note: @unicorn - upsteram, /, /assets,..

- , proxy_hide_header . ,

# /etc/nginx/sites-enabled/my_app
server {

  location @unicorn {

    # ...
    include /etc/nginx/shared/stealth_headers
    # ...
  }
}

# /etc/nginx/shared/stealth_headers
proxy_hide_header X-Powered-By;
proxy_hide_header X-Runtime    

, , , , x1a4?

, . gem party_foul. , Middlewares , , , , , . , , , , , , , NginX,

+ this does more if your NginX handles multiple configurations (you do not need to update multiple applications if some changes)

0
source

All Articles