kross
kross

Reputation: 3752

How to add nginx cache-control headers via an ebextensions config file?

First, I'm quite confused. I've ssh'ed to our elastic beanstalk instance to peruse where nginx is located etc, and it appears I can't find it.

Clearly, executing a curl command yields headers like:

Content-Encoding: gzip
Content-Type: text/css
Date: Wed, 23 Oct 2013 20:33:53 GMT
Last-Modified: Wed, 23 Oct 2013 18:59:04 GMT
Server: nginx/1.2.3
transfer-encoding: chunked
Connection: keep-alive

So I assume nginx is there somewhere. I was trying to discover the right location so I could inject a config file via ebextensions.

Essentially, I'm trying to make sure I set cache control in the far future, which this does not seem to be working:

  03-nginx.config:
    files:
      "/etc/nginx/conf.d/custom.conf" :
        mode: "000777"
        owner: ec2-user
        owner: ec2-user
        content: |
            location ~ ^/assets/ {
              expires 1y;
              add_header Cache-Control public;

              add_header ETag "";
              break;
            }

It appears that nginx is running via passenger-standalone.

[ec2-user@ip-10-196-221-244 support]$ pwd
/var/app/support
[ec2-user@ip-10-196-221-244 support]$ ls pids
passenger.pid  passenger.pid.lock
[ec2-user@ip-10-196-221-244 support]$ cat pids/passenger.pid
1781
[ec2-user@ip-10-196-221-244 support]$ ps -eaf | grep 1781
root      1781     1  0 Sep25 ?        00:00:00 nginx: master process /var/lib/passenger-standalone/3.0.17-x86_64-ruby1.9.3-linux-gcc4.6.2-1002/nginx-1.2.3/sbin/nginx -c /tmp/passenger-standalone.1704/config -p /tmp/passenger-standalone.1704/
webapp    1782  1781  0 Sep25 ?        00:00:57 nginx: worker process                                                                                                                                                       
ec2-user 26387 25743  0 10:53 pts/0    00:00:00 grep 1781

Update I'm using this config on the rails side with cloudfront configured. It's better than nothing though the headers don't match the ideal recommendation.

  #--------------------------------------------------------------------------------
  # CDN
  #   Enable serving of images, stylesheets, and JavaScripts from an asset server
  #   http://thediscoblog.com/blog/2013/05/01/the-rails-cloudfront-and-heroku-performance-hat-trick/
  #   http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/Expiration.html
  #   http://www.mnot.net/cache_docs/
  #   Check with http://redbot.org/
  #   - set asset host
  #--------------------------------------------------------------------------------
  config.action_controller.asset_host = "http://cdn.staging.acme.com"
  config.assets.compress = true # Compress JavaScripts and CSS
  config.assets.compile = false # Don't fallback to assets pipeline if a precompiled asset is missed
  config.assets.digest = true # Generate digests for assets URLs
  # must do this so we can inject the proper cache-control headers for cloudfront, served very few times anyways...
  #config.serve_static_assets = true
  config.static_cache_control = "public, max-age=#{1.year.to_i}"

Still open question: What am I missing? What location should I use to inject additional nginx configuration for standalone passenger?

Upvotes: 4

Views: 2133

Answers (1)

mentat
mentat

Reputation: 102

You can change the configuration that's generated for nginx with passenger standalone by modifying config.erb in "$(passenger-config --root)/resources/templates/standalone". See section 4.3 at http://www.modrails.com/documentation/Users%20guide%20Standalone.html.

Options for modifying it are:

  1. Replace entirely with a .ebextensions/.config files: call
  2. Try to in place change it using a command and some perl regexes

Either of these should work as both execute before the application server is started for the first time through hooks/preinit. Trying to apply it to a running instance may not work. You can kill your existing EC2 instance and autoscaling will spawn another one.

Upvotes: 1

Related Questions